List of usage examples for java.sql PreparedStatement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:com.dangdang.ddframe.rdb.sharding.example.config.spring.masterslave.repository.OrderRepositoryImpl.java
@Override public void delete() { String orderSql = "DELETE FROM `t_order`"; String orderItemSql = "DELETE FROM `t_order_item`"; try (Connection connection = shardingDataSource.getConnection()) { PreparedStatement preparedStatement = connection.prepareStatement(orderSql); preparedStatement.execute();/*from ww w . j av a2 s.c o m*/ preparedStatement.close(); preparedStatement = connection.prepareStatement(orderItemSql); preparedStatement.execute(); preparedStatement.close(); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON ex.printStackTrace(); } }
From source file:de.static_interface.reallifeplugin.module.stockmarket.database.table.StockUsersTable.java
@Override public void create() throws SQLException { String sql;// w w w. ja v a2s . c o m switch (db.getType()) { case H2: sql = "CREATE TABLE IF NOT EXISTS " + getName() + " (" + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "amount INT NOT NULL," + "stock_id INT NOT NULL," + "user_id INT NOT NULL," + "FOREIGN KEY (stock_id) REFERENCES " + db.getConfig().getTablePrefix() + StocksTable.TABLE_NAME + "(id) ON UPDATE CASCADE ON DELETE CASCADE," + "FOREIGN KEY (user_id) REFERENCES " + db.getConfig().getTablePrefix() + CorpUsersTable.TABLE_NAME + "(id) ON UPDATE CASCADE ON DELETE CASCADE," + "INDEX stock_id_I (stock_id)," + "INDEX user_id_I (user_id)" + ");"; break; case MYSQL: default: sql = "CREATE TABLE IF NOT EXISTS `" + getName() + "` (" + "`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`amount` INT NOT NULL," + "`stock_id` INT NOT NULL," + "`user_id` INT NOT NULL," + "FOREIGN KEY (`stock_id`) REFERENCES `" + db.getConfig().getTablePrefix() + StocksTable.TABLE_NAME + "`(`id`) ON UPDATE CASCADE ON DELETE CASCADE," + "FOREIGN KEY (`user_id`) REFERENCES `" + db.getConfig().getTablePrefix() + CorpUsersTable.TABLE_NAME + "`(`id`) ON UPDATE CASCADE ON DELETE CASCADE," + "INDEX `stock_id_I` (`stock_id`)," + "INDEX `user_id_I` (`user_id`)" + ");"; break; } PreparedStatement statement = db.getConnection().prepareStatement(sql); statement.executeUpdate(); statement.close(); }
From source file:de.static_interface.reallifeplugin.module.contract.database.table.ContractUserOptionsTable.java
@Override public void create() throws SQLException { String sql;//from w ww. j a va 2 s . co m switch (db.getType()) { case H2: sql = "CREATE TABLE IF NOT EXISTS " + getName() + " (" + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "user_id INT NOT NULL," + "contract_id INT NOT NULL," + "money DOUBLE," + "isCreator BOOLEAN NOT NULL" + "FOREIGN KEY (user_id) REFERENCES " + db.getConfig().getTablePrefix() + ContractUsersTable.TABLE_NAME + "(id) ON UPDATE CASCADE ON DELETE CASCADE," + "FOREIGN KEY (contract_id) REFERENCES " + db.getConfig().getTablePrefix() + ContractsTable.TABLE_NAME + "(id) ON UPDATE CASCADE ON DELETE CASCADE," + "INDEX isCreator_I (isCreator)" + ");"; break; case MYSQL: default: sql = "CREATE TABLE IF NOT EXISTS `" + getName() + "` (" + "`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`user_id` INT NOT NULL," + "`contract_id` INT NOT NULL," + "`money` DOUBLE," + "`isCreator` BOOLEAN NOT NULL" + "FOREIGN KEY (`user_id`) REFERENCES " + db.getConfig().getTablePrefix() + ContractUsersTable.TABLE_NAME + "(`id`) ON UPDATE CASCADE ON DELETE CASCADE," + "FOREIGN KEY (`contract_id`) REFERENCES " + db.getConfig().getTablePrefix() + ContractsTable.TABLE_NAME + "(`id`) ON UPDATE CASCADE ON DELETE CASCADE," + "INDEX `isCreator_I` (`isCreator`)" + ");"; break; } PreparedStatement statement = db.getConnection().prepareStatement(sql); statement.executeUpdate(); statement.close(); }
From source file:de.static_interface.reallifeplugin.module.corporation.database.table.CorpsTable.java
@Override public void create() throws SQLException { String sql;/* w ww . j a v a 2 s .c o m*/ switch (db.getType()) { case H2: sql = "CREATE TABLE IF NOT EXISTS " + getName() + " (" + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "balance DOUBLE NOT NULL," + "base_id VARCHAR(255) NOT NULL," + "base_world VARCHAR(255) NOT NULL," + "ceo_uuid VARCHAR(36) NOT NULL UNIQUE KEY," + "corp_name VARCHAR(255) NOT NULL UNIQUE KEY," + "isdeleted TINYINT(0) NOT NULL," + "tag VARCHAR(5) UNIQUE KEY," + "time BIGINT NOT NULL" + ");"; break; case MYSQL: default: sql = "CREATE TABLE IF NOT EXISTS `" + getName() + "` (" + "`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY," + "`balance` DOUBLE NOT NULL," + "`base_id` VARCHAR(255) NOT NULL," + "`base_world` VARCHAR(255) NOT NULL," + "`ceo_uuid` VARCHAR(36) NOT NULL UNIQUE KEY," + "`corp_name` VARCHAR(255) NOT NULL UNIQUE KEY, " + "`isdeleted` TINYINT(0) NOT NULL," + "`tag` VARCHAR(5) UNIQUE KEY," + "`time` BIGINT NOT NULL" + ");"; break; } PreparedStatement statement = db.getConnection().prepareStatement(sql); statement.executeUpdate(); statement.close(); }
From source file:it.emacro.extractor.db.ConnectionPool.java
public void closePreparedStatement(PreparedStatement ps) { if (ps == null) { return;/*from w w w.j a va 2 s. c o m*/ } try { ps.close(); } catch (SQLException ignore) { } }
From source file:com.hangum.tadpole.rdb.core.util.bander.cubrid.CubridExecutePlanUtils.java
/** * cubrid execute plan//from ww w . j a v a2 s . c om * * @param userDB * @param sql * @return * @throws Exception */ public static String plan(UserDBDAO userDB, String sql) throws Exception { if (!sql.toLowerCase().startsWith("select")) { logger.error("[cubrid execute plan ]" + sql); throw new Exception("This statment not select. please check."); } Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { Class.forName("cubrid.jdbc.driver.CUBRIDDriver"); conn = DriverManager.getConnection(userDB.getUrl(), userDB.getUsers(), userDB.getPasswd()); conn.setAutoCommit(false); // auto commit? false . sql = StringUtils.trim(sql).substring(6); if (logger.isDebugEnabled()) logger.debug("[qubrid modifying query]" + sql); sql = "select " + RECOMPILE + sql; pstmt = conn.prepareStatement(sql); ((CUBRIDStatement) pstmt).setQueryInfo(true); rs = pstmt.executeQuery(); String plan = ((CUBRIDStatement) pstmt).getQueryplan(); // ? . conn.commit(); if (logger.isDebugEnabled()) logger.debug("cubrid plan text : " + plan); return plan; } finally { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } }
From source file:adept.kbapi.sql.QuickJDBC.java
/** * execute database update// w w w.j a v a 2 s . com */ public void executeSqlUpdate(PreparedStatement preparedStmt) throws SQLException { try { preparedStmt.executeUpdate(); } finally { preparedStmt.close(); } }
From source file:org.openxdata.test.BaseContextSensitiveTest.java
private void executeSql(String sql) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(sql); try {//ww w. j a v a 2 s. co m statement.execute(); } finally { statement.close(); } }
From source file:com.cedarsoftware.ncube.NCubeManager.java
private static void jdbcCleanup(PreparedStatement stmt) { if (stmt != null) { try {/* www . ja v a 2s. co m*/ stmt.close(); } catch (SQLException e) { LOG.error("Error closing JDBC Statement", e); } } }
From source file:Emporium.Controle.ContrDestinatarioImporta.java
public static int inserir(int idCliente, int idDepartamento, String nome, String cpf_cnpj, String empresa, String cep, String endereco, String numero, String complemento, String bairro, String cidade, String uf, String email, String celular, String pais, String nomeBD, String tags) { Connection conn = Conexao.conectar(nomeBD); String sql = "INSERT INTO cliente_destinatario (idCliente, nome, cpf_cnpj, empresa, cep, endereco, numero, complemento, bairro, cidade, uf, email, celular, pais, tags, idDepartamento) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; //System.out.println("inserir Destinatario -----------------\n"+sql+"\n---------------"); try {/*from www. j a v a 2 s . c om*/ PreparedStatement valores = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); valores.setInt(1, idCliente); valores.setString(2, FormataString.removeSpecialChars(nome)); valores.setString(3, cpf_cnpj); valores.setString(4, empresa); valores.setString(5, cep); valores.setString(6, FormataString.removeSpecialChars(endereco)); valores.setString(7, numero); valores.setString(8, complemento); valores.setString(9, bairro); valores.setString(10, cidade); valores.setString(11, uf); valores.setString(12, email); valores.setString(13, celular); valores.setString(14, pais); valores.setString(15, tags); valores.setInt(16, idDepartamento); valores.executeUpdate(); int autoIncrementKey = 0; ResultSet rs = valores.getGeneratedKeys(); if (rs.next()) { autoIncrementKey = rs.getInt(1); } valores.close(); return autoIncrementKey; } catch (SQLException e) { //System.out.println("ERRO > "+e); ContrErroLog.inserir("HOITO - ContrPreVendaDest.inserir", "SQLException", sql, e.toString()); return 0; } finally { Conexao.desconectar(conn); } }