Java JDBC:连接返回 NULL,怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23303206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC: Connection returning NULL, what to do?
提问by Solace
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.
在 createdConnection() 中,conn 被初始化为 null。然后 try 块中的第一条语句应该建立与数据库的连接,然后第三条语句打印 conn 的当前模式的名称。
According to the API, getSchema() returns the current schema name or null if there is none.
根据API,getSchema() 返回当前模式名称,如果没有,则返回 null。
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?
这意味着没有与 conn 关联的架构(我认为架构名称与数据库名称相同)?任何人都可以建议我的预期是否正确,并建议为什么没有与 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);
}
}
采纳答案by Luiggi Mendoza
MySQL doesn't support the concept of schema. For MySQL, the schema is in fact the database. From MySQL Glossary:
MySQL 不支持schema的概念。对于 MySQL,模式实际上是数据库。来自MySQL 词汇表:
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:
根据MySQL 论坛的这个答案,您无法通过Connection#getSchema
方法(自 Java 7 和 JDBC 4 以来添加)获取当前数据库(或数据库),但除非您使用最新的 JDBC jar 驱动程序:Connection#getCatalog
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
JDBC 驱动程序(由于遗留问题,mysql 直到 5.0 才称它们为“模式”,而 JDBC 直到 JDBC4 才具有选择模式的方法),将数据库称为“目录”,因此您必须调用 getCatalogs()获取数据库列表
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:
我做了一个肮脏的快速证明上面的斜体句子(除非你使用最新的 JDBC jar 驱动程序)。使用getCatalog()
使用 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:
然后我在 Java 8 中执行相同的代码并取消注释包含getSchema
using mysql-connector-java-5.1.31-bin.jar(当前最新的 MySQL Java 连接器驱动程序)的语句。这是输出:
null
test
Which means they still don't support getSchema
method.
这意味着他们仍然不支持getSchema
方法。
Related:
有关的: