List of usage examples for java.sql Connection commit
void commit() throws SQLException;
Connection
object. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false); // start a transaction Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')"); conn.rollback(); // the preceding inserts will not commit; st.executeUpdate("INSERT INTO survey(id, name) VALUES('33', 'jeff')"); conn.commit(); // end the transaction st = conn.createStatement();/* w ww . ja v a 2 s .co m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection connection = null; try {/* w w w .j a va2s. c om*/ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID", "PASSWORD"); connection.setAutoCommit(false); Statement statement = connection.createStatement(); statement.executeUpdate("UPDATE Table1 SET Value = 1 WHERE Name = 'foo'"); statement.executeUpdate("UPDATE Table2 SET Value = 2 WHERE Name = 'bar'"); connection.commit(); } catch (SQLException ex) { connection.rollback(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; Connection con; Statement stmt;/*from ww w .j a v a 2 s. c om*/ 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(); } }
From source file:net.ontopia.persistence.rdbms.DDLExecuter.java
public static void main(String[] argv) throws Exception { // Initialize logging CmdlineUtils.initializeLogging();// w w w . ja v a 2 s. c o m // Initialize command line option parser and listeners CmdlineOptions options = new CmdlineOptions("DDLExecuter", argv); // Register logging options CmdlineUtils.registerLoggingOptions(options); // Parse command line options try { options.parse(); } catch (CmdlineOptions.OptionsException e) { System.err.println("Error: " + e.getMessage()); System.exit(1); } // Get command line arguments String[] args = options.getArguments(); if (args.length < 2) { System.err.println("Error: wrong number of arguments."); usage(); System.exit(1); } String schema = args[0]; String dbprops = args[1]; String action = args[2]; if (!("create".equals(action) || "drop".equals(action) || "recreate".equals(action))) { System.err.println("Error: unknown action: " + action); usage(); System.exit(1); } // Load property file Properties props = new Properties(); props.load(new FileInputStream(dbprops)); // Get database properties from property file String[] platforms = StringUtils.split(props.getProperty("net.ontopia.topicmaps.impl.rdbms.Platforms"), ","); Project project = DatabaseProjectReader.loadProject(schema); //! //! if (dbtype.equals("generic")) //! //! producer = new GenericSQLProducer(project, StringUtils.split(dbtype, ",")); //! //! else //! if (dbtype.equals("mysql")) //! producer = new MySqlSQLProducer(project); //! else if (dbtype.equals("oracle")) //! producer = new OracleSQLProducer(project); //! else { //! producer = new GenericSQLProducer(project, StringUtils.split(dbtype, ",")); //! //! System.err.println("Error: unknown database type: " + dbtype); //! //! usage(); //! //! System.exit(1); //! } // Create SQL producer GenericSQLProducer producer = getSQLProducer(project, platforms); log.debug("Using SQL producer: " + producer); // Create database connection DefaultConnectionFactory cfactory = new DefaultConnectionFactory(props, false); Connection conn = cfactory.requestConnection(); // Execute statements try { if ("create".equals(action)) producer.executeCreate(conn); else if ("drop".equals(action)) producer.executeDrop(conn); else if ("recreate".equals(action)) { producer.executeDrop(conn); producer.executeCreate(conn); } conn.commit(); } finally { if (conn != null) conn.close(); } }
From source file:TestBatchUpdate.java
public static void main(String args[]) { Connection conn = null; Statement stmt = null;//from www. ja v a 2 s . c o m ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); conn.setAutoCommit(false); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('11', 'A')"); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('22', 'B')"); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('33', 'C')"); int[] updateCounts = stmt.executeBatch(); conn.commit(); rs = stmt.executeQuery("SELECT * FROM batch_table"); while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("name"); System.out.println("id=" + id + " name=" + name); } } catch (BatchUpdateException b) { System.err.println("SQLException: " + b.getMessage()); System.err.println("SQLState: " + b.getSQLState()); System.err.println("Message: " + b.getMessage()); System.err.println("Vendor error code: " + b.getErrorCode()); System.err.print("Update counts: "); int[] updateCounts = b.getUpdateCounts(); for (int i = 0; i < updateCounts.length; i++) { System.err.print(updateCounts[i] + " "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor error code: " + ex.getErrorCode()); } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception ignore) { } } }
From source file:InsertPictureToMySql.java
public static void main(String[] args) throws Exception, IOException, SQLException { Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; FileInputStream fis = null;//from w ww . ja v a 2 s .c o m PreparedStatement ps = null; try { conn.setAutoCommit(false); File file = new File("myPhoto.png"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setString(1, "001"); ps.setString(2, "name"); ps.setBinaryStream(3, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally { ps.close(); fis.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); con.setAutoCommit(false);// w w w . j a va 2 s .com String sql = "INSERT product VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "A"); prest.setInt(2, 2002); prest.addBatch(); prest.setString(1, "B"); prest.setInt(2, 1998); prest.addBatch(); prest.setString(1, "C"); prest.setInt(2, 1980); prest.addBatch(); prest.setString(1, "D"); prest.setInt(2, 1975); prest.addBatch(); int count[] = prest.executeBatch(); con.commit(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//from w w w.ja v a 2 s.co m Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); int[] updateCounts = pstmt.executeBatch(); checkUpdateCounts(updateCounts); conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:com.kaidad.utilities.MBTilesBase64Converter.java
public static void main(String... args) throws SQLException { if (args.length != 1) { System.out.println("Usage: MBTilesBase64Converter /path/to/file.mbtiles"); System.exit(1);/*from w w w. jav a 2 s.c o m*/ } Connection c = null; try { c = connectToDb(args[0]); // Statement st = c.createStatement(); // ResultSet rs = st.executeQuery("SELECT type, name, tbl_name FROM sqlite_master WHERE type='table'"); // while(rs.next()) { // System.out.println("type: " + rs.getString(1) + ", name: " + rs.getString(2) + ", tbl_name: " + rs.getString(3)); // } JdbcTemplate template = new JdbcTemplate(new SingleConnectionDataSource(c, true)); executeConversion(template); c.commit(); } finally { if (c != null) { c.close(); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/testDb", "name", "pass"); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String insertEmp1 = "insert into emp values(10,'A','trainee')"; String insertEmp2 = "insert into emp values(11,'B','trainee')"; String insertEmp3 = "insert into emp values(12,'C','trainee')"; con.setAutoCommit(false);/*from w ww.ja v a 2 s. c o m*/ stmt.addBatch(insertEmp1); stmt.addBatch(insertEmp2); stmt.addBatch(insertEmp3); ResultSet rs = stmt.executeQuery("select * from emp"); rs.last(); System.out.println("rows before batch execution= " + rs.getRow()); stmt.executeBatch(); con.commit(); System.out.println("Batch executed"); rs = stmt.executeQuery("select * from emp"); rs.last(); System.out.println("rows after batch execution= " + rs.getRow()); }