How to run SQL Queries simultaneous in java and get response.
The will show you how to run SQL query parallel using java thread and give response once all finished its execution.
By using below Two java programs .For Reference please see below sql dump. I have used MySQL.
Below link contains all File
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author Bala2e
*
*/
public class ThreadWithSQL {
public void runAllThreads() {
final Connection con = this.getConnection();
final SQLQuery sqlQuery = new SQLQuery();
Thread firstQueryThread = new Thread(new Runnable() {
public void run() {
sqlQuery.getEmployeeDetails(con);
}
});
Thread secondQueryThread = new Thread(new Runnable() {
public void run() {
sqlQuery.getSalesmanDetails(con);
}
});
Thread thridQueryThread = new Thread(new Runnable() {
public void run() {
sqlQuery.getClientDetails(con);
}
});
firstQueryThread.start();
secondQueryThread.start();
thridQueryThread.start();
try {
firstQueryThread.join();
secondQueryThread.join();
thridQueryThread.join();
} catch (Exception e) {
e.printStackTrace();
}
ResultSet rsEmp = sqlQuery.rsEmp;
ResultSet rsSales = sqlQuery.rsSales;
ResultSet rsClient = sqlQuery.rsclient;
try {
System.out.println("---------------- Emp Details----------------------");
while (rsEmp != null && rsEmp.next()) {
System.out.println("id-" + rsEmp.getString(1) + " name- "+ rsEmp.getString(2));
}
System.out.println("---------------- Salesman Details----------------------");
while (rsSales != null && rsSales.next()) {
System.out.println("id-" + rsSales.getString(1) + " name- "+ rsSales.getString(2));
}
System.out.println("---------------- Client Details----------------------");
while (rsClient != null && rsClient.next()) {
System.out.println("id-" + rsClient.getString(1) + " name- "+ rsClient.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
ThreadWithSQL threadWithSQL = new ThreadWithSQL();
threadWithSQL.runAllThreads();
// threadWithSQL.runAllThreads();
}
}
Second Program
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author Bala2e
*
*/
public class SQLQuery {
ResultSet rsEmp, rsSales, rsclient;
public void getEmployeeDetails(Connection con) {
try {
Statement stmt = con.createStatement();
rsEmp = stmt.executeQuery("select e.emp_id,e.emp_name from test.employee e");
} catch (Exception e) {
e.printStackTrace();
}
}
public void getSalesmanDetails(Connection con) {
try {
Statement stmt = con.createStatement();
rsSales = stmt.executeQuery("select sm.sales_man_id,sm.name from test.sales_man sm");
} catch (Exception e) {
e.printStackTrace();
}
}
public void getClientDetails(Connection con) {
try {
Statement stmt = con.createStatement();
rsclient = stmt.executeQuery("select c.client_id, c.client_name from test.client c");
} catch (Exception e) {
e.printStackTrace();
}
}
Following is the SQL Dump.
CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `test`;
-- MySQL dump 10.13 Distrib 5.6.17, for Win64 (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.6.21-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `client`
--
DROP TABLE IF EXISTS `client`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `client` (
`client_id` int(11) NOT NULL,
`client_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `client`
--
LOCK TABLES `client` WRITE;
/*!40000 ALTER TABLE `client` DISABLE KEYS */;
INSERT INTO `client` VALUES (1,'murugan store'),(2,'kumar store');
/*!40000 ALTER TABLE `client` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `employee`
--
DROP TABLE IF EXISTS `employee`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `employee` (
`emp_id` int(11) NOT NULL,
`emp_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `employee`
--
LOCK TABLES `employee` WRITE;
/*!40000 ALTER TABLE `employee` DISABLE KEYS */;
INSERT INTO `employee` VALUES (1,'Arun'),(2,'Bala');
/*!40000 ALTER TABLE `employee` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `master_user_details`
--
DROP TABLE IF EXISTS `master_user_details`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `master_user_details` (
`user_detail_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` varchar(255) DEFAULT NULL,
`mobile_no` varchar(255) DEFAULT NULL,
PRIMARY KEY (`user_detail_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `master_user_details`
--
LOCK TABLES `master_user_details` WRITE;
/*!40000 ALTER TABLE `master_user_details` DISABLE KEYS */;
INSERT INTO `master_user_details` VALUES (1,'B.Balamurugan','27','9042221'),(2,'BALA','27','56565656'),(3,'cc','cccv','vv');
/*!40000 ALTER TABLE `master_user_details` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `sales_man`
--
DROP TABLE IF EXISTS `sales_man`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `sales_man` (
`sales_man_id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`sales_man_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `sales_man`
--
LOCK TABLES `sales_man` WRITE;
/*!40000 ALTER TABLE `sales_man` DISABLE KEYS */;
INSERT INTO `sales_man` VALUES (1,'ajay'),(2,'pinkku');
/*!40000 ALTER TABLE `sales_man` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `user_details`
--
DROP TABLE IF EXISTS `user_details`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user_details` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`role_id` varchar(255) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `user_details`
--
LOCK TABLES `user_details` WRITE;
/*!40000 ALTER TABLE `user_details` DISABLE KEYS */;
INSERT INTO `user_details` VALUES (1,'bala','bala','1');
/*!40000 ALTER TABLE `user_details` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2015-07-14 23:56:07