【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

posted @ 2022-03-30 08:47  逆火狂飙  阅读(47)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东