我正在尝试将我的应用程序与MySQL数据库连接。我下载了MySQL / Java连接器,并将.jar文件添加到了我的库中,但是我总是收到以下消息:
I / System.out:java.lang.ClassNotFoundException:com.mysql.jdbc.driverjava.lang.NullPointerException
build.gradle
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
//compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.18'
implementation files('libs/mysql-connector-java-8.0.18.jar')
}
我尝试与数据库连接的班级:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import android.os.AsyncTask;
import android.widget.EditText;
import java.sql.*;
import java.util.ArrayList;
import com.mysql.jdbc.Driver;
public class DBConnection {
public Connection getConnection() throws Exception
{
try {
String driver = "com.mysql.jdbc.driver";
String url = "jdbc:mysql://127.0.0.1:3306/test";
String username = "root";
String password = "";
Class.forName(driver).newInstance();
Connection c = DriverManager.getConnection(url, username, password);
System.out.println("Connected");
}catch (Exception e)
{
System.out.println(e);
}
return null;
}
ArrayList<String> array = new ArrayList<>();
public String read(EditText e1)
{
try{
Connection c = getConnection();
PreparedStatement statement = c.prepareStatement("SELECT c_name FROM c_customer;");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next())
{
array.add(resultSet.getString("c_name"));
}
e1.setText(array.get(0));
System.out.printf("Successfull");
}catch (Exception e)
{
System.out.println(e);
}
return null;
}
}
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
DBConnection db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText user = findViewById(R.id.nameInput);
final EditText pass = findViewById(R.id.passwortInput);
Button login = findViewById(R.id.loginButton);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db = new DBConnection();
db.read(user);
}
}
});
}
}
希望您能帮助我。
虽然我无法为您提供ClassNotFoundException的帮助(因为我之前没有与Gradle一起工作过,但是我可以告诉您NullPointerException是因为您没有在getConnection()方法中返回Connection。您只需在try / catch块的末尾返回null。在try块中返回变量c,在catch块中返回null。
此外,您的read方法也不会返回String,而是仅返回null。设置EditText后返回您的String,或者如果您正在做的只是更新参数值,则只需使该方法无效。
最后,我认为您不需要在getConnection()方法中引发Exception,因为您已经在try / catch块中进行了处理。祝你好运,找出另一个问题在哪里。