首先写一个DBUtil 工具类方便调用
package org.work.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
static {
try {
// 加载jdbc驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
// 使用DriverManager建立数据库连接
conn = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=consumer",
"sa", "1");
} catch (SQLException e) {
System.err.println("连接失败");
}
return conn;
}
public static void getClose(PreparedStatement ps, ResultSet rs,
Connection conn) {
// 关闭资源
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
然后创建一个login.jsp文件用来写页面登录界面,并且把pageEncoding="ISO-8859-1"改为pageEncoding=“UTF-8”
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
table {
margin: 0 auto;
}
.login {
margin: 0 auto;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<form>
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="pwd">
</td>
</tr>
<tr>
<td colspan="2" class="login">
<a href="userList.jsp"><input type="button" value="登录">
</a>
<a href="index.jsp"><input type="button" value="未有账号?注册">
</a>
</td>
</tr>
</table>
</form>
</body>
</html>
然后再创建一个userList.jsp文件用来书写页面布局和java代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="org.work.util.DBUtil"%>
<%@page import="java.sql.SQLException"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'userList.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table>
<tr>
<th>
编号
</th>
<th>
姓名
</th>
<th>
年龄
</th>
<th>
用户名
</th>
<th>
密码
</th>
</tr>
<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 1.调用util包下的DBUtil.getConn();加载驱动并和数据库建立连接
conn = DBUtil.getConn();
// 2.准备一个sql语句
String sql = "SELECT * FROM users";
try {
// 3.使用prepareStatement访问数据库
ps = conn.prepareStatement(sql);
// 4.使用ResultSet接收访问数据库的反馈结果
rs = ps.executeQuery();
while (rs.next()) {
%>
<tr>
<th>
<%=rs.getInt("id")%>
</th>
<th>
<%=rs.getString("name")%>
</th>
<th>
<%=rs.getInt("age")%>
</th>
<th>
<%=rs.getString("user_name")%>
</th>
<th>
<%=rs.getString("pwd")%>
</th>
</tr>
<%
}
} catch (SQLException e) {
e.printStackTrace();
}
// 5.调用util包下的getClose();方法关闭资源
DBUtil.getClose(ps, rs, conn);
%>
</table>
</body>
</html>
最后运行效果图如下
数据库数据如下