gpt4 book ai didi

安卓+mysql : Connection class returns a Null object

转载 作者:搜寻专家 更新时间:2023-11-01 09:24:17 25 4
gpt4 key购买 nike

我正在尝试通过 jtds jdbc 将我的 android 登录应用程序连接到 XAMP 服务器(Apache 在本地机器上运行)。似乎代码无法连接服务器。这是代码:

连接类.java

package com.ercess.databaseconnection;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.os.Bundle;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionClass {

public String driver = "net.sourceforge.jtds.jdbc.Driver";

String url = "jdbc:mysql://localhost/events-test";
//String url = "jdbc:mysql://127.0.0.1/events-test";
public String un = "root";
public String password = "password";
public String db = "users";

@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
java.sql.Connection conn = null;
String ConnURL = null;
try{
Class.forName(driver);
ConnURL = "jdbc:jtds:sqlserver://" + "127.0.0.1" +";databaseName="+ db + ";user=" + un+ ";password=" + password + ";";

conn = DriverManager.getConnection(ConnURL);



}catch (SQLException se){
Log.e("ERROR", se.getMessage());
}catch (ClassNotFoundException e){
Log.e("ERROR", e.getMessage());
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}
//Log.d("conn",conn.toString());
return conn;
}
}

主 Activity .java

    package com.ercess.databaseconnection;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class MainActivity extends AppCompatActivity {
EditText email, password;
Button login;
ProgressDialog progressDialog;
ConnectionClass connectionClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
connectionClass = new ConnectionClass();
progressDialog = new ProgressDialog(this);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dologin dologin = new Dologin();
dologin.execute();
}
});
}
public class Dologin extends AsyncTask<String, String, String>{
String emailstring = email.getText().toString();
String passstring = password.getText().toString();
String z = "";
boolean isSuccess = false;
String em, pass;
@Override
protected void onPreExecute() {
progressDialog.setMessage("Loading...");
progressDialog.show();
super.onPreExecute();
}
protected String doInBackground(String... params){
if(emailstring.trim().equals("") || passstring.trim().equals(""))
z = "Please enter all fields........";
else
{
try{
Connection con = connectionClass.CONN();
if(con == null){
z="Please, check your internet connection....";
}else{
String query = "select * from users where user='"+emailstring+"'and password='"+passstring+"'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
em = rs.getString(0);

pass = rs.getString(1);

if(em.equals(emailstring) && pass.equals(passstring))
{
isSuccess = true;
z = "Login successful...";
}
else isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions"+ex;
}
}
return z;
}
@Override
protected void onPostExecute (String s){
Toast.makeText(getBaseContext(),""+z, Toast.LENGTH_LONG).show();
progressDialog.hide();
}
}
}

我收到此输出:“请检查您的互联网连接……”。

此消息应该仅在 con = null 时触发。如何解决?

最佳答案

Android 不支持 MySQL 或 SQL Server:-你可以使用 Sqllite https://www.tutorialspoint.com/android/android_sqlite_database.htm或者您可以通过 PHP 访问 MySQL :- https://www.tutorialspoint.com/android/android_php_mysql.htm

关于安卓+mysql : Connection class returns a Null object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51984848/

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