【JDBC】取目标表中记录数的函数


代码:

protected int getActualCount(){
        String table=task.getTo().getTable();
        DB db=task.getTo().getDb();
        Connection conn = null;
        Statement stmt = null;

        try{
            Class.forName(db.getDriver());
            conn = DriverManager.getConnection(db.getUrl(), db.getName(), db.getPswd());

            String cntSql=String.format("select count(*) from %s",table);
            stmt = conn.createStatement();
            ResultSet rs=stmt.executeQuery(cntSql);
            while(rs.next()){
                int count=rs.getInt(1);
                return count;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return -1;
    }

END

相关