Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC: Connection returning NULL, what to do?

This is the output of the program given below:

Connection made!
Schema Name:null

Successfully connected to null
Releasing all open resources ...

Inside establishConnection(), conn is initialized as null. Then the first statement inside the try block is supposed to establish a connection with the database, and the third statement is then printing the name of the current schema of conn.

According to the API, getSchema() returns the current schema name or null if there is none.

This means that there is no schema (I think the schema name is same as the database name) associated with conn? Can anyone suggest if I am correct in my anticipation, and also suggest why is that there is no schema or null associated with conn?

public class ConnectDB {

    private Connection establishConnection() throws SQLException {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test_final1", "root", "password");
            System.out.println("Connection made!");
            System.out.println("Schema Name:"+conn.getSchema()+"\n");
        } catch (SQLException sqle) {
            System.err.println("SQL Exception thrown while making connection");
            printSQLException(sqle);
        }
        return conn;
    }

    public static void main(String[] args) {
        ConnectDB cdb= new ConnectDB();
        Connection myconn=null;
        try{
            myconn=cdb.establishConnection();
            if(myconn!=null) System.out.println("Successfully connected to " + myconn.getSchema());
        }catch (SQLException e) {
              ConnectDB.printSQLException(e);
        } catch (Exception e) {
          e.printStackTrace(System.err);
        } finally {
          ConnectDB.closeConnection(myconn);
        }

    }
like image 533
Solace Avatar asked Apr 25 '14 21:04

Solace


People also ask

How does JDBC handle null values while working?

Avoid using getXXX( ) methods that return primitive data types. Use wrapper classes for primitive data types, and use the ResultSet object's wasNull( ) method to test whether the wrapper class variable that received the value returned by the getXXX( ) method should be set to null.

What does connection null mean?

connection = null means connection object is eligible for garbage collection and does not return to connection pool. and f or. connection.close() means the connection object return to the. connection pool.

What is JDBC connection error?

JDBC connection errors. Connection errors are typically caused by incorrect or invalid connection string parameters, such as an invalid host name or database name. Verify that you are using correct and valid connection string parameters for the Gate.

What happens if we don't close the connection in JDBC?

If we don't close the connection, it will lead to connection memory leakage. Until application server/web server is shut down, connection will remain active, even if the user logs out.


1 Answers

MySQL doesn't support the concept of schema. For MySQL, the schema is in fact the database. From MySQL Glossary:

schema

Conceptually, a schema is a set of interrelated database objects, such as tables, table columns, data types of the columns, indexes, foreign keys, and so on. (...)

Based on this answer from MySQL forums, you can't get the current database (or databases) through Connection#getSchema method (which was added since Java 7 with JDBC 4) but using Connection#getCatalog unless you use the latest JDBC jar driver:

The JDBC driver (because of legacy, mysql didn't call them "schemas" until 5.0, and JDBC didn't have a way to select schemas until JDBC4), calls databases "catalogs", so thus you have to call getCatalogs() to get the list of databases

I made a dirty quick to prove the italic sentence above (unless you use the latest JDBC jar driver). Using getCatalog() worked using Java 6:

public class DirtyQuickTest {
    private static final String url = "jdbc:mysql://localhost:7841/test";
    private static final String user = "lmendozaj";
    private static final String password = "s3cr3t"; //I won't show you my password
    public static void main(String[] args) throws Exception {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password);
            //commented since doesn't exists in Java 6
            //System.out.println(con.getSchema());
            System.out.println(con.getCatalog());
        } finally {
            con.close();
        }
    }
}

Output:

test

Then I executed the same code in Java 8 and uncommented the statement containing getSchema using mysql-connector-java-5.1.31-bin.jar (currently the latest Java Connector Driver for MySQL). This was the output:

null
test

Which means they still don't support getSchema method.

Related:

  • Difference Between Schema / Database in MySQL
like image 151
Luiggi Mendoza Avatar answered Sep 18 '22 09:09

Luiggi Mendoza