Batch Processing in JDBC - Notes Lyst8846
Batch Processing in JDBC - Notes Lyst8846
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver successfully
loaded");
con=DriverManager.getConnection(url, un, pwd);
System.out.println("Connection established");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,0);
String query="select * from emp";
res = stmt.executeQuery(query);
System.out.println(metaData.getColumnName(i)+"
"+metaData.getColumnTypeName(i));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
try {
res.close();
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver successfully loaded");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,0);
System.out.println(stmt.execute(query));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,0);
stmt.addBatch(query);
stmt.addBatch(query1);
stmt.addBatch(query2);
stmt.executeBatch();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Prepared Statement:
In real-time, the data that is entered in the front-end should be extracted by a java program
and java should insert the query into the database.
Now we don't have front end interaction using scanner we will take input and we will set the
values as shown below:
pstmt = con.prepareStatement(query);
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Demoj {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver successfully loaded");
pstmt = con.prepareStatement(query);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
try {
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}