0

How can I close the ResultSet, Statement and Connection in this code because after few clicks, program gives me the error com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"

Here is the code...

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 Answers 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 */}
    }
}
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 }
}

}

1
  • Can you explain your answer a bit more?
    – ajacian81
    Dec 30, 2012 at 14:37
0

You can add :

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

you better add :

Db.connectDb().setAutoCommit(false); 

before

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.