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
DriverManager.getConnection returns null
提问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:3306
should be followed by the database name.
您的网址不完整。本jdbc:mysql://localhost:3306
应遵循的数据库名。
Trouble shoot steps:
故障排除步骤:
- Check the port
- Check if the db server is turned on
- 检查端口
- 检查数据库服务器是否开启
回答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 3
then you need to load the driver like
如果您正在使用,JDBC 3
那么您需要像这样加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
If you are working with JDBC 4
then 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 jar
is in classpath.
最后确保它mysql driver jar
在类路径中。