Androidでjdbcを使用しています(推奨されません)
次のコードがあります。
public int CheckUser(String uName,String password)
{
try {
String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
int value=0;
if (resultSet.next()) {
value=resultSet.getInt(1);
}
return value;
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
ヌルポインター例外が発生します statement = conn.createStatement();
ログキャット:
09-12 11:04:14.543: W/KeyCharacterMap(483): No keyboard for id 0
09-12 11:04:14.553: W/KeyCharacterMap(483): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-12 11:04:51.173: W/System.err(483): java.lang.NullPointerException
09-12 11:04:51.194: W/System.err(483): at com.example.messagesql.gaSQLConnect.CheckUser(gaSQLConnect.java:69)
09-12 11:04:51.213: W/System.err(483): at com.example.messagesql.Login$UserLogin.doInBackground(Login.java:109)
09-12 11:04:51.213: W/System.err(483): at com.example.messagesql.Login$UserLogin.doInBackground(Login.java:1)
09-12 11:04:51.225: W/System.err(483): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-12 11:04:51.233: W/System.err(483): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-12 11:04:51.233: W/System.err(483): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-12 11:04:51.253: W/System.err(483): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-12 11:04:51.253: W/System.err(483): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-12 11:04:51.264: W/System.err(483): at java.lang.Thread.run(Thread.java:1019)
助けてください。
Statement オブジェクトをローカルで宣言しようとしましたが、効果がなく、エラーが引き続き発生します。
この関数を呼び出すファイルのコード:
public class Login extends Activity {
public static String resp = "";
public static String name = "";
String username;
String password;
EditText etLoginID;
EditText etPassword;
final DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
List<Contact> contacts = db.getAllContacts();
if(contacts.size() != 0){
for (Contact cn : contacts){
resp = cn.getID();
name = cn.getName();
}
// Intent i=new Intent(getApplicationContext(),Message.class);
//startActivity(i);
}
etLoginID=(EditText)findViewById(R.id.etName);
etPassword=(EditText)findViewById(R.id.etPassword);
Button btnLogin =(Button)findViewById(R.id.btnLogin);
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");
username=etLoginID.getText().toString();
password=etPassword.getText().toString();
String[] values=new String[2];
values[0]=username;
values[1]=password;
UserLogin ul=new UserLogin();
ul.execute(values);
//Intent i= new Intent(getApplicationContext(),Messages.class);
//startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
private class UserLogin extends AsyncTask<String[], Void, String>{
final gaSQLConnect con=new gaSQLConnect();
String[] values;
@Override
protected String doInBackground(String[]... params) {
// TODO Auto-generated method stub
values=params[0];
String result=null;
int uid;
db.deleteContact(new Contact(resp));
if (!values[0].isEmpty() && !values[1].isEmpty())
{
uid=con.CheckUser(etLoginID.getText().toString(), etPassword.getText().toString());
return Integer.toString(uid) ;
}
else{
uid=0;;
}
return Integer.toString(uid) ;
}
protected void onPostExecute(String result){
Toast.makeText(getApplicationContext(),result, Toast.LENGTH_LONG).show();
db.addContact(new Contact(result, values[0]));
Intent i=new Intent(getApplicationContext(),Messages.class);
startActivity(i);
}
}
上記は、関数を呼び出すファイルのコードです。
この onClick ログイン方法では、接続を設定しました。
また、アプリケーション全体で持続しないのはなぜですか?
DBConnection クラスのコード全体:
public class gaSQLConnect
{
String url ="";
Connection conn=null;
Statement statement=null;
ResultSet resultSet=null;
public ResultSet getMessages(int uid)
{
try
{
String sql="select mid,uid,message,rstamp from MessagesMaster where uid='"+uid+"' aand rstamp = 0 order by tstamp desc";
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
return resultSet;
}
catch(Exception ex)
{
}
return resultSet;
}
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();
}
}
int recFound=0;
public int CheckUser(String uName,String password)
{
try {
Statement statement=null;
ResultSet resultSet=null;
String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
int value=0;
if (resultSet.next()) {
value=resultSet.getInt(1);
}
return value;
/* if (value > 0) {
recFound = 1;
} else {
recFound = 0;
}
if (recFound > 0)
{
return true;
} else {
return false;
}*/
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public void UpdateMessage(String[] mlst) {
// TODO Auto-generated method stub
try
{
String sql="update messagesmaster set rstamp = 1 where mid in (" + mlst + ") ";
statement = conn.createStatement();
statement.executeUpdate(sql);
}
catch(Exception ex)
{
}
}
}