【问题标题】:How to fix this error: I/System.out: java.lang.ClassNotFoundException: com.mysql.jdbc.driver java.lang.NullPointerException如何修复此错误:I/System.out: java.lang.ClassNotFoundException: com.mysql.jdbc.driver java.lang.NullPointerException
【发布时间】:2020-04-23 01:27:58
【问题描述】:

我正在尝试将我的应用程序与 MySQL 数据库连接。我下载了 MySQL/Java 连接器并将 .jar 文件添加到我的库中,但它不起作用,而且我总是收到以下消息:

I/System.out: java.lang.ClassNotFoundException: com.mysql.jdbc.driver java.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);
                }
            }
        });
    }
}

我希望你能帮助我。

【问题讨论】:

  • 要使用网络服务,您必须使用 php,不是吗?我没有任何使用php的知识。所以首先我将尝试使用 jdbc,然后可能会使用 Web 服务。但是谢谢你的提示:)
  • Web 服务可以是任何东西,包括 Java/Kotlin(检查 Spring Boot)或任何其他具有适当框架的语言,如 Python & Flask 或 Javascript & Node。
  • 这是一个学校项目,我们必须使用Android Studio、Java和MySql。所以我不能使用其他环境或语言。
  • 据我所知,MySQL Connector/J 8 无法在 Android 上运行,因为它使用了 Android 上不可用的 Java 功能。

标签: java android mysql jdbc


【解决方案1】:

虽然我无法帮助您处理 ClassNotFoundException(因为我之前没有使用过 Gradle),但我可以告诉您 NullPointerException 是因为您没有在 getConnection() 方法中返回 Connection。您只需在 try/catch 块的末尾返回 null 。在try块中返回变量c,在catch块中返回null。

此外,您的 read 方法也不返回字符串,再次返回 null。设置 EditText 后返回您的字符串,或者如果您所做的只是更新参数值,则将方法设为无效。

最后,我认为您不需要在 getConnection() 方法中抛出异常,因为您已经在 try/catch 块中处理了它。祝你好运找到另一个问题。

【讨论】:

  • 嗯,我试过了,还是不行。但是谢谢你让我注意这个错误:)
猜你喜欢
  • 2013-08-12
  • 2022-10-25
  • 2018-11-09
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多