List of usage examples for java.sql PreparedStatement execute
boolean execute() throws SQLException;
PreparedStatement
object, which may be any kind of SQL statement. From source file:com.yahoo.sql4d.sql4ddriver.sql.MysqlAccessor.java
/** * Suitable for CRUD operations where no result set is expected. * @param params//from w w w . j av a2 s. c om * @param query * @return */ public boolean execute(Map<String, String> params, String query) { final AtomicBoolean result = new AtomicBoolean(false); Tuple2<DataSource, Connection> conn = null; try { conn = getConnection(); NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(conn._1()); jdbcTemplate.execute(query, params, new PreparedStatementCallback<Void>() { @Override public Void doInPreparedStatement(PreparedStatement ps) { try { result.set(ps.execute()); } catch (SQLException e) { result.set(false); } return null; } }); } catch (Exception ex) { Logger.getLogger(MysqlAccessor.class.getName()).log(Level.SEVERE, null, ex); result.set(false); } finally { returnConnection(conn); } return result.get(); }
From source file:hr.fer.zemris.vhdllab.dao.impl.AbstractEntityDaoTest.java
private void setupNamedEntity(final String name) { String query = createInsertStatement("NamedEntityTable", "id, version, name", "null, 0, ?"); getJdbcTemplate().execute(query, new PreparedStatementCallback() { @Override/* w ww.j a va 2s. c o m*/ public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setString(1, name); return ps.execute(); } }); }
From source file:com.wso2telco.dao.TransactionDAO.java
/** * Insert transaction log.// w w w .ja va 2s. c o m * * @param transaction the transaction * @param contextId the context id * @param statusCode the status code * @throws Exception the exception */ public static void insertTransactionLog(Transaction transaction, String contextId, int statusCode) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = DbUtil.getConnectDBConnection(); String query = "INSERT INTO mcx_cross_operator_transaction_log (tx_id, tx_status, batch_id, api_id, " + "client_id," + " application_state, sub_op_mcc, sub_op_mnc, timestamp_start, timestamp_end, " + "exchange_response_code)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; ps = conn.prepareStatement(query); ps.setString(1, transaction.getTx_id()); ps.setString(2, transaction.getTx_status()); ps.setString(3, contextId); ps.setString(4, transaction.getApi().getId()); ps.setString(5, transaction.getClient_id()); ps.setString(6, transaction.getApplication_state()); ps.setString(7, transaction.getSubscriber_operator().getMcc()); ps.setString(8, transaction.getSubscriber_operator().getMnc()); ps.setString(9, transaction.getTimestamp().getStart()); ps.setString(10, transaction.getTimestamp().getEnd()); ps.setInt(11, statusCode); ps.execute(); } catch (SQLException e) { handleException("Error in inserting transaction log record : " + e.getMessage(), e); } finally { DbUtil.closeAllConnections(ps, conn, null); } }
From source file:net.mindengine.oculus.frontend.db.jdbc.MySimpleJdbcDaoSupport.java
/** * Returns a collection of string values * @param sql// w w w . j av a 2s . co m * @return * @throws Exception */ public Collection<String> queryStrings(String sql) throws Exception { PreparedStatement ps = getConnection().prepareStatement(sql); logger.info(ps); ps.execute(); ResultSet rs = ps.getResultSet(); Collection<String> results = new LinkedList<String>(); while (rs.next()) { results.add(rs.getString(1)); } return results; }
From source file:com.qagen.osfe.programData.ConfigFileLoader.java
@SuppressWarnings("unchecked") private void loadData(Element root) { try {/* ww w. j ava2s. co m*/ final List<Element> elements = root.element(ELEMENT.table.name()).element(ELEMENT.rows.name()) .elements(ELEMENT.row.name()); final ApplicationContext context = new ClassPathXmlApplicationContext(APP_CONTEXT); final DataSource dataSource = (DataSource) context.getBean("dataSource"); final Connection con = dataSource.getConnection(); for (Element element : elements) { final String statement = getStatement(element); final PreparedStatement preparedStatement = con.prepareStatement(statement); preparedStatement.execute(); } } catch (SQLException e) { throw new ProgramDataException(e); } }
From source file:de.nim.wscr.dao.MemberDAO.java
@Override public void deleteMember(Member member) { try {//w w w.j av a 2 s . c o m PreparedStatement statement = connection.prepareStatement("DELETE FROM db1.member WHERE ID = ?"); statement.setLong(1, member.getId()); statement.execute(); } catch (SQLException e) { throw new RuntimeException(e); } }
From source file:com.wso2telco.gsma.authenticators.DBUtils.java
public static String insertAuthFlowStatus(String username, String status, String uuid) throws SQLException, AuthenticatorException { Connection connection = null; PreparedStatement ps = null; String sql = "INSERT INTO `regstatus` (`uuid`,`username`, `status`) VALUES (?,?,?);"; connection = getConnectDBConnection(); ps = connection.prepareStatement(sql); ps.setString(1, uuid);// w w w.ja v a 2 s .c o m ps.setString(2, username); ps.setString(3, status); log.info(ps.toString()); ps.execute(); if (connection != null) { connection.close(); } return uuid; }
From source file:hu.petabyte.redflags.engine.gear.indicator.helper.KMonitorInstitutions.java
public void init() { if (initialized) { return;/*from w ww.j av a 2 s . c om*/ } if (null == dbhost || null == dbname || null == dbuser || null == dbpass) { LOG.warn("K-Monitor Institutions component is not initialized."); return; } try { LOG.info("Connecting to K-Monitor database..."); conn = DriverManager.getConnection( String.format("jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=utf-8", dbhost, dbname), dbuser, dbpass); LOG.info("Querying institutions..."); PreparedStatement ps = conn.prepareStatement("SELECT name FROM news_institutions"); ps.execute(); ResultSet rs = ps.getResultSet(); while (rs.next()) { institutions.add(rs.getString(1)); } rs.close(); ps.close(); LOG.info("We have {} institutions", institutions.size()); conn.close(); } catch (Exception e) { LOG.error("Failed to connect to KMDB.", e); } initialized = true; }
From source file:Modelo.FileUploadView.java
/** * * @param archivo//from w w w .ja va 2 s . co m * @return */ public boolean registrarArchivo(UploadedFile archivo) { ResultSet rs = null; Statement cmd = null; boolean ejecuto; try { cmd = con.createStatement(); String data = encodeFileToBase64Binary(archivo); // System.out.println("\n \n \n el archivo es: \n"+data+"\n \n "); PreparedStatement stment = con.prepareStatement("Insert into archivos value(?)"); stment.setString(1, data); stment.execute(); //ejecuto = cmd.execute("INSERT INTO archivos(longdata) values(" +data+ ");"); System.out.println("Guardado con exito"); return true; // rs.close(); } catch (Exception e) { System.out.println("SQLException ejecutando sentencia: " + e.getMessage()); return false; } }
From source file:com.haulmont.cuba.core.app.NumberIdWorker.java
protected Object executeScript(String entityName, String sqlScript) { EntityManager em = persistence.getEntityManager(getDataStore(entityName)); StrTokenizer tokenizer = new StrTokenizer(sqlScript, SequenceSupport.SQL_DELIMITER); Object value = null;//from ww w.jav a 2 s. c o m Connection connection = em.getConnection(); while (tokenizer.hasNext()) { String sql = tokenizer.nextToken(); try { PreparedStatement statement = connection.prepareStatement(sql); try { if (statement.execute()) { ResultSet rs = statement.getResultSet(); if (rs.next()) value = rs.getLong(1); } } finally { DbUtils.closeQuietly(statement); } } catch (SQLException e) { throw new IllegalStateException("Error executing SQL for getting next number", e); } } return value; }