错误返回值以计算MySQL上的行

时间:2012-03-15 11:30:01

标签: java mysql jdbc

我需要获取MySQL中表的数据行号,但是,在这段代码中,我总是得到相同的静态返回值: sizeDataPredicted 为5。我不知道为什么我的代码返回这里是我的代码:

public static int getPredictedRow(){
          int sizeDataPredicted = 0;

        try {
             Connection con = null;
  String url = "jdbc:mysql://localhost:3306/thesis";
  String db = "";
  String driver = "com.mysql.jdbc.Driver";
  Class.forName(driver);
  con =       (Connection) DriverManager.getConnection(url+db,"root","");
            try {
                String query = "SELECT COUNT(*) FROM predicted";
                 java.sql.Statement a =  con.createStatement();
                ResultSet rs = a.executeQuery(query);

                while (rs.next()){
                sizeDataPredicted = rs.getInt(1);
                }

            } catch (SQLException s) {
                System.out.println("Failed");
            }
        } catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }
        return sizeDataPredicted;

    }

非常感谢您的帮助

0 个答案:

没有答案