[英]Problem with connecting MYSQL in JAVA android app
I've tried all methods and ways from stack and videos and none of them actually worked so far.我已经尝试了堆栈和视频中的所有方法和方法,但到目前为止,它们都没有真正起作用。
MAIN ACTIVITY CLASS主要活动 CLASS
package com.example.androidmysql;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
TextView text;
Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView);
show = (Button) findViewById(R.id.button);
show.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/example","root","test");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM user");
String records ="";
while (resultSet.next()) {
records += resultSet.getString(1) + " " + resultSet.getString(2) + "\n";
}
text.setText(records);
}
catch (Exception e) {
e.printStackTrace();
text.setText(e.toString());
}
}
});
}
}
The main problem is that JDBC can't connect to database主要问题是JDBC无法连接数据库
OUTPUT OUTPUT
W/System.err: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
W/System.err: at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1009)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql.DriverManager.getConnection(DriverManager.java:580)
at java.sql.DriverManager.getConnection(DriverManager.java:218)
at com.example.androidmysql.MainActivity$1.onClick(MainActivity.java:47)
at android.view.View.performClick(View.java:7448)
W/System.err: at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
Here is the code which i uploaded on github.这是我在 github 上上传的代码。 Can someone test it on their pc with their example database?有人可以用他们的示例数据库在他们的电脑上测试它吗? https://github.com/simplybychris/DatabaseClientApp/tree/master https://github.com/simplybychris/DatabaseClientApp/tree/master
Database user and password are good, cause I've tested it on separate class without main activity and outside Java project and it works... It looks like it won't work in Mobile Classes数据库用户和密码很好,因为我已经在没有主要活动的单独 class 和 Java 项目之外对其进行了测试,它可以工作......看起来它在移动课程中不起作用
Thanks for any tips感谢您的任何提示
I don't know why people minusing the question if there is no correct answer.如果没有正确答案,我不知道为什么人们会忽略这个问题。 Anyway after 2 days of searching I've finally found the solution.无论如何,经过2天的搜索,我终于找到了解决方案。 There's the problem while connecting with mysql latest 8.0 driver.连接mysql最新8.0驱动时出现问题。 I suggest to change it to mysql 5.7.我建议将其更改为 mysql 5.7。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.