5

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

4 Answers 4

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

@Pacific - there's also no reason to tie your Java code to MySQL. Since you already have details.properties put in something like jdbc.url=jdbc:mysql://etc and jdbc.driver=com.mysql.jdbc.Driver then you can do Class.forName(details.getProperty("jdbc.driver"))
2

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.

1 Comment

Thank you very much Sir Pablo, Sergio and fivedigit
2

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");

Comments

0

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

2 Comments

Now my code is working but i made a mistake i know the database name is test, the table name is Email_list but when i go to mysql the table is Email_list is empty.
"when i go to mysql the table is Email_list is empty" means when you are using the mysql ide? or when you establish the connection?,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.