crysispeed
crysispeed

Reputation: 45

Access Wamp Server Database from Eclipse

I want to access my Wamp Server database which is in my computer with using Java. My code is below:

public static void main(String[] args)
    {
        try
        {
            // create our mysql database connection
            String myDriver = "com.mysql.jdbc.Driver";
            String myUrl = "jdbc:mysql://localhost:3306/deneme";
            Class.forName(myDriver);
            Connection conn = DriverManager.getConnection(myUrl, "root", "");

            // our SQL SELECT query. 
            // if you only need a few columns, specify them by name instead of using "*"
            String query = "SELECT * FROM users";

            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(query);

            // iterate through the java resultset
            while (rs.next())
            {
                int id = rs.getInt("id");
                String firstName = rs.getString("first_name");
                String lastName = rs.getString("last_name");
                Date dateCreated = rs.getDate("date_created");
                boolean isAdmin = rs.getBoolean("is_admin");
                int numPoints = rs.getInt("num_points");

                // print the results
                System.out.format("%s, %s, %s, %s, %s, %s\n", id, firstName, lastName, dateCreated, isAdmin, numPoints);
            }
            st.close();
        }
        catch (Exception e)
        {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }

After this, it gives me this error:

Got an exception! 
com.mysql.jdbc.Driver

How can I connect my database in my own computer to java?

Thanks.

Upvotes: 1

Views: 5100

Answers (1)

Abdelhak
Abdelhak

Reputation: 8387

Try to add the mysql-connector-java-xxxx-bin.jar in the /lib folder.
You can download it from here.
And right click on project properties --> Java Build Path --> External jar

Upvotes: 2

Related Questions