Look for "Water King" find "Water King"

Topics requirements: known to have a "Water King" in a post, which is published in the number of posts accounted for more than half of the total number of posts, the existing one table, find the "Water King" of the table.

Table has id, id number appears the most is the king of water.

code show as below:
Copy the code
package Week14;
import java.sql.Connection;
import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.DBUtil.*; public class Tiezi { static DBUtil DBUtil =new DBUtil(); public static void main(String[] args) { shuiwang(); } public static void shuiwang() { String sql = "select nameid,count(*) AS num from tiezi GROUP BY nameid ORDER BY num DESC"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String nameid=rs.getString("nameid"); String num=rs.getString("num"); System .out.println(nameid+" "+num); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } } }
Copy the code

 

Topics requirements: known to have a "Water King" in a post, which is published in the number of posts accounted for more than half of the total number of posts, the existing one table, find the "Water King" of the table.

Table has id, id number appears the most is the king of water.

code show as below:
Copy the code
package Week14;
import java.sql.Connection;
import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.DBUtil.*; public class Tiezi { static DBUtil DBUtil =new DBUtil(); public static void main(String[] args) { shuiwang(); } public static void shuiwang() { String sql = "select nameid,count(*) AS num from tiezi GROUP BY nameid ORDER BY num DESC"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String nameid=rs.getString("nameid"); String num=rs.getString("num"); System .out.println(nameid+" "+num); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } } }
Copy the code

 

Guess you like

Origin www.cnblogs.com/Evak/p/10964383.html