Connecting to MySQL With Java Program

posted on Nov 20th, 2016

MySQL

MySQL is an open-source relational database management system (RDBMS). It's name is a combination of "My", the name of co-founder Michael Widenius' daughter, and "SQL", the abbreviation for Structured Query Language.

The MySQL development project has made its source code available under the terms of the GNU General Public License, as well as under a variety of proprietary agreements. MySQL was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now owned by Oracle Corporation.

Pre Requirements

1) A machine with Ubuntu 14.04 LTS operating system

2) MySQL Server installed (How to install MySQL in Ubuntu)

3) mysql-connector-java.jar file

Connecting to MySQL with Java Example

The following steps to connect to MySQL Server with Java Client.

Steps to follow

Step 1 - Enter into MySQL command line inteface(CLI). Open a terminal (CTRL + ALT + T) and type the following command.

$ mysql -u root -p

Enter password

Enter password :****

Step 2 - Create a new database. Here 'userdb' is the new database name.

mysql> create database userdb;

Step 3 - Use 'userdb' database for creating tables.

mysql> use userdb;

Step 4 - Create a new table. Here 'user' is a new table name.

mysql> create table user(id int, name varchar(20));

Step 5 - Add mysql-connector-java.jar to your java project build path. You can find this jar file in /usr/share/java/ folder. If you didn't find, please follow the steps here

Step 6 - Write a Java program for connecting to MySQL database server. Below JdbcConnection.java program inserts the values in user table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConnection {

	Connection con = null;
	Statement st = null;
	ResultSet rs = null;
	String name = null;
	PreparedStatement pst = null;

	public JdbcConnection() {

		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/userdb", "root", "root");
			st = con.createStatement();
			int i = st.executeUpdate("insert into user values(1,'praveen')");
			if (i == 1) {
				System.out.println("inserted successfully");
			} else {
				System.out.println("error while inserting");

			}
		} catch (Exception e) {
			e.toString();
		} finally {
			try {
				if (st != null)
					st.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
	}

	public static void main(String a[]) {
		new JdbcConnection();
	}
}

You can check it in MySQL CLI, whether the values are inserted or not.

mysql> select * from user;
+------+---------+
| id   | name    |
+------+---------+
|    1 | praveen |
+------+---------+
1 row in set (0.00 sec)

Please share this blog post and follow me for latest updates on

facebook             google+             twitter             feedburner

Previous Post                                                                                          Next Post

Labels : MySQL Installation on Ubuntu   Creating New User in MySQL