简体   繁体   中英

Connecting Java to SQL DB

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.

PS: 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)

It looks like you get NPE on one of the following lines:

s.close();
c.close();

So, you need to check s and c on null before calling close() . Eg

if(s != null)
   s.close();

I'd also recommend you to look at try-with-resources. This can help to structure the code better and avoid the writing of boilerplate code. Related topic: How should I use try-with-resources with JDBC?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM