Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to mysql using java?

Tags:

java

mysql

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

like image 285
Pacific Avatar asked Feb 01 '12 19:02

Pacific


People also ask

How do I connect to MySQL database?

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.

What is Java Connector MySQL?

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.


2 Answers

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
like image 72
Pablo Santa Cruz Avatar answered Oct 13 '22 05:10

Pablo Santa Cruz


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.

like image 43
fivedigit Avatar answered Oct 13 '22 05:10

fivedigit