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
To Connect to a MySQL Database Expand the Drivers node from the Database Explorer. Right-click the MySQL (Connector/J driver) and choose Connect Using.... The New Database Connection dialog box is displayed. In the Basic Setting tab, enter the Database's URL <HOST>:<PORT>/<DB> in the corresponding text field.
MySQL ConnectorMySQL ConnectorMySQL Connectors provide connectivity to the MySQL server for client programs. APIs provide low-level access to MySQL resources using either the classic MySQL protocol or X Protocol.https://dev.mysql.com › doc › connectors › connectors-apisConnectors and APIs Manual :: 1 Introduction - MySQL/J is a JDBC Type 4 driver, implementing the JDBC 4.2 specification. The Type 4 designation means that the driver is a pure Java implementation of the MySQL protocol and does not rely on the MySQL client libraries.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With