当前位置:首页 > JSP教程 > JSP常见问题

实例讲解JSP获取ResultSet结果集中的数据的方法

获得所有的记录

<%@page language="java" contenttype="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!doctype html> <html> <head> <title>查询所有用户的记录</title> </head> <body> <% string url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址 string user = "root";//登录数据库的用户名 string password = "zhangda890126;;";//登录数据库的用户名的密码 connection conn = null; try{ class.forname("com.mysql.jdbc.driver");//加载jdbc驱动程序 conn = drivermanager.getconnection(url,user,password);//链接数据库 }catch(classnotfoundexception e){ out.println("找不到驱动类");//抛出异常时,提示信息 }catch(sqlexception e){ out.println("链接mysql数据库失败");//处理sqlexception异常 } try{ statement stmt = conn.createstatement();//创建语句对象statement string queryall = "select * from user";//查询所有的用户 resultset rs = stmt.executequery(queryall); while(rs.next()){ int userid = rs.getint(1);//获取第一个字段userid的值 string username = rs.getstring(2);//获取第二个字段username的值 string userpassword = rs.getstring(3);//获取第三个字段password的值 //打印出所有的用户的信息 out.println("用户id:"+userid+" 用户名:"+username+" 用户的密码 "+userpassword+"<br />"); } }catch(sqlexception e){ out.println("查询所有用户信息失败"); } %> </body> </html>

获得所有的记录中的指定字段的记录

<%@page language="java" contenttype="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!doctype html> <html> <head> <title>查询所有用户的记录的用户id和用户名</title> </head> <body> <% string url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址 string user = "root";//登录数据库的用户名 string password = "zhangda890126;;";//登录数据库的用户名的密码 connection conn = null; try{ class.forname("com.mysql.jdbc.driver");//加载jdbc驱动程序 conn = drivermanager.getconnection(url,user,password);//链接数据库 }catch(classnotfoundexception e){ out.println("找不到驱动类");//抛出异常时,提示信息 }catch(sqlexception e){ out.println("链接mysql数据库失败");//处理sqlexception异常 } try{ statement stmt = conn.createstatement();//创建语句对象statement string queryall = "select userid,username from user";//查询所有的用户 resultset rs = stmt.executequery(queryall); while(rs.next()){ int userid = rs.getint(1);//获取第一个字段userid的值 string username = rs.getstring(2);//获取第二个字段username的值 //打印出所有的用户的信息 out.println("用户id:"+userid+" 用户名:"+username+"<br />"); } }catch(sqlexception e){ out.println("查询所有用户信息失败"); } %> </body> </html>

获得指定起始位置和条数的记录

<%@page language="java" contenttype="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!doctype html> <html> <head> <title>获得第二条记录开始的三条记录</title> </head> <body> <% string url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址 string user = "root";//登录数据库的用户名 string password = "zhangda890126;;";//登录数据库的用户名的密码 connection conn = null; try{ class.forname("com.mysql.jdbc.driver");//加载jdbc驱动程序 conn = drivermanager.getconnection(url,user,password);//链接数据库 }catch(classnotfoundexception e){ out.println("找不到驱动类");//抛出异常时,提示信息 }catch(sqlexception e){ out.println("链接mysql数据库失败");//处理sqlexception异常 } try{ statement stmt = conn.createstatement();//创建语句对象statement string queryall = "select * from user limit 1,3";//查询所有的用户 resultset rs = stmt.executequery(queryall); while(rs.next()){ int userid = rs.getint(1);//获取第一个字段userid的值 string username = rs.getstring(2);//获取第二个字段username的值 string userpassword = rs.getstring(2);//获取第三个字段的password的值 //打印出所有的用户的信息 out.println("用户id:"+userid+" 用户名:"+username+" 用户密码:"+userpassword+"<br />"); } }catch(sqlexception e){ out.println("查询所有用户信息失败"); } %> </body> </html>

遍历resultset中的数据并转化为表格
在网上找了很久遍历resultset中的数据并将其依次填充到一个网页表格中,有说将resultset数据转化到一个二维数组中,再依次输出,但二位数组需要提前指定存储大小,不方便扩增。其实用如下方法即可:

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



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!