In this tutorial you will learn how to use concurrency in JDBC Transaction, ResultSet etc.
In this tutorial you will learn how to use concurrency in JDBC Transaction, ResultSet etc.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")); } } }
ResultSet Concurrency is 1007 Roll No- 1, Name- Vinay, Course- MCA, Address- Motihari Roll No- 2, Name- Ram, Course- BCA, Address- Patna |
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.........."); } } }
Databese Connection Done........ Roll No.- 1, Name- Vinay, Course- MCA, Address- Motihari Roll No.- 2, Name- Ram, Course- BCA, Address- Patna Connection Closed.......... |