List of usage examples for java.sql Connection prepareStatement
PreparedStatement prepareStatement(String sql) throws SQLException;
PreparedStatement
object for sending parameterized SQL statements to the database. From source file:Emporium.Controle.ContrVpne.java
public static boolean inserirVpne(String sql, String nomeBD) { Connection conn = Conexao.conectar(nomeBD); try {//from w ww.j a v a 2s. c o m PreparedStatement valores = conn.prepareStatement(sql); valores.executeUpdate(); valores.close(); return true; } catch (SQLException e) { Logger.getLogger(ContrPreVendaImporta.class.getName()).log(Level.WARNING, e.getMessage(), e); return false; } finally { Conexao.desconectar(conn); } }
From source file:com.chaosinmotion.securechat.server.commands.UpdateForgottenPassword.java
public static boolean processRequest(Login.UserInfo userinfo, JSONObject requestParams) throws ClassNotFoundException, SQLException, IOException { String newPassword = requestParams.getString("password"); String requestToken = requestParams.getString("token"); /*//from w w w. j a va2s . c o m * Determine if the token matches for this user record. We are in the * unique situation of having a logged in user, but he doesn't know * his password. We also ignore any requests with an expired * token. */ Connection c = null; PreparedStatement ps = null; ResultSet rs = null; try { /* * Delete old requests */ c = Database.get(); ps = c.prepareStatement("DELETE FROM forgotpassword WHERE expires < LOCALTIMESTAMP"); ps.execute(); ps.close(); ps = null; /* * Verify the token we passed back was correct */ ps = c.prepareStatement( "SELECT token " + "FROM forgotpassword " + "WHERE userid = ? " + "AND token = ?"); ps.setInt(1, userinfo.getUserID()); ps.setString(2, requestToken); rs = ps.executeQuery(); if (!rs.next()) return false; // token does not exist or expired. rs.close(); rs = null; ps.close(); ps = null; /* * Step 2: Modify the password. */ ps = c.prepareStatement("UPDATE Users SET password = ? WHERE userid = ?"); ps.setString(1, newPassword); ps.setInt(2, userinfo.getUserID()); ps.execute(); return true; } finally { if (rs != null) rs.close(); if (ps != null) ps.close(); if (c != null) c.close(); } }
From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java
/** * Records a database version as being executed * * @param db The feature to be added to the Version attribute * @param version The feature to be added to the Version attribute * @throws SQLException Description of the Exception *//*from w w w . j av a2 s . c om*/ public static void addVersion(Connection db, String version) throws SQLException { // Add the specified version PreparedStatement pst = db.prepareStatement( "INSERT INTO database_version " + "(script_filename, script_version) VALUES (?, ?) "); pst.setString(1, DatabaseUtils.getTypeName(db) + "_" + version); pst.setString(2, version); pst.execute(); pst.close(); }
From source file:com.freemedforms.openreact.db.DbSchema.java
/** * Determine if a patch has been applied yet. * //from w ww . j a v a2s .c o m * @param patchName * @return Success. */ public static boolean isPatchApplied(String patchName) { Connection c = Configuration.getConnection(); int found = 0; PreparedStatement cStmt = null; try { cStmt = c.prepareStatement("SELECT COUNT(*) FROM tPatch " + " WHERE patchName = ? " + ";"); cStmt.setString(1, patchName); boolean hadResults = cStmt.execute(); if (hadResults) { ResultSet rs = cStmt.getResultSet(); rs.next(); found = rs.getInt(1); rs.close(); } } catch (NullPointerException npe) { log.error("Caught NullPointerException", npe); } catch (Throwable e) { } finally { DbUtil.closeSafely(cStmt); DbUtil.closeSafely(c); } return (boolean) (found > 0); }
From source file:com.sql.Activity.java
/** * Updates activity set to no longer awaiting timestamp for items that have * been properly stamped//from w ww .j a va 2s . c om * * @param id Integer */ public static void markEntryStamped(int id) { Connection conn = null; PreparedStatement ps = null; try { conn = DBConnection.connectToDB(); String sql = "UPDATE Activity SET awaitingTimestamp = 0 WHERE id = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(conn); DbUtils.closeQuietly(ps); } }
From source file:net.big_oh.common.jdbc.JdbcProxyExerciser.java
private static void exercisePreparedSelect(Connection con) throws SQLException { logger.info(StringUtils.center("exercise prepared select", 100, "-")); PreparedStatement preparedStmnt = null; ResultSet rs = null;//from www. ja v a 2s. c om try { preparedStmnt = con.prepareStatement("SELECT * FROM TEST_TABLE WHERE TEST_COLUMN = ?"); preparedStmnt.setString(1, "value1"); rs = preparedStmnt.executeQuery(); while (rs.next()) { System.out.println(rs.getString("TEST_COLUMN")); } } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(preparedStmnt); } }
From source file:com.freemedforms.openreact.db.DbSchema.java
/** * Record record of patch into tPatch table so that patches only run once. * //from www . j av a 2s . com * @param patchName * @return Success. */ public static boolean recordPatch(String patchName) { Connection c = Configuration.getConnection(); boolean status = false; PreparedStatement cStmt = null; try { cStmt = c.prepareStatement( "INSERT INTO tPatch " + " ( patchName, stamp ) " + " VALUES ( ?, NOW() ) " + ";"); cStmt.setString(1, patchName); cStmt.execute(); status = true; } catch (NullPointerException npe) { log.error("Caught NullPointerException", npe); } catch (SQLException sq) { log.error("Caught SQLException", sq); } catch (Throwable e) { } finally { DbUtil.closeSafely(cStmt); DbUtil.closeSafely(c); } return status; }
From source file:com.chaosinmotion.securechat.server.commands.CreateAccount.java
/** * Process the create account request. This should receive the following * objects: the username, the password, the device ID and the public key * for the device. This adds a new entry in the account database, and * creates a new device./*from w w w. j a va2 s . c o m*/ * * If the user account cannot be created, this returns nil. * @param requestParams * @return */ public static UserInfo processRequest(JSONObject requestParams) throws ClassNotFoundException, SQLException, IOException { String username = requestParams.optString("username"); String password = requestParams.optString("password"); String deviceid = requestParams.optString("deviceid"); String pubkey = requestParams.optString("pubkey"); /* * Attempt to insert a new user into the database */ Connection c = null; PreparedStatement ps = null; ResultSet rs = null; try { c = Database.get(); ps = c.prepareStatement("INSERT INTO Users " + " ( username, password ) " + "VALUES " + " ( ?, ? ); SELECT currval('Users_userid_seq')"); ps.setString(1, username); ps.setString(2, password); try { ps.execute(); } catch (SQLException ex) { return null; // Can't insert; duplicate username? } int utc = ps.getUpdateCount(); int userid = 0; if ((utc == 1) && ps.getMoreResults()) { rs = ps.getResultSet(); if (rs.next()) { userid = rs.getInt(1); } rs.close(); rs = null; } ps.close(); ps = null; /* * We now have the user index. Insert the device. Note that it is * highly unlikely we will have a UUID collision, but we verify * we don't by deleting any rows in the device table with the * specified UUID. The worse case scenario is a collision which * knocks someone else off the air. (The alternative would be * to accidentally send the wrong person duplicate messages.) * * Note that we don't actually use a device-identifying identifer, * choosing instead to pick a UUID, so we need to deal with * the possibility (however remote) of duplicate UUIDs. * * In the off chance we did have a collision, we also delete all * old messages to the device; that prevents messages from being * accidentally delivered. */ ps = c.prepareStatement("DELETE FROM Messages " + "WHERE messageid IN " + " (SELECT Messages.messageid " + " FROM Messages, Devices " + " WHERE Messages.deviceid = Devices.deviceid " + " AND Devices.deviceuuid = ?)"); ps.setString(1, deviceid); ps.execute(); ps.close(); ps = null; ps = c.prepareStatement("DELETE FROM Devices WHERE deviceuuid = ?"); ps.setString(1, deviceid); ps.execute(); ps.close(); ps = null; ps = c.prepareStatement("INSERT INTO Devices " + " ( userid, deviceuuid, publickey ) " + "VALUES " + " ( ?, ?, ?)"); ps.setInt(1, userid); ps.setString(2, deviceid); ps.setString(3, pubkey); ps.execute(); /* * Complete; return the user info record */ return new Login.UserInfo(userid); } finally { if (rs != null) rs.close(); if (ps != null) ps.close(); if (c != null) c.close(); } }
From source file:io.apiman.gateway.engine.policies.BasicAuthenticationPolicyTest.java
/** * Creates an in-memory datasource.//from w ww.j a v a2s .c o m * @throws SQLException */ private static BasicDataSource createInMemoryDatasource() throws SQLException { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(Driver.class.getName()); ds.setUsername("sa"); //$NON-NLS-1$ ds.setPassword(""); //$NON-NLS-1$ ds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); //$NON-NLS-1$ Connection connection = ds.getConnection(); connection.prepareStatement( "CREATE TABLE users ( username varchar(255) NOT NULL, password varchar(255) NOT NULL, PRIMARY KEY (username) )") .executeUpdate(); connection.prepareStatement( "INSERT INTO users (username, password) VALUES ('bwayne', 'ae2efd698aefdf366736a4eda1bc5241f9fbfec7')") .executeUpdate(); connection.prepareStatement( "INSERT INTO users (username, password) VALUES ('ckent', 'ea59f7ca52a2087c99374caba0ff29be1b2dcdbf')") .executeUpdate(); connection.close(); return ds; }
From source file:com.keybox.manage.db.ProfileDB.java
/** * deletes profile/*from ww w . ja v a 2 s .c om*/ * * @param profileId profile id */ public static void deleteProfile(Long profileId) { Connection con = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("delete from profiles where id=?"); stmt.setLong(1, profileId); stmt.execute(); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } finally { DBUtils.closeConn(con); } }