Categories
Java

Java: MySQl connection using JDBC driver

Easy few steps to “Hello World JDBC MySQL connection”

Step 1: Requirement

Install MySQL: MySQL can be installed directly by going to MySQL website. Or it can be installed using WAMP or XAMPP. Don’t forget to start WAMP or XAMPP.

Download: mysql-connector-java.jar

Install Java IED: like Eclipse. Using Eclipse in this tutorial.

The code: I have shared it below.

package com.webtechmonk.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class HelloWordlJdbc {
	private static final String QUERY = "select id,name,email,country,password from Users";
	public static void main(String[] args) {
		// TODO Auto-generated method stub

        // using try-with-resources to avoid closing resources (boilerplate code)

        // Step 1: Establishing a Connection
        try (Connection connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/webtechmonk_jdbc?useSSL=false", "root", "");

            // Step 2:Create a statement using connection object
            Statement stmt = connection.createStatement();

            // Step 3: Execute the query or update query
            ResultSet rs = stmt.executeQuery(QUERY)) {

            // Step 4: Process the ResultSet object.
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String email = rs.getString("email");
                String country = rs.getString("country");
                String password = rs.getString("password");
                System.out.println(id + "," + name + "," + email + "," + country + "," + password);
            }
        } catch (SQLException e) {
            printSQLException(e);
        }
        // Step 4: try-with-resource statement will auto close the connection.
	}
	
    public static void printSQLException(SQLException ex) {
        for (Throwable e: ex) {
            if (e instanceof SQLException) {
                e.printStackTrace(System.err);
                System.err.println("SQLState: " + ((SQLException) e).getSQLState());
                System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
                System.err.println("Message: " + e.getMessage());
                Throwable t = ex.getCause();
                while (t != null) {
                    System.out.println("Cause: " + t);
                    t = t.getCause();
                }
            }
        }
    }
	
}

Step 2: Procedure

Go to: http://localhost/phpmyadmin

Create a database: “webtechmonk_jdbc”

and table “Users”

Use the following table structure:

Insert some data into the table, which could be used in the select statement.

Now Open Eclipse, and create a new Java Project.

Name Java Project and Click on Finsh.

Click on scr and create a new package.

Create a new class by clicking on the package name.

Give the class a name, and click on Finsh

Copy the code above in the class file. And save it.

Open the run configuration by clicking on the class name

Click on Java Application and then click on New launch configuration button

Browse the Project name

Then select the project name from the list

Select the main class

Add mysql-connector to the class path, click on Add External JARs

Select the mysql-connector downloaded, click and select and run

The final out is output of select statement from myslq database

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.