如何在此代码中关闭ResultSet,Statement和Connection?

时间:2012-12-30 14:11:05

标签: connection resultset

如何在此代码中关闭ResultSet,Statement和Connection,因为在几次单击后,程序给出了错误com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:数据源拒绝建立连接,来自服务器的消息:“连接太多“

这是代码......

private void pautoInc() {
    int i;
    int len;
    String code = "";
    try {
        Statement s = Db.connectDb().createStatement();
        ResultSet rs = s.executeQuery("SELECT COUNT(*) AS x FROM product");
        while (rs.next()) {
            String st = rs.getString("x");
            len = st.length();
            i = Integer.parseInt(st);

            if (len == 1 & i != 9) {
                i += 1;
                code = "P0000" + i;
            } else if (i == 9) {
                i += 1;
                code = "P000" + i;
            } else if (len == 2) {
                i += 1;
                code = "P000" + i;

            } else if (len == 3) {
                i += 1;
                code = "P00" + i;
            }
            prodid.setText(code);
        }
    } catch (Exception e) {
        e.printStackTrace();;
    }
}

}

3 个答案:

答案 0 :(得分:2)

private void pautoInc() {
    int i;
    int len;
    String code = "";
    Connection c = null;
    Statement s = null;
    ResultSet rs = null;
    try {
        c = Db.connectDb();
        s = c.createStatement();
        rs = s.executeQuery("SELECT COUNT(*) AS x FROM product");
        while (rs.next()) {
            String st = rs.getString("x");
            len = st.length();
            i = Integer.parseInt(st);

            // ... your business code
        }
    } catch (Exception e) {
        e.printStackTrace();;
    } finally {
        if (null != rs) try { rs.close(); } catch (Exception e) { /* ignore */}
        if (null != s) try { s.close(); } catch (Exception e) { /* ignore */}
        if (null != c) try { c.close(); } catch (Exception e) { /* ignore */}
    }
}

答案 1 :(得分:1)

private void pautoInc() {
int i;
int len;
String code = "";
Statement s = null;
ResultSet rs = null;
try {
    s = Db.connectDb().createStatement();
    rs = s.executeQuery("SELECT COUNT(*) AS x FROM product");
    while (rs.next()) {
        String st = rs.getString("x");
        len = st.length();
        i = Integer.parseInt(st);

        if (len == 1 & i != 9) {
            i += 1;
            code = "P0000" + i;
        } else if (i == 9) {
            i += 1;
            code = "P000" + i;
        } else if (len == 2) {
            i += 1;
            code = "P000" + i;

        } else if (len == 3) {
            i += 1;
            code = "P00" + i;
        }
        prodid.setText(code);
    }
} catch (Exception e) {
    e.printStackTrace();;
} finally {
    try {
        if (rs != null) rs.close();
        if (s != null) s.close();
    } catch(Exception e) { // can not close the rs or statement }
}

}

答案 2 :(得分:0)

您可以添加:

s.getMoreResults(java.sql.Statement.CLOSE_CURRENT_RESULT);
Db.connectDb().commit();
Db.connectDb().close();        

你最好添加:

Db.connectDb().setAutoCommit(false); 

之前

ResultSet rs = s.executeQuery("SELECT COUNT(*) AS x FROM product");