SoFunction
Updated on 2025-04-06

Example explains how JSP obtains data in the ResultSet result set

Obtain all records

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import=".*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Query all user records</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//The URL address of the connection database      String user = "root";//User name of login to the database      String password = "zhangda890126;;";//Password of username of login database      Connection conn = null;
      try{
        ("");//Load the JDBC driver        conn = (url,user,password);//Link database      }catch(ClassNotFoundException e){
        ("Driver class not found");// When an exception is thrown, prompt message      }catch(SQLException e){
        ("Linking MySQL database failed");// Handle SQLException exception      }
      try{
         
        Statement stmt = ();//Create Statement        String queryAll = "SELECT * FROM user";//Query all users        ResultSet rs = (queryAll);
        while(()){
          int userid = (1);//Get the value of the first field userid          String username = (2);//Get the value of the second field username          String userpassword = (3);//Get the value of the third field password           
          //Print out all user information          ("User ID:"+userid+" username:"+username+"User's Password"+userpassword+"<br />");
        }
      }catch(SQLException e){
        ("Failed to query all user information");
      }
    %>
  </body>
</html>

Obtain records of all records with specified fields

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import=".*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Users who query all user recordsidand username</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//The URL address of the connection database      String user = "root";//User name of login to the database      String password = "zhangda890126;;";//Password of username of login database      Connection conn = null;
      try{
        ("");//Load the JDBC driver        conn = (url,user,password);//Link database      }catch(ClassNotFoundException e){
        ("Driver class not found");// When an exception is thrown, prompt message      }catch(SQLException e){
        ("Linking MySQL database failed");// Handle SQLException exception      }
      try{
         
        Statement stmt = ();//Create Statement        String queryAll = "SELECT userid,username FROM user";//Query all users        ResultSet rs = (queryAll);
        while(()){
          int userid = (1);//Get the value of the first field userid          String username = (2);//Get the value of the second field username           
           
          //Print out all user information          ("User ID:"+userid+" username:"+username+"<br />");
        }
      }catch(SQLException e){
        ("Failed to query all user information");
      }
    %>
  </body>
</html>

Get records of the specified starting position and number of records

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import=".*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Obtain three records starting with the second record</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//The URL address of the connection database      String user = "root";//User name of login to the database      String password = "zhangda890126;;";//Password of username of login database      Connection conn = null;
      try{
        ("");//Load the JDBC driver        conn = (url,user,password);//Link database      }catch(ClassNotFoundException e){
        ("Driver class not found");// When an exception is thrown, prompt message      }catch(SQLException e){
        ("Linking MySQL database failed");// Handle SQLException exception      }
      try{
         
        Statement stmt = ();//Create Statement        String queryAll = "SELECT * FROM user limit 1,3";//Query all users        ResultSet rs = (queryAll);
        while(()){
          int userid = (1);//Get the value of the first field userid          String username = (2);//Get the value of the second field username          String userpassword = (2);//Get the password value of the third field           
          //Print out all user information          ("User ID:"+userid+" username:"+username+"User Password:"+userpassword+"<br />");
        }
      }catch(SQLException e){
        ("Failed to query all user information");
      }
    %>
  </body>
</html>

Iterate through the data in the ResultSet and convert it into a table
I searched for a long time on the Internet to traverse the data in the ResultSet and populated them into a web table in turn. Some people say that the ResultSet data is converted into a two-dimensional array and then output it in turn, but the two-digit array needs to specify the storage size in advance, which is inconvenient to amplify. In fact, use the following method:

while(()){
    ("<tr><td>"+(1)+"</td><td>" +(2)+"</td><td>"+(3)+"</td><td>"
        +(4)+"</td><td>"+(5)+"</td><td>"+(6)+"</td><td>"
        +(7)+"</td><td>"+(8)+"</td><td>"+(9)+"</td><td>"
        +(10)+"</td><td>"+(11)+"</td><td>"+(12)+"</td><tr>");
}