获得所有的记录
<%@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>