Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JDBC:连接返回NULL,怎么办?_Java_Mysql_Jdbc_Null_Database Connection - Fatal编程技术网

Java JDBC:连接返回NULL,怎么办?

Java JDBC:连接返回NULL,怎么办?,java,mysql,jdbc,null,database-connection,Java,Mysql,Jdbc,Null,Database Connection,这是以下程序的输出: Connection made! Schema Name:null Successfully connected to null Releasing all open resources ... 在establishConnection()内部,conn初始化为null。然后,try块中的第一条语句应该与数据库建立连接,第三条语句则打印conn的当前模式的名称 根据,getSchema()返回当前架构名称,如果没有,则返回null。 这意味着没有与conn关联的模式(我

这是以下程序的输出:

Connection made!
Schema Name:null

Successfully connected to null
Releasing all open resources ...
在establishConnection()内部,conn初始化为null。然后,try块中的第一条语句应该与数据库建立连接,第三条语句则打印conn的当前模式的名称

根据,getSchema()返回当前架构名称,如果没有,则返回null。

这意味着没有与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);
        }

    }

MySQL不支持模式的概念。对于MySQL,模式实际上就是数据库。发件人:

模式

从概念上讲,模式是一组相互关联的数据库对象,如表、表列、列的数据类型、索引、外键等。(……)

基于,除非使用最新的JDBC jar驱动程序,否则无法通过方法(自Java 7和JDBC 4一起添加)获取当前数据库(或多个数据库):

JDBC驱动程序(由于遗留问题,mysql直到5.0才将其称为“模式”,JDBC直到JDBC4才有办法选择模式)调用数据库“目录”,因此您必须调用getCatalogs()来获取数据库列表

为了证明上面斜体的句子,我做了一个肮脏的快速测试(除非您使用最新的JDBCJAR驱动程序)。使用
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();
        }
    }
}
输出:

test
然后,我在Java 8中执行了相同的代码,并使用mysql-connector-Java-5.1.31-bin.jar(目前最新的mysql Java连接器驱动程序)取消了包含
getSchema
的语句的注释。这就是输出:

null
test
这意味着它们仍然不支持
getSchema
方法

相关的:


这个问题不是重复的。另一个问题是关于Android的,主要是关于JTDs驱动程序(我没有用过)。我发布这条评论是为了澄清,因为我收到一条评论说它是重复的,在我解释后被删除了。你加载
MYSQL驱动程序的代码在哪里!问题不在于获取
null
连接,而在于执行
connection\getSchema
方法返回的
null
值。