List of usage examples for java.sql Connection commit
void commit() throws SQLException;
Connection
object. From source file:gridool.util.jdbc.JDBCUtils.java
/** * Commits a <code>Connection</code> then closes it, avoid closing if null. */// w ww. j a v a 2s . c o m public static void commitAndClose(Connection conn) throws SQLException { if (conn == null) { throw new IllegalArgumentException( "Given connection is null although trying to commit the transaction."); } try { conn.commit(); } finally { conn.close(); } }
From source file:com.cloudera.sqoop.manager.CubridManagerExportTest.java
public static void assertRowCount(long expected, String tableName, Connection connection) { Statement stmt = null;//from w ww . j a v a 2s . c o m ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT count(*) FROM " + tableName); rs.next(); assertEquals(expected, rs.getLong(1)); } catch (SQLException e) { LOG.error("Can't verify number of rows", e); fail(); } finally { try { connection.commit(); if (stmt != null) { stmt.close(); } if (rs != null) { rs.close(); } } catch (SQLException ex) { LOG.info("Ignored exception in finally block."); } } }
From source file:com.laudandjolynn.mytv.Main.java
/** * ???/*from w ww . j a v a 2 s . c o m*/ */ private static void initDbData0(MyTvData data) { if (data.isDataInited()) { logger.info("init data had insert into db."); return; } Properties tvStationProp = new Properties(); try { tvStationProp.load(Main.class.getResourceAsStream("/" + Constant.TV_STATION_INIT_DATA_FILE_NAME)); } catch (IOException e) { throw new MyTvException( "error occur while load property file: " + Constant.TV_STATION_INIT_DATA_FILE_NAME, e); } Collection<Object> values = tvStationProp.values(); List<String> insertSqlList = new ArrayList<String>(values.size()); String insertSql = "insert into my_tv (stationName,displayName,classify,channel,sequence)"; for (Object value : values) { insertSqlList.add(insertSql + " values (" + value.toString() + ")"); } Connection conn = null; Statement stmt = null; try { conn = DataSourceManager.getConnection(); conn.setAutoCommit(false); stmt = conn.createStatement(); for (String sql : insertSqlList) { stmt.addBatch(sql); logger.info("execute sql: " + sql); } stmt.executeBatch(); conn.commit(); data.writeData(null, Constant.XML_TAG_DATA, "true"); } catch (SQLException e) { if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { throw new MyTvException("error occur while rollback transaction.", e1); } } throw new MyTvException("error occur while execute sql on db.", e); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { throw new MyTvException("error occur while close statement.", e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { throw new MyTvException("error occur while close sqlite connection.", e); } } } }
From source file:com.aurel.track.dbase.Migrate411To412.java
public static void addTaskIsMilestone() { TFieldBean taskIsMilestoneFieldBean = FieldBL.loadByPrimaryKey(SystemFields.INTEGER_TASK_IS_MILESTONE); if (taskIsMilestoneFieldBean == null) { LOGGER.info("Adding task is milestone field"); Connection cono = null; try {// www. ja v a 2 s . c o m cono = InitDatabase.getConnection(); Statement ostmt = cono.createStatement(); cono.setAutoCommit(false); ostmt.executeUpdate(addField(SystemFields.INTEGER_TASK_IS_MILESTONE, "TaskIsMilestone", "com.aurel.track.fieldType.types.system.check.SystemTaskIsMilestone")); ostmt.executeUpdate(addFieldConfig(SystemFields.INTEGER_TASK_IS_MILESTONE, SystemFields.INTEGER_TASK_IS_MILESTONE, "Task is milestone", "Is milestone")); cono.commit(); cono.setAutoCommit(true); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } finally { try { if (cono != null) { cono.close(); } } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } }
From source file:com.laudandjolynn.mytv.Main.java
/** * ??/* w w w .j a v a 2 s . c om*/ */ private static void initDb(MyTvData data) { if (data.isDbInited()) { logger.debug("db have already init."); return; } File myTvDataFilePath = new File(Constant.MY_TV_DATA_FILE_PATH); Connection conn = null; Statement stmt = null; try { conn = DataSourceManager.getConnection(); conn.setAutoCommit(false); stmt = conn.createStatement(); List<String> sqlList = loadSql(); for (String sql : sqlList) { stmt.addBatch(sql); logger.info("execute sql: " + sql); } stmt.executeBatch(); conn.commit(); DataSourceManager.DATA_SOURCE_PROP.remove(DataSourceManager.RES_KEY_DB_SQL_LIST); data.writeData(null, Constant.XML_TAG_DB, "true"); } catch (SQLException e) { if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { throw new MyTvException("error occur while rollback transaction.", e); } } myTvDataFilePath.deleteOnExit(); throw new MyTvException("error occur while execute sql on db.", e); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { throw new MyTvException("error occur while close statement.", e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { throw new MyTvException("error occur while close sqlite connection.", e); } } } }
From source file:com.cloudera.sqoop.manager.PostgresqlExportTest.java
public static void assertRowCount(long expected, String tableName, Connection connection) { Statement stmt = null;/* www. ja va 2 s .c o m*/ ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT count(*) FROM " + tableName); rs.next(); assertEquals(expected, rs.getLong(1)); } catch (SQLException e) { LOG.error("Can't verify number of rows", e); fail(); } finally { try { connection.commit(); if (stmt != null) { stmt.close(); } if (rs != null) { rs.close(); } } catch (SQLException ex) { LOG.info("Ignored exception in finally block."); } } }
From source file:de.iteratec.iteraplan.db.SqlScriptExecutor.java
public static void executeSqlStatements(final List<String> sqlStrings, Connection connection) throws SQLException { for (String statement : sqlStrings) { PreparedStatement stmt = null; try {/*from w w w . j a va 2s . c o m*/ stmt = connection.prepareStatement(statement); stmt.executeUpdate(); } catch (SQLException se) { // ignore alter table errors because these might be ok, if tables do not exist if (!statement.trim().startsWith("alter table")) { LOGGER.error("database error when running db statement '" + statement + "'."); throw se; } } finally { if (stmt != null) { stmt.close(); } } } connection.commit(); }
From source file:com.ibm.research.rdf.store.runtime.service.sql.UpdateHelper.java
private static String executeCall(Connection conn, String sql, int retPid, Object... params) { CallableStatement stmt = null; String ret = null;// ww w. j av a 2s.c o m try { conn.setAutoCommit(false); } catch (SQLException ex) { log.error(ex); ex.printStackTrace(); System.out.println(ex.getLocalizedMessage()); return ret; } try { stmt = conn.prepareCall(sql); int i = 1; for (Object o : params) { stmt.setObject(i, o); i++; } stmt.registerOutParameter(retPid, Types.VARCHAR); stmt.execute(); ret = stmt.getString(retPid); conn.commit(); } catch (SQLException e) { // log.error(e); // e.printStackTrace(); // System.out.println(e.getLocalizedMessage()); ret = null; try { conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block log.error(e1); e1.printStackTrace(); System.out.println(e1.getLocalizedMessage()); ret = null; } } finally { closeSQLObjects(stmt, null); } try { conn.setAutoCommit(true); } catch (SQLException ex) { log.error(ex); ex.printStackTrace(); System.out.println(ex.getLocalizedMessage()); ret = null; } return ret; }
From source file:com.cloudera.sqoop.manager.SQLServerManagerExportManualTest.java
public static void checkSQLBinaryTableContent(String[] expected, String tableName, Connection connection) { Statement stmt = null;//from w w w. ja va 2 s.c om ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT TOP 1 [b1], [b2] FROM " + tableName); rs.next(); assertEquals(expected[0], rs.getString("b1")); assertEquals(expected[1], rs.getString("b2")); } catch (SQLException e) { LOG.error("Can't verify table content", e); fail(); } finally { try { connection.commit(); if (stmt != null) { stmt.close(); } if (rs != null) { rs.close(); } } catch (SQLException ex) { LOG.info("Ignored exception in finally block."); } } }
From source file:com.ibm.research.rdf.store.runtime.service.sql.StoreHelper.java
public static void releaseAutoCommit(Connection conn, boolean changedAutoCommit) { if (changedAutoCommit) { try {/*w w w.j av a2s . c om*/ conn.commit(); conn.setAutoCommit(true); } catch (SQLException e) { } } }