List of usage examples for java.sql PreparedStatement executeUpdate
int executeUpdate() throws SQLException;
PreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT
, UPDATE
or DELETE
; or an SQL statement that returns nothing, such as a DDL statement. From source file:SerializeJavaObjects_MySQL.java
public static long writeJavaObject(Connection conn, Object object) throws Exception { String className = object.getClass().getName(); PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL); // set input parameters pstmt.setString(1, className);//from w ww . ja va2 s . c o m pstmt.setObject(2, object); pstmt.executeUpdate(); // get the generated key for the id ResultSet rs = pstmt.getGeneratedKeys(); int id = -1; if (rs.next()) { id = rs.getInt(1); } rs.close(); pstmt.close(); System.out.println("writeJavaObject: done serializing: " + className); return id; }
From source file:net.sf.sprockets.sql.Statements.java
/** * Execute the insert, update, or delete statement, get the number of rows affected, and close * the statement./*from w w w.j a v a2s. c o m*/ * * @param stmt * must already have parameters set * @since 1.4.0 */ public static int update(PreparedStatement stmt) throws SQLException { int rows = stmt.executeUpdate(); stmt.close(); return rows; }
From source file:com.nabla.wapp.server.database.Database.java
public static boolean executeUpdate(final Connection conn, final String sql, final Object... parameters) throws SQLException { final PreparedStatement stmt = StatementFormat.prepare(conn, sql, parameters); try {/*from w w w .j ava 2 s. c o m*/ return stmt.executeUpdate() == 1; } finally { stmt.close(); } }
From source file:com.data2semantics.yasgui.server.db.ConnectionFactory.java
private static void setDeltaApplied(Connection connect, int deltaId) throws SQLException { String sql = "INSERT INTO Deltas (Id) VALUES(?)"; PreparedStatement statement = connect.prepareStatement(sql); statement.setInt(1, deltaId);// w w w . j a v a2 s .c om statement.executeUpdate(); }
From source file:com.sapienter.jbilling.tools.ConvertToBinHexa.java
private static void updateUserRow(int id, String password) throws SQLException { PreparedStatement stmt = connection.prepareStatement("UPDATE base_user set password = ? where ID = ?"); stmt.setString(1, password);/* w ww.j av a 2 s. c o m*/ stmt.setInt(2, id); stmt.executeUpdate(); }
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 v a 2 s .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:jp.co.opentone.bsol.linkbinder.CorresponBodyUpdater.java
static void execute(PreparedStatement stmt) throws Exception { String dir = "100506-Body???/??Body"; stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6564LOG.txt"))); stmt.setLong(2, 1093);//from www.ja va2 s.c o m stmt.executeUpdate(); stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6565LOG.txt"))); stmt.setLong(2, 1094); stmt.executeUpdate(); stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6661LOG.txt"))); stmt.setLong(2, 1095); stmt.executeUpdate(); }
From source file:eu.earthobservatory.testsuite.utils.Utils.java
public static void dropdb() throws SQLException { strabon.close();/*ww w .j a v a 2s. co m*/ //Drop the temp database conn.close(); String url = "jdbc:postgresql://" + serverName + ":" + port; conn = DriverManager.getConnection(url, username, password); PreparedStatement pst = conn.prepareStatement("DROP DATABASE " + databaseName); pst.executeUpdate(); pst.close(); conn.close(); }
From source file:com.l2jfree.gameserver.instancemanager.RaidPointsManager.java
public static void cleanUp() { Connection con = null;//from w w w.j av a2 s . c o m try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement; statement = con.prepareStatement("DELETE from character_raid_points WHERE charId > 0"); statement.executeUpdate(); statement.close(); _list.clear(); } catch (Exception e) { _log.fatal("could not clean raid points: ", e); } finally { L2DatabaseFactory.close(con); } }
From source file:mitll.xdata.dataset.kiva.ingest.KivaIngest.java
@SuppressWarnings("unchecked") public static void loadTable(String tableName, String schemaFilename, String dataFilename) throws Exception { Object[] temp = processSchema(schemaFilename); List<String> names = (List<String>) temp[0]; List<String> types = (List<String>) temp[1]; Class.forName("org.h2.Driver"); Connection connection = DriverManager.getConnection("jdbc:h2:tcp://localhost//h2data/kiva/kiva", "sa", ""); String createSQL = createCreateSQL(tableName, names, types); String insertSQL = createInsertSQL(tableName, names); PreparedStatement statement = connection.prepareStatement(createSQL); statement.executeUpdate(); statement.close();/* w ww . j a va 2 s. c om*/ statement = connection.prepareStatement(insertSQL); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(dataFilename), "UTF-8")); String line = null; int count = 0; long t0 = System.currentTimeMillis(); while ((line = br.readLine()) != null) { count++; List<String> values = split(line, "\t"); executePreparedStatement(statement, types, values); if (count % 10000 == 0) { System.out.println( "count = " + count + "; " + (System.currentTimeMillis() - 1.0 * t0) / count + " ms/insert"); } } br.close(); statement.close(); long t1 = System.currentTimeMillis(); System.out.println("total count = " + count); System.out.println("total time = " + ((t1 - t0) / 1000.0) + " s"); System.out.println((t1 - 1.0 * t0) / count + " ms/insert"); System.out.println((1000.0 * count / (t1 - 1.0 * t0)) + " inserts/s"); }