Statement: executeBatch()
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Connection con;
Statement stmt;
ResultSet rs;
try {
Class.forName(driver);
con = DriverManager.getConnection("jdbc:odbc:databaseName", "student", "student");
// Start a transaction
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.addBatch("UPDATE EMP SET JOB = 1");
// Submit the batch of commands for this statement to the database
stmt.executeBatch();
// Commit the transaction
con.commit();
// Close the existing to be safe before opening a new one
stmt.close();
// Print out the Employees
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM EMP");
// Loop through and print the employee number, job, and hiredate
while (rs.next()) {
int id = rs.getInt("EMPNO");
int job = rs.getInt("JOB");
String hireDate = rs.getString("HIREDATE");
System.out.println(id + ":" + job + ":" + hireDate);
}
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Related examples in the same category