Example usage for java.sql PreparedStatement executeQuery

List of usage examples for java.sql PreparedStatement executeQuery

Introduction

In this page you can find the example usage for java.sql PreparedStatement executeQuery.

Prototype

ResultSet executeQuery() throws SQLException;

Source Link

Document

Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

Usage

From source file:com.tethrnet.manage.db.SessionAuditDB.java

/**
 * returns terminal logs for user session for host system
 *
 * @param sessionId     session id/*from  ww  w.jav  a2  s  .co  m*/
 * @param instanceId    instance id for terminal session
 * @return session output for session
 */
public static List<SessionOutput> getTerminalLogsForSession(Connection con, Long sessionId,
        Integer instanceId) {

    List<SessionOutput> outputList = new LinkedList<SessionOutput>();
    try {
        PreparedStatement stmt = con.prepareStatement(
                "select * from terminal_log where instance_id=? and session_id=? order by log_tm asc");
        stmt.setLong(1, instanceId);
        stmt.setLong(2, sessionId);
        ResultSet rs = stmt.executeQuery();
        String output = "";
        while (rs.next()) {
            output = output + rs.getString("output");
        }

        output = output.replaceAll("\\u0007|\u001B\\[K|\\]0;|\\[\\d\\d;\\d\\dm|\\[\\dm", "");
        while (output.contains("\b")) {
            output = output.replaceFirst(".\b", "");
        }
        DBUtils.closeRs(rs);

        SessionOutput sessionOutput = new SessionOutput();
        sessionOutput.setSessionId(sessionId);
        sessionOutput.setInstanceId(instanceId);
        sessionOutput.getOutput().append(output);

        outputList.add(sessionOutput);

        DBUtils.closeRs(rs);

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    return outputList;

}

From source file:com.zimbra.cs.mailbox.util.MetadataDump.java

private static Row getItemRow(DbConnection conn, int groupId, int mboxId, int itemId, boolean fromDumpster)
        throws ServiceException {
    PreparedStatement stmt = null;
    ResultSet rs = null;/*from w ww.  j a  va2  s  . c  o  m*/
    try {
        String sql = "SELECT * FROM " + DbMailItem.getMailItemTableName(groupId, fromDumpster)
                + " WHERE mailbox_id = " + mboxId + " AND id = " + itemId;
        stmt = conn.prepareStatement(sql);
        rs = stmt.executeQuery();
        if (!rs.next())
            throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, null);
        Row row = new Row();
        ResultSetMetaData rsMeta = rs.getMetaData();
        int cols = rsMeta.getColumnCount();
        for (int i = 1; i <= cols; i++) {
            String colName = rsMeta.getColumnName(i);
            String colValue = rs.getString(i);
            if (rs.wasNull())
                colValue = null;
            row.addColumn(colName, colValue);
        }
        return row;
    } catch (SQLException e) {
        throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}

From source file:com.concursive.connect.web.webdav.WebdavManager.java

/**
 * Gets the webdavPassword attribute of the WebdavManager object
 *
 * @param db       Description of the Parameter
 * @param username Description of the Parameter
 * @return The webdavPassword value/*from ww w.j  a va2s .c om*/
 * @throws SQLException Description of the Exception
 */
public static String getWebdavPassword(Connection db, String username) throws SQLException {
    String password = "";
    PreparedStatement pst = db.prepareStatement(
            "SELECT webdav_password " + "FROM users " + "WHERE username = ? " + "AND enabled = ? ");
    pst.setString(1, username);
    pst.setBoolean(2, true);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        password = rs.getString("webdav_password");
    }
    rs.close();
    pst.close();
    return password;
}

From source file:com.keybox.manage.db.SessionAuditDB.java

/**
 * returns terminal logs for user session for host system
 *
 * @param sessionId     session id/* w w  w.  j a va 2  s.  c  o m*/
 * @param instanceId    instance id for terminal session
 * @return session output for session
 */
public static List<SessionOutput> getTerminalLogsForSession(Connection con, Long sessionId,
        Integer instanceId) {

    List<SessionOutput> outputList = new LinkedList<>();
    try {
        PreparedStatement stmt = con.prepareStatement(
                "select * from terminal_log where instance_id=? and session_id=? order by log_tm asc");
        stmt.setLong(1, instanceId);
        stmt.setLong(2, sessionId);
        ResultSet rs = stmt.executeQuery();
        StringBuilder outputBuilder = new StringBuilder("");
        while (rs.next()) {
            outputBuilder.append(rs.getString("output"));
        }

        String output = outputBuilder.toString();
        output = output.replaceAll("\\u0007|\u001B\\[K|\\]0;|\\[\\d\\d;\\d\\dm|\\[\\dm", "");
        while (output.contains("\b")) {
            output = output.replaceFirst(".\b", "");
        }
        DBUtils.closeRs(rs);

        SessionOutput sessionOutput = new SessionOutput();
        sessionOutput.setSessionId(sessionId);
        sessionOutput.setInstanceId(instanceId);
        sessionOutput.getOutput().append(output);

        outputList.add(sessionOutput);

        DBUtils.closeRs(rs);

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    return outputList;

}

From source file:module.entities.NameFinder.DB.java

public static void InsertNewLemma2DB(String lemma, String category) throws SQLException {
    String selectSQL = "SELECT * FROM  WHERE  = ? ";
    PreparedStatement preparedStatement = connection.prepareStatement(selectSQL);
    preparedStatement.setString(1, lemma);
    preparedStatement.setString(2, category);
    ResultSet rs = preparedStatement.executeQuery();
    String insertSQL = "INSERT INTO  " + "(lemma_text,lemma_category) VALUES" + "(?,?)";
    PreparedStatement prepStatement = connection.prepareStatement(insertSQL);
    int id = -1;/*from   w w  w . j a  v a 2 s . c o  m*/
    if (rs.next()) {
        id = rs.getInt(1);
    } else {
        prepStatement.setString(1, lemma);
        prepStatement.setString(2, category);
        prepStatement.addBatch();
    }
    prepStatement.executeBatch();
    prepStatement.close();
}

From source file:com.asakusafw.bulkloader.testutil.UnitTestUtil.java

/**
 * ??//from   www .j av  a2 s.c  o m
 * @param tableName
 * @param expected
 * @return
 */
public static boolean countAssert(String tableName, int expected) throws Exception {
    String sql = "SELECT count(*) FROM " + tableName;
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        printLog("executeQuery???SQL" + sql, "countAssert");
        conn = DBConnection.getConnection();
        stmt = conn.prepareStatement(sql);
        rs = stmt.executeQuery();
        if (rs.next()) {
            int count = rs.getInt("count(*)");
            if (expected == count) {
                printLog("????" + count, "countAssert");
                return true;
            } else {
                printLog("??????" + tableName, "countAssert");
                return false;
            }
        } else {
            printLog("??????" + tableName, "countAssert");
            return false;
        }
    } finally {
        DBConnection.closeRs(rs);
        DBConnection.closePs(stmt);
        DBConnection.closeConn(conn);
    }
}

From source file:com.sql.RelatedCase.java

public static List<RelatedCaseModel> getRelatedCases(EmailOutModel eml) {
    List<RelatedCaseModel> list = new ArrayList();
    Connection conn = null;/*from   w  w w  . jav  a  2 s . c om*/
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        int i = 0;
        conn = DBConnection.connectToDB();
        String sql = "SELECT * FROM RelatedCase WHERE LEN(relatedCaseNumber) = 16 " + " AND CaseYear = ? "
                + " AND CaseType = ? " + " AND CaseMonth = ? " + " AND CaseNumber = ? ";

        ps = conn.prepareStatement(sql);
        ps.setString(1, eml.getCaseYear());
        ps.setString(2, eml.getCaseType());
        ps.setString(3, eml.getCaseMonth());
        ps.setString(4, eml.getCaseNumber());

        rs = ps.executeQuery();
        while (rs.next()) {
            String[] relatedCase = rs.getString("relatedCaseNumber").split("-");

            if (relatedCase.length == 4) {
                RelatedCaseModel item = new RelatedCaseModel();
                item.setCaseYear(rs.getString("caseYear"));
                item.setCaseType(rs.getString("caseType"));
                item.setCaseMonth(rs.getString("caseMonth"));
                item.setCaseNumber(rs.getString("caseNumber"));
                item.setRelatedCaseYear(relatedCase[0]);
                item.setRelatedCaseType(relatedCase[1]);
                item.setRelatedCaseMonth(relatedCase[2]);
                item.setRelatedCaseNumber(relatedCase[3]);
                list.add(item);
            }
        }
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Determines status of some jar file.//from   ww  w. ja  v a 2 s  . com
 * @return Returns one of DOWNLOADED, INSTALLED, or AVAILABLE.
 */
private static String getStatus(String jarFile) throws SQLException {
    String fn = "${FARRAGO_HOME}/plugin/" + jarFile;
    String outfile = FarragoProperties.instance().expandProperties(fn);
    File f = new File(outfile);
    if (f.exists()) { // Either just downloaded or installed
        String ret = "DOWNLOADED";
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        String name = jarFile.replaceAll("\\.jar", "");
        String query = "SELECT name FROM localdb.sys_root.dba_jars WHERE " + "name = ? AND url IN (?,?)";
        PreparedStatement ps = conn.prepareStatement(query);
        ps.setString(1, name);
        ps.setString(2, fn);
        ps.setString(3, "file:" + fn);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            ret = "INSTALLED";
        }
        rs.close();
        ps.close();

        return ret;
    } else {
        return "AVAILABLE";
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static List<KVPair> getKeyValuePairs() {
    List<KVPair> results = new ArrayList<KVPair>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;//from ww w  .j a  v a2  s.co  m
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM KV_PAIR ORDER BY k");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            results.add(new KVPair(rs.getString("k"), rs.getString("v")));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:edu.ucsd.library.xdre.web.StatsCollectionsReportController.java

public static long getDiskSize(String collectionId) throws SQLException {
    Connection con = null;//from ww  w.  j a  v a  2  s . com
    PreparedStatement ps = null;
    ResultSet rs = null;
    long size = 0;
    try {
        con = Constants.DAMS_DATA_SOURCE.getConnection();
        ps = con.prepareStatement(StatsUsage.DLP_COLLECTION_RECORD_QUERY);
        ps.setString(1, collectionId);
        rs = ps.executeQuery();
        if (rs.next()) {
            size = rs.getLong("SIZE_BYTES");
        }
    } finally {
        Statistics.close(rs);
        Statistics.close(ps);
        Statistics.close(con);
        rs = null;
        ps = null;
        con = null;
    }
    return size;
}