在一个JSP页面 显示数据库不同表的内容
2016-01-24 · 做真实的自己 用良心做教育
JSP页面 显示数据库不同表的内容,需要把不同表的数据分别封装到list中。
<span style="font-size:12px;"><span style="font-size:14px;"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ page contentType="text/html;charset=utf-8"%>
<html>
<head>
<style type="text/css">
table {
border: 2px #CCCCCC solid;
width: 360px;
}
td,th {
height: 30px;
border: #CCCCCC 1px solid;
}
</style>
</head>
<body>
<%
//驱动程序名
String driverName = "com.mysql.jdbc.Driver";
//数据库用户名
String userName = "root";
//密码
String userPasswd = "szy";
//数据库名
String dbName = "studentmanage";
//表名
String tableName = "student";
//联结字符串
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" + userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql = "SELECT * FROM " + tableName;
ResultSet rs = statement.executeQuery(sql);
%>
<br>
<br>
<table align="center">
<tr>
<th>
<%
out.print("学号");
%>
</th>
<th>
<%
out.print("姓名");
%>
</th>
<th>
<%
out.print("专业");
%>
</th>
<th>
<%
out.print("班级");
%>
</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td>
<%
out.print(rs.getString(1));
%>
</td>
<td>
<%
out.print(rs.getString(2));
%>
</td>
<td>
<%
out.print(rs.getString(3));
%>
</td>
<td>
<%
out.print(rs.getString(4));
%>
</td>
</tr>
<%
}
%>
</table>
<div align="center">
<br> <br> <br>
<%
out.print("数据查询成功,恭喜你");
%>
</div>
<%
rs.close();
statement.close();
connection.close();
%>
</body>
</html></span><span style="font-size:24px;color: rgb(255, 0, 0);">
</span></span>
展示结果如下:
在做之前先导入sqljdbc.jar
后台:
//实体类(数据库其中一张表的字段)
package s2.org.Entity;
public class Student {
private int id;
private String Name;
private int Age;
private String Sex;
private String Address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
//数据访问类
package s2.org.Dao;
import java.sql.*;
public class DataBase {
private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url="jdbc:sqlserver://localhost:1433;databaseName=Student";
private static final String Name="sa";
private static final String pwd="sasa";
private Connection con;
private PreparedStatement pre;
private ResultSet rs;
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getCon() throws SQLException{
con=DriverManager.getConnection(url,Name,pwd);
return con;
}
public ResultSet query(String sql,Object[]param){
try {
con=getCon();
pre=con.prepareStatement(sql);
if(param!=null && param.length>0){
for(int i=0;i<param.length;i++){
pre.setObject(i+1, param[i]);
}
}
rs=pre.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public int Update(String sql,Object[]param){
int rows=0;
try {
con=getCon();
pre=con.prepareStatement(sql);
if(param!=null && param.length>0){
for(int i=0;i<param.length;i++){
pre.setObject(i+1, param[i]);
}
}
rows=pre.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rows;
}
public void close(){
try {
if(rs!=null)
rs.close();
if(pre!=null)
pre.close();
if(con!=null && con.isClosed())
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//接口
package s2.org.Dao;
import java.util.List;
import s2.org.Entity.Student;
public interface StudentDao {
public List<Student> getAllStudentList();
}
//接口实现类
package s2.org.Dao.Impl;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import s2.org.Dao.*;
import s2.org.Entity.*;
public class StudentDaoImpl extends DataBase implements StudentDao{
public List<Student> getAllStudentList() {
// TODO Auto-generated method stub
String sql="select * from T_Student";
ResultSet rs=super.query(sql, null);
List<Student> list=new ArrayList<Student>();
Student student=null;
try {
while(rs.next()){
student=new Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setAge(rs.getInt(3));
student.setSex(rs.getString(4));
student.setAddress(rs.getString(5));
list.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
super.close();
}
return list;
}
}
前台:
<%@ page language="java" import="java.util.*,s2.org.Dao.Impl.*,s2.org.Entity.*,s2.org.Dao.*" pageEncoding="GBK"%>
<%
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">
</head>
<body>
<form action="<%=path %>/studentadd.jsp" method="post" name="form1">
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
</tr>
<%
StudentDao studentDao=new StudentDaoImpl();
for(Student student:studentDao.getAllStudentList()){
%>
<tr>
<td><a href="<%=path %>/content.jsp?oper=getObject&Name=<%=student.getName() %>"><%=student.getName() %></a></td>
<td><%=student.getAge() %></td>
<td><%=student.getSex() %></td>
<td><%=student.getAddress() %></td>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
这是一张表的显示,几张表的显示是一样的,你的JSP页面有四个表格,那么就把四个表格里面再嵌入表格,就把四张表的内容显示出来了(这是我的方法,可能有点笨,不过挺容易理解的)
思路:当你请求一个servlet或者Action时,先将你所要的数据查出来,即:调用四张表对应的Biz层的查询方法。这个时候,你一定会得到四个List,然后将4个List分别使用request转发给jsp页面,最后在页面上使用循环语句,将4个List循环出来。
做法:
第一:查数据
List listOne = biz.modifyTableOne();//获取第一个表的数据。
List listTwo= biz.modifyTableTwo();//获取第二个表的数据。
List listThree = biz.modifyTableThree();//获取第三个表的数据。
List listFore = biz.modifyTableFore();//获取第四个表的数据。
第二:分别转发四个集合
request.setAttribute("listOne",listOne);
request.setAttribute("listTwo",listTwo);
request.setAttribute("listThree ",listThree );
request.setAttribute("listFore ",listFore );
第三:在jsp中显示数据:
这个就不用我说了吧!
就是 select 出四个表里你要的那个字段
然后在 JSP里分别调用 就可以了
就像你只查找一个表 然后把这个表的数据显示在JSP里
但是你要查找四个表 就一个一个的查 查了以后 再一个一个的显示到JSP里