【发布时间】:2018-09-11 08:34:25
【问题描述】:
我正在学习 Java,但我有一个错误。
我想使用 mysql 数据库
这是一个代码。
CarDAO.java
package jv16;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
//DAO : 데이터 처리 객체
public class CarDAO {
public Connection dbconn(){
Connection conn = null;
try {
FileInputStream fis = new FileInputStream("d:\\db.prop");
Properties prop = new Properties();
prop.load(fis);
String url = prop.getProperty("url");
String id = prop.getProperty("id");
String password = prop.getProperty("password");
conn = DriverManager.getConnection(url,id,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
找了很多stackoverflow的文章来解决问题,
但我无法修复它。
这是 db.prop 文件中的代码。
url=jdbc:mysql://localhost/java
driver=com.mysql.jdbc.Driver
id=java
password=*******
这是 Eclipse 显示的错误代码。
com.mysql.jdbc.Driver
null
java
********
java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(DriverManager.java:649)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at jv16.SqlInsertTest.main(SqlInsertTest.java:24)
这是 SqlInsertTest.java 中的第 24 行代码。
conn = DriverManager.getConnection(url,id,password);
【问题讨论】:
-
你检查过
db.prop文件中的id和密码是否正常加载到程序中吗? -
是的。 id和密码,驱动工作正常。
-
可能是错字。您可以在某个地方用数字
1代替字母l。因此,在您的代码中的某个位置,它可能不是url,而是ur1。你能仔细检查一下是不是这样吗? -
你在
String url = prop.getProperty("url");这一行之后检查了url的值 -
我解决了这个问题。是因为你。前卫!非常感谢所有回答的人。