【问题标题】:java.sql.SQLException: The url cannot be null [duplicate]java.sql.SQLException:网址不能为空[重复]
【发布时间】: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的值
  • 我解决了这个问题。是因为你。前卫!非常感谢所有回答的人。

标签: java mysql


【解决方案1】:

试试这个...https://stackoverflow.com/a/9494755/8311225... 我认为 db.prop 扩展名将是“.properties”而不是“.prop”..

【讨论】:

  • 我将扩展名改为属性并检查了链接,但我还没有解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2014-07-07
  • 2015-10-08
  • 2016-06-26
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多