How do you access the value of an SQL count () query in a Java program

前端 未结 7 1139
梦毁少年i
梦毁少年i 2021-02-01 00:54

I want to get to the value I am finding using the COUNT command of SQL. Normally I enter the column name I want to access into the getInt() getString() method, what do I do in t

7条回答
  •  情歌与酒
    2021-02-01 01:01

            <%
            try{
                Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bala","bala","bala");
                if(con == null) System.out.print("not connected");
                Statement st = con.createStatement();
                String myStatement = "select count(*) as total from locations";
                ResultSet rs = st.executeQuery(myStatement);
                int num = 0;
                while(rs.next()){
                    num = (rs.getInt(1));
                }
    
            }
            catch(Exception e){
                System.out.println(e);  
            }
    
            %>
    

提交回复
热议问题