java DriverManager.getConnection 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26024939/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:09:30  来源:igfitidea点击:

DriverManager.getConnection returns null

javamysqljdbc

提问by CtrlAltDolev

I am trying to use a Java class that already worked great for me in the past in order to access MySQL from my JSP project, running Tomcat 6. The problem is, that this line:

我正在尝试使用过去对我来说已经很好用的 Java 类,以便从运行 Tomcat 6 的 JSP 项目访问 MySQL。问题是,这一行:

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "password");

gives con a value of null.

给 con 一个 null 值。

Now, my question is - what can cause the "getConnection" method return null?

现在,我的问题是 - 什么会导致“getConnection”方法返回 null?

Edit: I see now that also, I get the infamous ClassNotFoundException: com.mysql.jdbc.Driver. I have the connector included in my build path. So why am I getting this error? And, does it have to do with getConnection returning null?

编辑:我现在也看到了,我得到了臭名昭著的 ClassNotFoundException:com.mysql.jdbc.Driver。我的构建路径中包含了连接器。那么为什么我会收到这个错误呢?而且,它与 getConnection 返回 null 有关系吗?

回答by Jilles van Gurp

You need to initialize the driver before you can get a connection from the DriverManager.

您需要先初始化驱动程序,然后才能从 DriverManager 获得连接。

Class.forName("com.mysql.jdbc.Driver"); 

回答by Zeus

Your url is incomplete. The jdbc:mysql://localhost:3306should be followed by the database name.

您的网址不完整。本jdbc:mysql://localhost:3306应遵循的数据库名。

Trouble shoot steps:

故障排除步骤:

  1. Check the port
  2. Check if the db server is turned on
  1. 检查端口
  2. 检查数据库服务器是否开启

回答by Rika

According to this article(which has a good example) http://www.vogella.com/tutorials/MySQLJava/article.html,

根据这篇文章(有一个很好的例子)http://www.vogella.com/tutorials/MySQLJava/article.html

"con" will be null if the getConnection throws an exception and is caught in your code.

如果 getConnection 抛出异常并在您的代码中被捕获,则“con”将为空。

Like:

喜欢:

Connection con = null;
 try{ 
con = DriveManager.getConnection("Not right url");   

}catch(SQLException sqlE){};
System.out.println(con); //it will be null

you need to do

你需要做

   connect = DriverManager.getConnection("jdbc:mysql://localhost/feedback?"
          + "user=sqluser&password=sqluserpw");

So maybe have "user = root&password = password"

所以也许有“user = root&password = password”

Also if there is ClassNotFoundException most likely you need a jar file

此外,如果有 ClassNotFoundException 很可能你需要一个 jar 文件

DriverManager API: http://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)

DriverManager API:http: //docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)

回答by sol4me

If you are working with JDBC 3then you need to load the driver like

如果您正在使用,JDBC 3那么您需要像这样加载驱动程序

Class.forName("com.mysql.jdbc.Driver"); 

If you are working with JDBC 4then you are no longer required to manually load the JdbcDriver.

如果您正在使用,JDBC 4则不再需要手动加载 JdbcDriver。

And then pass valid connection_urlto getConnection

然后将有效的connection_url传递给getConnection

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDbName", "root", "password");

Lastly make sure that mysql driver jaris in classpath.

最后确保它mysql driver jar在类路径中。