Created
December 11, 2014 08:31
-
-
Save honux77/e05877e44176d3b80fc6 to your computer and use it in GitHub Desktop.
jdbc transaction example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.sql.*; | |
| public class Tran { | |
| public static void main(String[] args) throws Exception { | |
| Class.forName("com.mysql.jdbc.Driver"); | |
| String host = "jdbc:mysql://192.168.56.102/popidb"; | |
| String user = "popi"; | |
| String pw = "db1004"; | |
| Connection conn = DriverManager.getConnection(host, user, pw); | |
| conn.setAutoCommit(false); | |
| Statement stmt = conn.createStatement(); | |
| //insert | |
| //stmt.execute("INSERT INTO TEST VALUES (777)"); | |
| //select | |
| String sql = "SELECT * FROM TEST"; | |
| ResultSet rs = stmt.executeQuery(sql); | |
| while (rs.next()) { | |
| int n = rs.getInt(1); | |
| System.out.println(n); | |
| } | |
| System.out.println("Press enter"); | |
| System.in.read(); | |
| rs.close(); | |
| stmt.close(); | |
| //conn.commit(); | |
| conn.rollback(); | |
| conn.close(); | |
| System.out.println("Thank you"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment