Heim  >  Fragen und Antworten  >  Hauptteil

java.sql.SQLNonTransientConnectionException: Es kann keine Verbindung zum Datenbankserver hergestellt werden

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粉086993788P粉086993788283 Tage vor343

Antworte allen(1)Ich werde antworten

  • P粉320361201

    P粉3203612012024-01-11 14:39:51

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

    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。

    Antwort
    0
  • StornierenAntwort