Example usage for java.sql ResultSet close

List of usage examples for java.sql ResultSet close

Introduction

In this page you can find the example usage for java.sql ResultSet close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:org.flywaydb.test.sample.spring3.BaseDBHelper.java

/**
 * Simple counter query to have simple test inside test methods.
 *
 * @return number of customer in database
 * @throws Exception/*www.j  a v a2s . c  om*/
 */
public int countCustomer() throws Exception {
    int result = -1;
    Statement stmt = con.createStatement();
    String query = "select count(*) from Customer";

    ResultSet rs = stmt.executeQuery(query);
    rs.next();
    Long cnt = rs.getLong(1);
    result = cnt.intValue();

    rs.close();
    stmt.close();

    return result;
}

From source file:com.rajaram.bookmark.dao.BookmarkDaoImpl.java

@Override
public List<Bookmark> getBookmarks(String userName) {

    String sql = "select * from " + tableName + " where user_name = ?";

    Connection conn = null;/*from w w w  .  ja  v a 2 s. co  m*/
    BookmarkRowMapper rowExtract = new BookmarkRowMapper();
    List<Bookmark> bookmarks = null;

    try {
        conn = dataSource.getConnection();
        PreparedStatement prepareStatement = conn.prepareStatement(sql);
        prepareStatement.setString(1, userName);
        ResultSet rs = prepareStatement.executeQuery();

        bookmarks = rowExtract.mapRow(rs);

        rs.close();
        prepareStatement.close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }

    return bookmarks;
}

From source file:org.flywaydb.test.sample.helper.BaseDBHelper.java

/**
 * Simple counter query to have simple test inside test methods.
 * /*from  w  w  w .  j  a  va 2 s.c  om*/
 * @return number of customer in database
 * @throws Exception
 */
public int countCustomer() throws Exception {
    int result = -1;

    Statement stmt = con.createStatement();
    String query = "select count(*) from Customer";

    ResultSet rs = stmt.executeQuery(query);
    rs.next();
    Long cnt = rs.getLong(1);
    result = cnt.intValue();

    rs.close();
    stmt.close();

    return result;
}

From source file:genericepayadmin.AddIpBean.java

public static ArrayList getTreasury(Connection con) throws Exception {
    PreparedStatement ps = null;//  w  ww  .j ava2 s.c  o m
    ResultSet rs = null;
    ArrayList al = new ArrayList();

    try {
        String sql = "select wbser.active, wbser.id, gdep.dept_name, wbser.ipaddress,wbser.checksum from webservice_validator wbser,generic_dept gdep "
                + " where gdep.DEPT_ID=wbser.deptid ";
        ps = con.prepareStatement(sql);

        rs = ps.executeQuery();
        while (rs.next()) {
            AddIpBean tbean = new AddIpBean();
            tbean.setId(rs.getString("id"));
            tbean.setDept_name(rs.getString("dept_name"));
            tbean.setIpaddress(rs.getString("ipaddress"));
            tbean.setChecksum(rs.getString("checksum"));
            tbean.setStatus(rs.getString("active"));
            al.add(tbean);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        if (ps != null)
            ps.close();
        if (rs != null)
            rs.close();
    }
    return al;
}

From source file:net.sf.l2j.gameserver.model.entity.ClanHallSiege.java

public long restoreSiegeDate(int ClanHallId) {
    long res = 0;
    Connection con = null;// w  ww  .  ja v a2s.co  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = con.prepareStatement("SELECT siege_data FROM clanhall_siege WHERE id=?");
        statement.setInt(1, ClanHallId);
        ResultSet rs = statement.executeQuery();
        if (rs.next())
            res = rs.getLong("siege_data");
        rs.close();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: can't get clanhall siege date: " + e.getMessage(), e);
    } finally {
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return res;
}

From source file:com.trackplus.ddl.DataReader.java

private static int getClobTableData(BufferedWriter writer, Connection connection) throws DDLException {
    Statement st = MetaDataBL.createStatement(connection);
    try {//from  w ww  .j a v  a 2  s  .co  m
        ResultSet rs = st.executeQuery("SELECT * FROM TMSPROJECTEXCHANGE");
        int idx = 0;
        String[] columns = MetaDataBL.getColumnsMsProjectExchange();
        while (rs.next()) {
            StringBuilder line = new StringBuilder();
            for (int i = 0; i < columns.length; i++) {
                String value = rs.getString(columns[i]);
                if (value != null && "FILECONTENT".equals(columns[i])) {
                    value = encodeBase64FileContent(value);
                }
                line.append(value);
                if (i < columns.length - 1) {
                    line.append(",");
                }
            }
            MetaDataBL.appendLine(writer, line.toString());
            idx++;
        }
        rs.close();
        return idx;
    } catch (SQLException ex) {
        throw new DDLException(ex.getMessage(), ex);
    }
}

From source file:AppletJDBCDrop.java

private void loadTables() {
    Vector v = new Vector();
    try {/*from  w  w  w  .j  av  a 2s .c o  m*/
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("SHOW TABLES");

        while (rs.next()) {
            v.addElement(rs.getString(1));
        }
        rs.close();
    } catch (SQLException e) {
    }
    v.addElement("acc_acc");
    v.addElement("acc_add");
    v.addElement("junk");
    tableList.setListData(v);
}

From source file:it.emacro.extractor.db.ConnectionPool.java

protected void closeResultSet(ResultSet rs) {
    if (rs == null) {
        return;/*ww  w  . j a  va 2s .co m*/
    }
    try {
        rs.close();
    } catch (SQLException ignore) {
    }
}

From source file:com.ywang.alone.handler.task.AuthTask.java

/**
 *  { 'key':'2597aa1d37d432a','uid':'1020293' }
 * /*from w  w  w  . j  av  a  2s . co m*/
 * @param param
 * @return
 */
private static String getUserInfo(String msg) {

    JSONObject jsonObject = AloneUtil.newRetJsonObject();
    JSONObject param = JSON.parseObject(msg);
    String token = param.getString("key");
    String userId = null;

    if (StringUtils.isEmpty(token)) {
        jsonObject.put("ret", Constant.RET.NO_ACCESS_AUTH);
        jsonObject.put("errCode", Constant.ErrorCode.NO_ACCESS_AUTH);
        jsonObject.put("errDesc", Constant.ErrorDesc.NO_ACCESS_AUTH);
        return jsonObject.toJSONString();
    }

    Jedis jedis = JedisUtil.getJedis();
    Long tokenTtl = jedis.ttl("TOKEN:" + token);
    if (tokenTtl == -1) {
        jsonObject.put("ret", Constant.RET.NO_ACCESS_AUTH);
        jsonObject.put("errCode", Constant.ErrorCode.NO_ACCESS_AUTH);
        jsonObject.put("errDesc", Constant.ErrorDesc.NO_ACCESS_AUTH);
    } else {
        userId = jedis.get("TOKEN:" + token);
        LoggerUtil.logMsg("uid is " + userId);
    }

    JedisUtil.returnJedis(jedis);

    if (StringUtils.isEmpty(userId)) {
        jsonObject.put("ret", Constant.RET.NO_ACCESS_AUTH);
        jsonObject.put("errCode", Constant.ErrorCode.NO_ACCESS_AUTH);
        jsonObject.put("errDesc", Constant.ErrorDesc.NO_ACCESS_AUTH);
        return jsonObject.toJSONString();
    }

    String aimUid = param.getString("uid");//uid??
    if (StringUtils.isEmpty(aimUid)) {
        aimUid = userId;
    }
    DruidPooledConnection conn = null;
    PreparedStatement stmt = null;
    JSONObject data = new JSONObject();
    try {
        conn = DataSourceFactory.getInstance().getConn();

        conn.setAutoCommit(false);

        stmt = conn.prepareStatement("SELECT * FROM USERBASE WHERE USER_ID = ?",
                Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, aimUid);

        ResultSet userInfoRs = stmt.executeQuery();
        if (userInfoRs.next()) {
            UserInfo userInfo = new UserInfo();
            userInfo.setRegTime(userInfoRs.getLong("REG_TIME"));
            userInfo.setUserId(userInfoRs.getString("USER_ID"));
            userInfo.setAvatar(userInfoRs.getString("AVATAR"));
            userInfo.setNickName(userInfoRs.getString("NICKNAME"));
            userInfo.setAge(userInfoRs.getString("AGE"));
            userInfo.setHoroscope(userInfoRs.getString("HORO_SCOPE"));
            userInfo.setHeight(userInfoRs.getString("HEIGHT"));
            userInfo.setWeight(userInfoRs.getString("WEIGHT"));
            userInfo.setRoleName(userInfoRs.getString("ROLENAME"));
            userInfo.setAffection(userInfoRs.getString("AFFECTION"));
            userInfo.setPurpose(userInfoRs.getString("PURPOSE"));
            userInfo.setEthnicity(userInfoRs.getString("ETHNICITY"));
            userInfo.setOccupation(userInfoRs.getString("OCCUPATION"));
            userInfo.setLivecity(userInfoRs.getString("LIVECITY"));
            userInfo.setLocation(userInfoRs.getString("LOCATION"));
            userInfo.setTravelcity(userInfoRs.getString("TRAVELCITY"));
            userInfo.setMovie(userInfoRs.getString("MOVIE"));
            userInfo.setMusic(userInfoRs.getString("MUSIC"));
            userInfo.setBooks(userInfoRs.getString("BOOKS"));
            userInfo.setFood(userInfoRs.getString("FOOD"));
            userInfo.setOthers(userInfoRs.getString("OTHERS"));
            userInfo.setIntro(userInfoRs.getString("INTRO"));
            userInfo.setLastLoginTime(userInfoRs.getLong("LAST_LOGIN_TIME"));
            //            userInfo.setMessagePwd(userInfoRs
            //                  .getString("MESSAGE_PWD"));
            userInfo.setMessageUser(userInfoRs.getString("MESSAGE_USER"));
            //         
            userInfo.setOnline("1");

            data.put("userInfo", JSONObject.toJSON(userInfo));

            PreparedStatement pstmt = conn.prepareStatement(
                    "SELECT * FROM uploads WHERE USER_ID = ? and ENABLING=1", Statement.RETURN_GENERATED_KEYS);
            pstmt.setString(1, aimUid);

            ResultSet pSet = pstmt.executeQuery();
            JSONArray jsonArray = new JSONArray();
            while (pSet.next()) {
                jsonArray.add(pSet.getString("PHOTO_PATH"));
            }
            data.put("photos", JSONObject.toJSON(jsonArray));
            jsonObject.put("data", JSONObject.toJSON(data));

            pSet.close();
            pstmt.close();

        } else {
            jsonObject.put("ret", Constant.RET.SYS_ERR);
            jsonObject.put("errCode", Constant.ErrorCode.SYS_ERR);
            jsonObject.put("errDesc", Constant.ErrorDesc.SYS_ERR);
        }

        userInfoRs.close();
        conn.commit();
        conn.setAutoCommit(true);
    } catch (SQLException e) {
        LoggerUtil.logServerErr(e);
        jsonObject.put("ret", Constant.RET.SYS_ERR);
        jsonObject.put("errCode", Constant.ErrorCode.SYS_ERR);
        jsonObject.put("errDesc", Constant.ErrorDesc.SYS_ERR);
    } finally {
        try {
            if (null != stmt) {
                stmt.close();
            }
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            LoggerUtil.logServerErr(e.getMessage());
        }
    }

    return jsonObject.toJSONString();

}

From source file:de.innovationgate.webgate.api.jdbc.custom.JDBCSource.java

public static void closeResultSet(ResultSet resultSet) {
    if (resultSet != null) {
        try {//  ww  w  . j a  va  2  s.c om
            if (resultSet.getStatement() != null) {
                resultSet.getStatement().close();
            } else {
                resultSet.close();
            }
        } catch (SQLException e1) {
            Logger.getLogger(JDBCSource.LOGGER_NAME).error("Error closing result set in JDBC Source", e1);
        }
    }
}