search

Home  >  Q&A  >  body text

java.sql.SQLNonTransientConnectionException: Unable to create a connection to the database server

I have a problem with error code "java.sql.SQLNonTransientConnectionException: Unable to create a connection to the database server". I'm trying to establish a connection to a database using JDBC and the credentials are spelled correctly. Can you help me check possible problems in this project?

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);
        }
    }
}

I checked the connection, rewrote the code, added the controller to the manifest.

P粉086993788P粉086993788352 days ago414

reply all(1)I'll reply

  • P粉320361201

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

    Ideally, this problem occurs when the connection string is wrong. You can check that the formed connection string is in the correct format.

    The expected connection string for mysql jdbc connection is as follows,

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

    Please refer to https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html For more information about connection strings Lots of details.

    Also, make sure the database is up and running on the port provided in the code, i.e. localhost:3306.

    reply
    0
  • Cancelreply