Connecting Java to SQL DB (XAMPP)

Answered

I'm trying to connect Java to SQL DataBase and XAMPP, but the ide returns me the error 08S01. I read many topics on the web about the driver, but I couldn't solve my problem. Can someone help me? I attached my code. Thanks in advance, Davide

P.S.: The name of the DB is written in the arguments of the ide. I'm trying to connect and create a table into the DataBase. However the code works on other ide, but not on Intellij Idea (Mac), is it a driver problem?

ERROR:

Error
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Error 0
Error 08S01

 

CODE:

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RubricaCreate {
private static String driver = "com.mysql.jdbc.Driver";
private String databaseURL = "jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String password = "";

private static Connection c = null;
private static Statement s = null;

public RubricaCreate(String nome) {
    try {
        try {
            Class.forName(driver);
            c = DriverManager.getConnection(databaseURL, user, password);
            System.out.println("Stablished connection");
            c.setAutoCommit(true);
            s = c.createStatement();
        } catch (ClassNotFoundException ex) {
            System.out.println("Error");
            System.out.println(ex.getMessage());
            return;
        } catch (SQLException ex) {
            System.out.println("Error");
            showSQLException(ex);
            return;
        }
        try {
            String query = "CREATE DATABASE" + nome;
            s.executeUpdate(query);
            System.out.println("Query executed");
        } catch (SQLException ex) {
            System.out.println("SQL error");
            showSQLException(ex);
        }
    } finally {
        System.out.println("I should close the connection");
        try {
            s.close(); //-------------ERROR HERE----------------
        } catch (SQLException ex) {
            showSQLException(ex);
        }
        try {
            c.close();
        } catch (SQLException ex) {
            showSQLException(ex);
        }
    }
}
private static void RubricaCreateTable(String nomeDB, String nomeTabella) {
    try {
        String databaseURLtable = "jdbc:mysql://localhost:3306/" + nomeDB;
        Class.forName(driver);
        c = DriverManager.getConnection(databaseURLtable, user, password);
        System.out.println("Stablished connection");
        c.setAutoCommit(true);
        s = c.createStatement();
    } catch (ClassNotFoundException ex) {
        System.out.println("Error");
        System.out.println(ex.getMessage());
        return;
    } catch (SQLException ex) {
        System.out.println("Error");
        showSQLException(ex);
        return;
    }
    try {
        String query = "CREATE TABLE " + nomeTabella + " (id INT, nome VARCHAR(10))";
        s.executeUpdate(query);
        System.out.println("Quey executed");
    } catch (SQLException ex) {
        System.out.println("SQL error");
        showSQLException(ex);
    } finally {
        System.out.println("");
        try {
            s.close();
        } catch (SQLException ex) {
            showSQLException(ex);
        }
        try {
            c.close();
        } catch (SQLException ex) {
            showSQLException(ex);
        }
    }
}

private static void showSQLException(java.sql.SQLException e) {
    SQLException next = e;
    while (next != null) {
        System.out.println(next.getMessage());
        System.out.println("Error" + next.getErrorCode());
        if (next.getErrorCode() == 1064) {
            System.out.println("Do that");
        }
        System.out.println("Errore" + next.getSQLState());
        next = next.getNextException();
    }
}

public static void main(String[] args) {
    if (args.length == 1) {
        new RubricaCreate(args[0]); //---------ERROR HERE----------
        String nomeTab = "mariaDB";
        RubricaCreateTable(args[0], nomeTab);
    } else {
        System.out.println("java RubricaCreate <nomeDB>");
    }
}
}
0
1 comment

Hello,

Looks like we have similar issue on YouTrack: https://youtrack.jetbrains.com/issue/DG-8830
Could you please answer the questions in comments?

0

Please sign in to leave a comment.