2014-04-25 71 views
5

这是程序的下面给出的输出:)JDBC:连接返回NULL,该怎么办?

Connection made! 
Schema Name:null 

Successfully connected to null 
Releasing all open resources ... 

内部establishConnection(康涅狄格州被初始化为空。然后,try块内的第一条语句应该与数据库建立连接,第三条语句将打印conn当前模式的名称。

根据API,getSchema()返回当前模式名称,如果没有,则返回null。

这意味着没有与conn关联的模式(我认为模式名与数据库名相同)任何人都可以建议我的预期是否正确,并且还建议为什么没有与conn相关的模式或null?

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); 
     } 

    } 
+2

这个问题是不是重复。另一个问题是关于Android,非常关注JTD驱动程序(我没有使用过)。我发表了这条评论以作澄清,因为我有评论说它是重复的,在我的解释后被删除。 – Solace

+0

哪里是你已经加载'MYSQL驱动程序'的代码 –

+1

人!问题不在于获取'null'连接,而是执行'Connection#getSchema'方法获得'null'返回值。 –

回答

6

MySQL不支持架构的概念。对于MySQL,模式实际上是数据库。从MySQL Glossary

模式

概念,模式是一组相互关联的数据库对象,如表,表中的列,列的数据类型,索引,外键,等等。 (...)基于this answer from MySQL forums

,则无法通过Connection#getSchema方法(因为Java 7中并加入与JDBC 4),但使用Connection#getCatalog获得当前的数据库(或数据库),除非你使用最新的JDBC驱动程序的jar

JDBC驱动程序(遗留下来的,因为MySQL并没有称之为“模式”,直到5.0,JDBC没有办法选择的模式,直到JDBC4),调用数据库“目录“,因此你必须调用getCatalogs()来获取数据库列表

我肮脏的很快就证明了上面的斜体语句(,除非使用最新的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(); 
     } 
    } 
} 

输出:

test 

然后我执行相同的代码在Java中8和未注释使用MySQL连接器的Java-5.1.31槽含有getSchema的发言.jar(目前是MySQL的最新Java连接器驱动程序)。这是输出:

null 
test 

这意味着他们仍然不支持getSchema方法。

相关:

-1

创建一个seprate类并定义getConnection方法static。我想这可能帮助ü

public class ConnectionManager { 
private static String url = "jdbc:mysql://localhost:3306/myDB";  
private static String driverName = "com.mysql.jdbc.Driver"; 
private static String usernsme = "root"; 
private static String pasword = "root"; 
private static Connection con; 
private static String url; 

public static Connection getConnection() { 
    try { 
     Class.forName(driverName); 
     try { 
      con = DriverManager.getConnection(url, username, password); 
     } catch (SQLException ex) { 

      System.out.println("Failed to create the database connection."); 
     } 
    } catch (ClassNotFoundException ex) { 

     System.out.println("Driver not found."); 
    } 
    return con; 
} 
} 

在这里领取代码连接如下:

private Connection con = null; 
private Statement stmt = null; 
private ResultSet rs = null; 
con = ConnectionManager.getConnection(); 
stmt = con.createStatement(); 
rs = stmt.executeQuery(sql); 
+0

我试过了,没有帮助。其次,你能否澄清一下“定义静态类”是什么意思? – Solace

+0

您需要为JDBC连接创建分离类。在我的情况下,我做了ConnectionManager类。并在此类上定义静态方法。和我在这个答案中一样。 – Vishal16

+0

我已经试过了。它没有帮助。但仅仅是为了让我明白,请澄清你为什么建议它?你的解决方案将如何帮助? – Solace

相关问题