Home Tutorial Java Jdbc Concurrency in JDBC

 
 

Share on Google+Share on Google+
Concurrency in JDBC
Posted on: October 15, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how to use concurrency in JDBC Transaction, ResultSet etc.
JDBC Concurrency

JDBC Concurrency

Many database applications allows multiple users to access data  from database at the same time. If you don't implement concurrency then the performance of your application degrades rapidly. Therefore it is very important to implement the concurrency, You can implement the concurrency in many operation, such as Transaction processing, Connection, Result Set etc. This helps a lot to improve the performance of your application.

At first create table named student in MySql database and inset values into it as.

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
 );

JDBCResultSetConcurrency.java

package roseindia.net;

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

public class JDBCResultSetConcurrency {
	private String driverName = "com.mysql.jdbc.Driver";
	private String connectionURL = "jdbc:mysql://192.168.10.13:3306/student";

	public JDBCResultSetConcurrency() {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}

	public Connection createcoConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(connectionURL, "root", "root");
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return conn;
	}

	public static void main(String srgs[]) throws SQLException {
		JDBCResultSetConcurrency newConnection = new JDBCResultSetConcurrency();
		Statement stmt = newConnection.createcoConnection().createStatement();
		String query = "SELECT * FROM student";
		ResultSet rs = stmt.executeQuery(query);
		int concurrency = rs.getConcurrency();
		System.out.println("ResultSet Concurrency is " + concurrency);
		while (rs.next()) {
			System.out.println("Roll No- " + rs.getInt("RollNo") + ", Name- "
					+ rs.getString("Name") + ", Course- "
					+ rs.getString("course") + ", Address- "
					+ rs.getString("Address"));
		}
	}
}

When you run this application it will display message as shown below:


ResultSet Concurrency is 1007
Roll No- 1, Name- Vinay, Course- MCA, Address- Motihari
Roll No- 2, Name- Ram, Course- BCA, Address- Patna

Download this example code

JDBCTransactionConcurrencyExample.java

package roseindia.net;

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

public class JDBCTransactionConcurrencyExample {
	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String driverName = "com.mysql.jdbc.Driver";
		String connectionUrl = "jdbc:mysql://192.168.10.13:3306/";
		String databaseName = "student";
		String userName = "root";
		String password = "root";
		try {
			// Loading Driver
			Class.forName(driverName);
			// Creating Connection
			conn = DriverManager.getConnection(connectionUrl + databaseName,
					userName, password);
			// Setting auto commit false
			conn.setAutoCommit(false);
			System.out.println("Databese Connection Done........");
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
			System.out.println("Error In Connection");
			System.exit(0);
		}
		try {
			// Creating Statement
			stmt = conn.createStatement();
			// Creating Query String
			String updateQuery1 = "INSERT INTO student VALUES(1,'Vinay','MCA','Motihari')";
			String updateQuery2 = "INSERT INTO student VALUES(2,'Ram','BCA','Patna')";
			String selectQuery = "SELECT * FROM student";
			stmt.executeUpdate(updateQuery1);
			stmt.executeUpdate(updateQuery2);
			rs = stmt.executeQuery(selectQuery);
			DatabaseMetaData dbMetaData = conn.getMetaData();
			if (dbMetaData.supportsTransactionIsolationLevel(8)) {
				conn.setTransactionIsolation(8);
				conn.commit();
			}
			// Calling commit() method

			while (rs.next()) {
				System.out.println("Roll No.- " + rs.getInt("RollNo")
						+ ", Name- " + rs.getString("Name") + ", Course- "
						+ rs.getString("Course") + ", Address- "
						+ rs.getString("Address"));
			}
		} catch (Exception e) {
			System.out.println(e.toString());
			System.exit(0);
		} finally {
			// Closing Connection
			conn.close();
			stmt.close();
			rs.close();
			System.out.println("Connection Closed..........");
		}
	}
}

When you run this application it will display message as shown below:


Databese Connection Done........
Roll No.- 1, Name- Vinay, Course- MCA, Address- Motihari
Roll No.- 2, Name- Ram, Course- BCA, Address- Patna
Connection Closed..........

Download this example code

Advertisement

Related Tags for Concurrency in JDBC:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com