I am and building an android app. Where my ConnectionURL
is
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "databaseName=" + database + ";user=" + user
+ ";password=" + password + ";";
Acc. to this answer: Help me create a jTDS connection string
The connectionString should have been
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value[;...]]
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS;user=sa;password=s3cr3t
Or, if you prefer to use getConnection(url, "sa", "s3cr3t"):
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS
But, I am not using SQLEXPRESS. I using Microsoft SQL Server Developer Edition.
What I have done is, one button click(android) the event occurs. Login form verifies UserName and Password and writes to new TextEdit.
package com.muchmore.mydatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
Button loginbtn;
TextView errorlbl;
EditText edname, edpassword;
Connection connect;
PreparedStatement preparedStatement;
Statement st;
String ipaddress, db, username, password;
@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
String database, String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "databaseName=" + database + ";user=" + user
+ ";password=" + password + ";";
/*
I was tyring to built a right connection string here. Please get it working :)
ConnectionURL = "jdbc:jtds:sqlserver://localhost/"
+ "databaseName=" + "MyDatabase" + ";user=" + "sa"
+ ";password=" + "sa@123" + ";";
*/
//private String URL = "jdbc:jtds:sqlserver://localhost/brandix;instance=sqlexpress;useNTLMv2=true;domain=workgroup"
connection = DriverManager.getConnection(ConnectionURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginbtn = (Button) findViewById(R.id.btnlogin);
errorlbl = (TextView) findViewById(R.id.lblerror);
edname = (EditText) findViewById(R.id.txtname);
edpassword = (EditText) findViewById(R.id.txtpassword);
ipaddress = "127.0.0.1";
db = "MyDatabase";
username = "sa";
password = "sa@123";
connect = ConnectionHelper(username, password, db, ipaddress);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
connect = ConnectionHelper(username, password, db, ipaddress);
st = connect.createStatement();
ResultSet rs = st.executeQuery("select * from login where userid='" + edname.getText().toString() + "' and password='" + edpassword.getText().toString() + "'");
if (rs != null && rs.next()) {
errorlbl.setText("Login Successful!!!");
} else {
errorlbl.setText("Sorry, wrong credentials!!!");
}
} catch (SQLException e) {
errorlbl.setText(e.getMessage().toString());
}
}
});
There is null reference in connect reference. So, I am sure there is some error in connection string. Otherwise my android code looks fine. Please see whats the right format of string.