0

I am studying Java and I have an error.

I want to work with the mysql database

It's a code.

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

I've been looking for a lot of stackoverflow articles to solve the problem,

but I have not been able to fix it.

This is the code in the db.prop file.

url=jdbc:mysql://localhost/java

driver=com.mysql.jdbc.Driver

id=java

password=*******

It's the error code that Eclipse shows.

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)

This is the 24th line code in SqlInsertTest.java.

conn = DriverManager.getConnection(url,id,password);
Helll
  • 1
  • 1
  • 4
  • Have you checked if id and password are loading fine into the program from the `db.prop` file? – yaswanth Apr 02 '18 at 05:55
  • yes. id and password, driver works fine. – Helll Apr 02 '18 at 06:37
  • It could be a typo. You could have put the number `1` in place of the letter `l` at some place. So instead of `url` it could be `ur1` at some place in your code. Can you just double check if that's the case? – yaswanth Apr 02 '18 at 06:58
  • 2
    did you inspect the value of `url` after this line `String url = prop.getProperty("url");` – Arun Sudhakaran Apr 02 '18 at 07:14
  • I solved the problem. It's because of you. Progman! Thank you so much Thanks to all those who answered. – Helll Apr 02 '18 at 14:16

1 Answers1

0

try this...https://stackoverflow.com/a/9494755/8311225... I think db.prop extension would be ".properties" not ".prop" ..

sk sakil
  • 186
  • 1
  • 12
  • I changed the extension to properties and checked the link, but I have not resolved it yet. – Helll Apr 02 '18 at 06:42