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