java - JDBC: Connection returning NULL, what to do? -
This is the output of the program given below:
The connection has been created! Schema Name: The blank is successfully linked to all open resources leakage ... Within the established connection (), conn is started as zero. The first statement inside the try block is considered to establish a connection with the database, and the third statement is then printing the name of the current schema of conn.
According to GetSchema () names the current schema or if none, then zero Does this mean that any schema (I think schema name is similar to database name) is connected to conn? Can someone suggest that I am right in my anticipation, and it is also suggesting that there is no schema or null associated with Kan? public class connectivity {private connection established connection () SQLException throws {connection conn = null; Try {conn = DriverManager.getConnection ("jdbc: mysql: // localhost: 3306 / test_final1", "root", "password"); System.out.println ("made connection!"); System.out.println ("schema name:" + conn.getSchema () + "\ n"); } Grip (Escalation Scale) {System.err.println ("SQL exception thrown during connection"); PrintSQLException (sqle); } Return conn; } Public static zero main (string [] args) {connectivity CDB = new connectible (); Connection myconn = null; Try {Myconn = cdb.establishConnection (); If (myconn! = Null) is successfully connected to System.out.println ("myconn.getSchema ()"); } Grip (SQLException E) {ConnectDB.printSQLException (E); } Hold (exception e) {e.printStackTrace (System.err); } Finally {ConnectDB.closeConnection (myconn); }}
MySQL does not support the concept of schema . For MySQL, schema is actually database from: Schema Conceptually, a schema is a set of interactive database objects, Such as tables, table columns, data type columns, indexed, foreign keys, and so on. Depending on (...)
, you can not get through the current database (or database) method (which was paired with JDBC4 from Java 7), But you use the latest JDBC jar driver while using : JDBC driver (due to heritage, mysql did not call them "schema" till 5.0 , And JDBC did not have any way to select the schema until JDBC44), the database was " Catalog ", so you have to call getCatalogs () to get the list of databases
I made the dirty quick to prove italic sentence from top ( when Till you do not use the latest JDBC jar driver ). Using the Java code, using getcatalog () : public class DirtyQuickTest {Private constant last string url = "jdbc: mysql: // localhost : 7841 / test "; Personal stable final string user = "lmendozaj"; Private Static Last String Password = "s3cr3t"; // I will not show you my password Public static zero main (string [] args throws exceptions {connection con = null; Try {class.forName ("com.mysql.jdbc.Driver"); Con = DriverManager.getConnection (url, user, password); // does not exist in Java 6 since the comment / system.out.println (con.getSchema ()); Println (con.getCatalog ()); } Finally {con.close (); }}} Output:
test Then I executed the same code in Java 8 MySQL Description with getSchema using the -connector-java-5.1.31-bin.jar (currently the latest Java connector driver for MySQL). This output was: null test This means that they still do not support the getSchema method. Related:
-
Comments
Post a Comment