简体   繁体   中英

How to connect to mysql using java?

i'm want to store emails from Gmail into my mysql database. i found Inboxreader with google but the part for connection to mysql is not working. the username, database name, password is correct.

can anyone help me. Thank you.

here is the part of the code

{
            Properties details= new Properties();
            details.load(new FileInputStream("details.properties"));
            String userName = details.getProperty("root");
            String password = details.getProperty("password");
            String url = details.getProperty("jdbc:mysql://localhost/test");
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
            PreparedStatement st= conn.prepareStatement("insert into 'Email_list' values(?)");
            for(String mail:mails)
            {
            try{
                  st.setString(1, mail);
                  st.execute();
                }catch(Exception e){}
            }



        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
            e.printStackTrace();
        }
        finally

Here is the error code:

Cannot connect to database server
java.sql.SQLException: The url cannot be null
Reading:23
    at java.sql.DriverManager.getConnection(DriverManager.java:554)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at inboxreader.InboxReader.connecttoMySql(InboxReader.java:181)
    at inboxreader.InboxReader.Start(InboxReader.java:82)
    at inboxreader.InboxReader.main(InboxReader.java:34)

Thank you

This is your problem:

String url = details.getProperty("jdbc:mysql://localhost/test");

You are getting a null value in url . That's because there is no property called jdbc:mysql://localhost/test in your properties file.

You have two options. One would be using url directly with something like:

String url = "jdbc:mysql://localhost/test";

The other option would be having a correctly set up property in details.properties :

# hello, I am details.properties file
jdbc.url=jdbc:mysql://localhost/test

Then, in your Java code you would read url from property like this:

String url = details.getProperty("jdbc.url"); // note that we're changing property name

You're trying to get the value of a property from details like this:

String url = details.getProperty("jdbc:mysql://localhost/test");

It seems to me that the name of the property there is actually your value.

Thats because you don't have one key "jdbc:mysql://localhost/test" in your properties file. Let's say that details.properties have this content:

url=jdbc:mysql://localhost/test

So your code should be

String url = details.getProperty("url");

I am sure that your property key ha issue :

 String url = details.getProperty("jdbc:mysql://localhost/test");

You should first validate that weather you have correct key or not

 if (details.getProperty("jdbc:mysql://localhost/test") != null || 
    details.getProperty("jdbc:mysql://localhost/test").trim().length > 0){
       url =details.getProperty("jdbc:mysql://localhost/test");
 }else{
 return new Exception("Wrong property key");
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM