我是Java的新手,我试图建立连接并使用Android Studio在运行在xampp上的mysql数据库上执行一条语句,但是通过每种不同的方法,我都会遇到不同的错误。
仅通过直接从类中运行main方法,就可以使用mysql连接器5.1.47和8.0.16从表中建立连接并收集数据,而在运行模拟器时则无法。
我还尝试了以下YouTube上的一些教程,例如:https://youtu.be/kZAmVRuRQio 但是我最终遇到了更多错误。
ConnectionClass.java代码:
package com.example.fitnet;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionClass {
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
System.out.println("Starting driver");
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Connecting");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fitnet", "root", "");
System.out.println("Connected");
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return conn;
}
}
我的登录方法:
public void login() {
Intent intent = new Intent(this, AppActivity.class);
EditText emailF = findViewById(R.id.inputEmail);
String email = emailF.getText().toString();
EditText passwordF = findViewById(R.id.inputPassword);
String password = passwordF.getText().toString();
try {
connectionClass = new ConnectionClass();
Connection con = connectionClass.CONN();
if (con == null) {
System.out.println("Please check your internet connection");
} else {
String query = "select * from users where email='" + email + "' and password='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String userID = rs.getString(1);
String nm = rs.getString(2);
String pw = rs.getString(3);
System.out.println(userID + " " + nm + " " + pw);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这些是在我的应用崩溃后立即显示错误消息的日志:
Starting driver
W/.example.fitne: Accessing hidden method Ljava/lang/invoke/LambdaMetafactory;->metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; (blacklist, linking, denied)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fitnet, PID: 6109
java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.runtime/javalib/core-oj.jar)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.<clinit>(AbandonedConnectionCleanupThread.java:58)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at java.lang.Class.forName(Class.java:379)
at com.mysql.cj.jdbc.NonRegisteringDriver.<clinit>(NonRegisteringDriver.java:99)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at java.lang.Class.forName(Class.java:379)
at com.example.fitnet.ConnectionClass.CONN(ConnectionClass.java:20)
at com.example.fitnet.LoginActivity.login(LoginActivity.java:60)
at com.example.fitnet.LoginActivity$1.onClick(LoginActivity.java:34)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3400(View.java:801)
at android.view.View$PerformClick.run(View.java:27301)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)