tables.jsp
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ page import="java.sql.DriverManager" %>
-
<%@ page import="java.sql.PreparedStatement" %>
-
<%@ page import="java.sql.ResultSet" %>
-
<%@ page import="java.sql.SQLException" %>
-
<%@ page import="java.sql.Connection" %>
-
<%@ page import="java.sql.Statement" %>
-
<%@ page import="model.User" %>
-
<%@ page import="javax.servlet.http.HttpServletRequest" %>
-
<!DOCTYPE html>
-
<html lang="en">
-
<%@ include file="menu.jsp" %>
-
<body>
-
<div id="content">
-
<div id="content-header">
-
<h1>Tables</h1>
-
<div class="btn-group">
-
<a class="btn btn-large tip-bottom" title="Manage Files"><i class="icon-file"></i></a>
-
<a class="btn btn-large tip-bottom" title="Manage Users"><i class="icon-user"></i></a>
-
<a class="btn btn-large tip-bottom" title="Manage Comments"><i class="icon-comment"></i><span class="label label-important">5</span></a>
-
<a class="btn btn-large tip-bottom" title="Manage Orders"><i class="icon-shopping-cart"></i></a>
-
</div>
-
</div>
-
<div id="breadcrumb">
-
<a href="#" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
-
<a href="#" class="current">Tables</a>
-
</div>
-
<div class="container-fluid">
-
<div class="row-fluid">
-
<div class="span12">
-
<div class="widget-box">
-
<div class="widget-title">
-
<h5>Dynamic table</h5>
-
</div>
-
<div class="widget-content nopadding">
-
<table class="table table-bordered data-table">
-
<thead>
-
<tr>
-
<th>ID</th>
-
<th>USER</th>
-
<th>NAME</th>
-
<th>TEL</th>
-
</tr>
-
</thead>
-
<tbody>
-
<%
-
List<User> list = new ArrayList<User>();
-
list = (List<User>)request.getAttribute("list");
-
for (User user : list){
-
%>
-
<tr>
-
<td><%=user.getId() %></td>
-
<td><%=user.getAccount() %></td>
-
<td><%=user.getUsername() %></td>
-
<td><%=user.getTel() %></td>
-
</tr>
-
<%} %>
-
</tbody>
-
</table>
-
</div>
-
</div>
-
</div>
-
</div>
-
<div class="row-fluid">
-
<div id="footer" class="span12">
-
2012 © UniAdmin.</div>
-
</div>
-
</div>
-
</div>
-
<script src="js/jquery.min.js"></script>
-
<script src="js/jquery.ui.custom.js"></script>
-
<script src="js/bootstrap.min.js"></script>
-
<script src="js/jquery.uniform.js"></script>
-
<script src="js/select2.min.js"></script>
-
<script src="js/jquery.dataTables.min.js"></script>
-
<script src="js/unicorn.js"></script>
-
<script src="js/unicorn.tables.js"></script>
-
</body>
-
</html>
web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
-
<display-name>web_project</display-name>
-
<welcome-file-list>
-
<welcome-file>welcome.jsp</welcome-file>
-
</welcome-file-list>
-
<servlet>
-
<servlet-name>FindServlet</servlet-name>
-
<servlet-class>servlet.FindServlet</servlet-class>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>FindServlet</servlet-name>
-
<url-pattern>/FindServlet</url-pattern>
-
</servlet-mapping>
-
</web-app>
FindSerlvet.java
-
package servlet;
-
import java.io.IOException;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.List;
-
import javax.servlet.ServletException;
-
import javax.servlet.annotation.WebServlet;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import model.User;
-
public class FindServlet extends HttpServlet {
-
private static final long serialVersionUID = 1L;
-
public FindServlet() {
-
super();
-
}
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
Connection conn = null;
-
PreparedStatement ps = null;
-
ResultSet resultSet = null;
-
String sql;
-
// MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值
-
// 避免中文乱码要指定useUnicode和characterEncoding
-
// 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,
-
// 下面语句之前就要先创建javademo数据库
-
String url = "jdbc:mysql://localhost:3306/s61?"
-
+ "user=root&password=&useUnicode=true&characterEncoding=UTF8";
-
try {
-
// 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,
-
// 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
-
Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
-
System.out.println("成功加载MySQL驱动程序");
-
// 一个Connection代表一个数据库连接
-
conn = DriverManager.getConnection(url);
-
// Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
-
sql = "select a.F01 as id,a.F02 as user,b.F02 as name,a.F04 as tel from s61.t6110 a,s61.t6141 b where a.F01=b.F01 ";
-
ps = conn.prepareStatement(sql);
-
resultSet = ps.executeQuery();
-
Integer id = null;
-
String account = null;
-
String name = null;
-
String tel = null;
-
List<User> list = new ArrayList<User>();
-
while (resultSet.next()){
-
User myuser = new User();
-
id = resultSet.getInt(1);
-
myuser.setId(id);
-
account = resultSet.getString(2);
-
myuser.setAccount(account);
-
name = resultSet.getString(3);
-
myuser.setUsername(name);
-
tel = resultSet.getString(4);
-
myuser.setTel(tel);
-
list.add(myuser);
-
}
-
request.setAttribute("list", list);
-
} catch (SQLException e) {
-
System.out.println("MySQL操作错误");
-
e.printStackTrace();
-
} catch (Exception e) {
-
e.printStackTrace();
-
} finally {
-
try {
-
resultSet.close();
-
ps.close();
-
conn.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
request.getRequestDispatcher("tables.jsp")
-
.forward(request, response);
-
}
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
doGet(request, response);
-
}
-
}
User.java
-
package model;
-
public class User {
-
private Integer id;
-
private String account;
-
private String username;
-
private String tel;
-
public void setId(Integer id){
-
this.id = id;
-
}
-
public Integer getId(){
-
return id;
-
}
-
public void setAccount(String account){
-
this.account = account;
-
}
-
public String getAccount(){
-
return account;
-
}
-
public void setUsername(String username){
-
this.username = username;
-
}
-
public String getUsername(){
-
return username;
-
}
-
public void setTel(String tel){
-
this.tel = tel;
-
}
-
public String getTel(){
-
return tel;
-
}
-
}
来自为知笔记(Wiz)
转载于:https://www.cnblogs.com/tangbinghaochi/p/6293150.html