I'm trying to connect Java to a 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.
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 Net Beans, but not on Intellij Idea (Mac), is it a driver problem?
Update: I don't know why, but the code returns a different error:
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("Query 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>");
}
}
}
Error
com.mysql.jdbc.Driver
I should close the connection
Exception in thread "main" java.lang.NullPointerException
at rubricacreate.RubricaCreate.(RubricaCreate.java:53)
at rubricacreate.RubricaCreate.main(RubricaCreate.java:119)