gpt4 book ai didi

android - 创建 Android 应用程序和 MS SQL Server 之间的连接

转载 作者:行者123 更新时间:2023-11-29 22:08:05 27 4
gpt4 key购买 nike

我的代码:

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());
}
}
});

日志猫:

>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

当我输入用户名和密码的值时,模拟器显示消息:

Unfortunately your app has stopped working. Can anyone pointing me to resolve it. thanks in advance

最佳答案

这不是将移动应用程序与数据库连接的正确方法。相反,您应该创建一个在服务器上运行的中间件(REST/SOAP Web服务)。该中间件将依次连接数据库,您的移动应用程序(无论是 Android、iOS 还是 Windows 应用程序)都可以与该中间件通信。

关于android - 创建 Android 应用程序和 MS SQL Server 之间的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963916/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com