How To Use JDBC To Connect MySql Database

This article will show you examples about how to use JDBC to connect to mysql server.

1. Download MySQL Server.

  1. If your purpose is just for study, you can download XAMPP for using mysql db, XAMPP includes both Apache, MySql and Php.
    xampp download page
  2. If you want to use mysql server separately, you can go to mysql download page to download and install my sql server.
  3. This tutorial just use XAMPP. After download and install XAMPP, you can start it by click XAMPP Control Panel in system startup menu.
    xampp control panel
  4. In the Control Panel, you should start MySql and Apache server. When you see the green rectangle, the server startup is ready.
    start mysql and apache server in xampp control panel
  5. Click the MySQL Admin button in above picture, there will popup a web browser open the phpAdmin web page. You can manage mysql database in this web based gui.
    phpMyAdmin mysql database manage page
  6. Click database test in left panel. And create a table student with name and email column in right panel.
    create mysql table student

2. Download MySql Server JDBC Driver

  1. Click here go to download mysql jdbc driver page.
    download mysql jdbc driver
  2. Do not need to Sign Up, just click the bottom link to download mysql jdbc jar directly.
    download jdbc driver directly
  3. After download, unzip it to a local folder and add the jar file to your java project.
    add mysql jdbc jar file into java project

3. MySQL JDBC Example Code

The basic JDBC java code for database connection is same, the only different is the database connection url and jdbc driver class name.

/* Register database jdbc driver class. */			
Class.forName("com.mysql.jdbc.Driver");
			
/* Create database connection url. */
String mysqlConnUrl = "jdbc:mysql://localhost:3306/test";
			
/* db user name. */
String mysqlUserName = "root";
			
/* db password. */
String mysqlPassword = "";
			
/* Get the Connection object. */
java.sql.Connection conn = DriverManager.getConnection(mysqlConnUrl, mysqlUserName , mysqlPassword);

The Complete MySQL JDBC Connection java code. You can see java code comments for detail explanation. Please note do not forget close the java.sql.Connection object after use it. If not, the db connection speed will be slower and slower.

package com.dev2qa.java.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

public class MySqlExample {

	public static void main(String[] args) {
		
		try
		{
			MySqlExample mySqlExample = new MySqlExample();
			
			Connection conn = mySqlExample.getMySqlConnection();
			
			/* You can use the connection object to do any insert, delete, query or update action to the mysql server.*/
			
			/* Do not forget to close the database connection after use, this can release the database connection.*/
			conn.close();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* This method return java.sql.Connection object from MySQL server. */
	public Connection getMySqlConnection()
	{
		/* Declare and initialize a sql Connection variable. */
		Connection ret = null;
		
		try
		{
		
			/* Register for jdbc driver class. */
			Class.forName("com.mysql.jdbc.Driver");
			
			/* Create connection url. */
			String mysqlConnUrl = "jdbc:mysql://localhost:3306/test";
			
			/* db user name. */
			String mysqlUserName = "root";
			
			/* db password. */
			String mysqlPassword = "";
			
			/* Get the Connection object. */
			ret = DriverManager.getConnection(mysqlConnUrl, mysqlUserName , mysqlPassword);
			
			/* Get related meta data for this mysql server to verify db connect successfully.. */
			DatabaseMetaData dbmd = ret.getMetaData();
			
			String dbName = dbmd.getDatabaseProductName();
			
			String dbVersion = dbmd.getDatabaseProductVersion();
			
			String dbUrl = dbmd.getURL();
			
			String userName = dbmd.getUserName();
			
			String driverName = dbmd.getDriverName();
			
			System.out.println("Database Name is " + dbName);
			
			System.out.println("Database Version is " + dbVersion);
			
			System.out.println("Database Connection Url is " + dbUrl);
			
			System.out.println("Database User Name is " + userName);
			
			System.out.println("Database Driver Name is " + driverName);
						
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			return ret;
		}
	}

}

Run above code, you can see below output in java console.

Database Name is MySQL
Database Version is 5.5.5-10.1.24-MariaDB
Database Connection Url is jdbc:mysql://localhost:3306/test
Database User Name is [email protected]
Database Driver Name is MySQL Connector Java

Source Code :

Download “MySqlConnectionExample.zip” MySqlConnectionExample.zip – Downloaded 360 times – 938 B

(Visited 5,520 times, 28 visits today)
READ :   How To Install MySQL On Ubuntu

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.