Webサービスを使用せずにAndroidを介してSqlDatabase接続を行っています。
接続クラスには次の 2 つの機能があります。
public class gaSQLConnect
{
String url ="";
Connection conn=null;
Statement statement=null;
ResultSet resultSet=null;
public void setConnection(String DBName,String UserName,String Password)
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
url ="jdbc:jtds:sqlserver://10.0.2.2:1433;instanceName=14GRAFICALI\\MSSQLSERVER2008;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password;
conn =DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public Boolean CheckUser(String uName,String password)
{
int recFound=0;
try
{
statement=conn.createStatement();
resultSet=statement.executeQuery("select * from UserMaster where username="+uName+" and password="+password+"");
if(resultSet.last())
{
if(resultSet.getRow()>0)
{
recFound=1;
}
else
{
recFound=0;
}
}
}
catch (Exception e) {
e.printStackTrace();
recFound=0;
}
if(recFound==0)
return false;
else
return true;
}
}
次のコードを使用してこれらの関数を呼び出しています。
final gaSQLConnect con=new gaSQLConnect();
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
con.setConnection("AndroidDB", "sa", "ok");
boolean isValidUser=con.CheckUser(etLoginID.getText().toString(), etPassword.getText().toString());
if(isValidUser)
{
Intent i= new Intent(getApplicationContext(),Messages.class);
startActivity(i);
}
}
});
私はアプリをデバッグし、例外が次の行にあることを知りました:
resultSet=statement.executeQuery("select * from UserMaster where username="+uName+" and password="+password+"");
ログキャット:
09-11 11:14:05.896: E/AndroidRuntime(756): FATAL EXCEPTION: main
09-11 11:14:05.896: E/AndroidRuntime(756): java.lang.NullPointerException
09-11 11:14:05.896: E/AndroidRuntime(756): at com.example.messagesql.gaSQLConnect.CheckUser(gaSQLConnect.java:49)
09-11 11:14:05.896: E/AndroidRuntime(756): at com.example.messagesql.Login$1.onClick(Login.java:37)
09-11 11:14:05.896: E/AndroidRuntime(756): at android.view.View.performClick(View.java:2485)
09-11 11:14:05.896: E/AndroidRuntime(756): at android.view.View$PerformClick.run(View.java:9080)
09-11 11:14:05.896: E/AndroidRuntime(756): at android.os.Handler.handleCallback(Handler.java:587)
09-11 11:14:05.896: E/AndroidRuntime(756): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 11:14:05.896: E/AndroidRuntime(756): at android.os.Looper.loop(Looper.java:123)
09-11 11:14:05.896: E/AndroidRuntime(756): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-11 11:14:05.896: E/AndroidRuntime(756): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 11:14:05.896: E/AndroidRuntime(756): at java.lang.reflect.Method.invoke(Method.java:507)
09-11 11:14:05.896: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-11 11:14:05.896: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-11 11:14:05.896: E/AndroidRuntime(756): at dalvik.system.NativeStart.main(Native Method)