java.sql.SQLNonTransientConnectionException: Es kann keine Verbindung zum Datenbankserver hergestellt werden
P粉086993788
P粉086993788 2024-01-10 16:45:25
0
1
340

Ich habe ein Problem mit dem Fehlercode „java.sql.SQLNonTransientConnectionException: Es konnte keine Verbindung zum Datenbankserver hergestellt werden“. Ich versuche, mithilfe von JDBC eine Verbindung zu einer Datenbank herzustellen, und die Anmeldeinformationen sind korrekt geschrieben. Können Sie mir helfen, mögliche Probleme in diesem Projekt zu überprüfen?

package com.example.daily.db;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.daily.R;
import com.mysql.cj.protocol.Resultset;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class controler extends AppCompatActivity {
    private controler controlador;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tercera);
        controlador = new controler();
    }
    public static void micontroler (){

        String usuario = "root";
        String password = "Prueb@1";
        String url = "jdbc:mysql://localhost:3306/daily_db";
        Connection conexion;
        Statement statement;
        Resultset rs;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


        try {
            conexion = DriverManager.getConnection(url,usuario,password);
            statement = conexion.createStatement();
            statement.executeUpdate("INSERT INTO USUARIOS(EMAIL,NOMBRES,APELLIDOS,APODO,FECHA_NACIMIENTO) VALUES ('Teste','TESTn','TESTa','TESTp','14/06/22')");


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

Ich habe die Verbindung überprüft, den Code neu geschrieben und den Controller zum Manifest hinzugefügt.

P粉086993788
P粉086993788

Antworte allen(1)
P粉320361201

理想情况下,当连接字符串错误时会出现此问题。您可以检查一下形成的连接字符串的格式是否正确。

mysql jdbc 连接的预期连接字符串如下,

jdbc:mysql://localhost:3306/daily_db?user=root&password=Prueb@1&serverTimezone=UTC

请参考https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html 有关连接字符串的更多详细信息。

此外,请确保数据库已启动并在代码中提供的端口上运行,即 localhost:3306。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!