Skip to content

Instantly share code, notes, and snippets.

@honux77
Created December 11, 2014 08:31
Show Gist options
  • Select an option

  • Save honux77/e05877e44176d3b80fc6 to your computer and use it in GitHub Desktop.

Select an option

Save honux77/e05877e44176d3b80fc6 to your computer and use it in GitHub Desktop.
jdbc transaction example
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