English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following is an example that usescommitandrollbackIt is described in the JDBC transaction tutorial.
This example code is written based on the environment and database settings in the previous chapters.
Copy and paste the following example into JDBCExample.java, and compile and run as follows:
//Step1.Import the required software packages import java.sql.*; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //Step2Register the JDBC driver Class.forName("com.mysql.jdbc.Driver"); //Step3Establish connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //Step4Set automatic commit to false. conn.setAutoCommit(false); //Step5Execute the query to create the statement // The necessary parameter for RS example. System.out.println("Creating statement..."); stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //Step6Insert a row into the 'Employees' table. System.out.println("Inserting one row...."); String SQL = "INSERT INTO Employees " + "VALUES (106, 20, 'Rita', 'Tez')"; stmt.executeUpdate(SQL); //Step7Insert another row into the 'Employees' table. SQL = "INSERT INTO Employees " + "VALUES (107, 22, 'Sita', 'Singh')"; stmt.executeUpdate(SQL); //Step8Submit data here. System.out.println("Commiting data here...."); conn.commit(); //Step9:Now list all available records. String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); System.out.println("List result set for reference...."); printRs(rs); //Step10:Cleanup environment rs.close(); catch(SQLException se catch(SQLException se){ } //Handle JDBC error se.printStackTrace(); // If there is an error, rollback the changes. System.out.println("Rolling back data here...."); try{ if(conn!=null) conn.rollback(); }2){ se2.printStackTrace(); }//End attempt } //Handle Class.forName error e.printStackTrace(); } //To close resources try{ stmt.close(); catch(SQLException se }2){ } try{ if(conn!=null) catch(SQLException se){ } se.printStackTrace(); } } System.out.println("Goodbye!"); }//End main public static void printRs(ResultSet rs) throws SQLException{ //Ensure we start from the first row rs.beforeFirst(); while(rs.next()){ //Search by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display value System.out.print("ID: ", + id); System.out.print(", Age: ", + age); System.out.print( + System.out.println( + last); } System.out.println(); }//End of printRs() }//End of JDBCExample
Now let's compile the above example as follows:
C:\>javac JDBCExample.java C:\>
At runtimeJDBCExampleIt will produce the following result-
C:\>java JDBCExample Connecting to database... Creating statement... Inserting one row.... Commiting data here.... List result set for reference.... ID: 10, Age: 18, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 3, First: Zaid, Last: Khan ID: 103, Age: 28, First: Sumit, Last: Mittal ID: 106, Age: 2, First: Rita, Last: Tez ID: 107, Age: 22, First: Sita, Last: Singh Goodbye! C:\>