Skip to content

Instantly share code, notes, and snippets.

@mehmetbebek
Last active August 29, 2015 13:57
Show Gist options
  • Save mehmetbebek/9913725 to your computer and use it in GitHub Desktop.
Save mehmetbebek/9913725 to your computer and use it in GitHub Desktop.
Insert Batch Recods(More than One Record At A Time) On Oracle via Java
//If your database is mysql then you can use these codes
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work_training", "root", "");
Statement st = conn.createStatement();
String query = "insert into SALES values (?,?, ?, ?,?, ? ,?)";
PreparedStatement pStatement = conn.prepareStatement(query);
int[] products = new int[]{100, 200, 300, 400};
int[] salesman = new int[]{10, 20, 30, 40};
int[] years = new int[]{2010, 2011, 2012, 2013};
int[] regions = new int[]{10, 20, 30};
Random r = new Random();
long startTime = System.currentTimeMillis();
for (int count = 0; count < 1000; count++) {
pStatement.setString(1, Integer.toString(count));
pStatement.setString(2,
Integer.toString(products[(int) (Math.random() * 4)]));
pStatement.setString(3,
Integer.toString(salesman[(int) (Math.random() * 4)]));
pStatement.setString(4,
Integer.toString(years[(int) (Math.random() * 4)]));
pStatement.setString(5,
Integer.toString((int) (Math.random() * 40 + 1)));
pStatement.setString(6,
Integer.toString((int) (Math.random() * 10 + 1) * 100));
pStatement.setString(7,
Integer.toString(regions[(int) (Math.random() * 3)]));
pStatement.executeUpdate();
}
// conn.commit();
//here is so important if you uncomment this you can get this error
//java.sql.SQLException: Can't call commit when autocommit=true
pStatement.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
Logger.getLogger(Sqlbatchinsert.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Sqlbatchinsert.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@XXXX",
"sys as sysdba", "sys");
Statement st = conn.createStatement();
String query = "insert into SALES values (?,?, ?, ?,?, ? ,?)";
PreparedStatement pStatement = conn.prepareStatement(query);
int[] products = new int[] { 100, 200, 300, 400 };
int[] salesman = new int[] { 10, 20, 30, 40 };
int[] years = new int[] { 2010, 2011, 2012, 2013 };
int[] regions = new int[] { 10, 20, 30 };
Random r = new Random();
long startTime = System.currentTimeMillis();
for (int count = 0; count < 1000; count++) {
pStatement.setString(1, Integer.toString(count));
pStatement.setString(2,
Integer.toString(products[(int) (Math.random() * 4)]));
pStatement.setString(3,
Integer.toString(salesman[(int) (Math.random() * 4)]));
pStatement.setString(4,
Integer.toString(years[(int) (Math.random() * 4)]));
pStatement.setString(5,
Integer.toString((int) (Math.random() * 40 +1)));
pStatement.setString(6,
Integer.toString((int) (Math.random() * 10 + 1) * 100));
pStatement.setString(7,
Integer.toString(regions[(int) (Math.random() * 3)]));
pStatement.executeUpdate();
}
conn.commit();
pStatement.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment