我的代码:
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 + ";database name =" + database + ";user =" + user + ";password =" + password + ";" ;
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException se)
{
Log.e("Error", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("Error",e.getMessage());
}
catch (Exception e)
{
Log.e("Error",e.getMessage());
}
return connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginbtn = (Button) findViewById(R.id.btn_login);
errorlbl = (TextView) findViewById(R.id.lbl_error);
edname = (EditText) findViewById(R.id.txt_username);
edpassword = (EditText) findViewById(R.id.txt_password);
ipaddress = "xxxxxxxxx";
username = "xxxxxxx";
password = "xxxxxxx";
db = "DKMobile";
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 DKMUsers where user_ID = '"+ edname.getText().toString()+"'and Password = '"+edpassword.getText().toString()+"'");
if(rs!=null && rs.next()){
Intent i = new Intent(MainActivity.this,MainActivity.class);
startActivity(i);
}
else {
errorlbl.setText("Sorry, wrong credentials!!!");
}
}catch (SQLException e)
{
errorlbl.setText(e.getMessage().toString());
}
}
});
logcat的:
>08-12 16:46:20.183 2027-2027/com.dk.dkmobile E/Error﹕ Single-Sign-On is only supported on Windows. Please specify a user name.
08-12 16:51:49.661 2027-2027/com.dk.dkmobile E/Error﹕ Single-Sign-On is only supported on Windows. Please specify a user name.
08-12 16:51:49.661 2027-2027/com.dk.dkmobile D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
08-12 16:51:49.669 2027-2027/com.dk.dkmobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.dk.dkmobile, PID: 2027
java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
at com.dk.dkmobile.MainActivity$1.onClick(MainActivity.java:83)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-12 16:51:54.120 2027-2027/com.dk.dkmobile I/Process﹕ Sending signal. PID: 2027 SIG: 9
当我输入用户名和密码的值时,模拟器会显示消息:
不幸的是,您的应用已停止工作。任何人都可以指点我解决它。提前谢谢
答案 0 :(得分:0)
这不是将移动应用程序与数据库连接的正确方法。 相反,您应该创建一个在服务器上运行的中间件(REST / SOAP Web服务)。 这个中间件将依次与数据库和您的移动应用程序连接,无论是Android,iOS还是Windows应用程序都可以与此中间件进行通信。
答案 1 :(得分:0)
您正在尝试连接到mysql。您应该创建一个php / jsp或任何在线运行查询的语言,并以json或xml格式提供数据,然后使用asynctask使用httpclient解析这些数据。这将是一个更好的方法
答案 2 :(得分:0)
仅供参考,没有任何服务器通信(Web services),您无法在Android应用程序中使用网站数据库。
如果您想管理网站和Android应用程序之间的通信,则意味着您需要一个拥有Web /移动应用程序数据的集中式服务器。那么,之后通过使用Web服务方法(REST / SOAP),您可以实现它们之间的通信