0

如何关闭此代码中的 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();;
    }
}

}

4

3 回答 3

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 */}
    }
}
于 2012-12-30T14:17:05.820 回答
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 }
}

}

于 2012-12-30T14:17:36.340 回答
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");    
于 2013-04-18T14:30:07.163 回答