package Test;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
-
Servlet implementation class Servlet01
*/
@WebServlet("/Servlet01")
public class Servlet01 extends HttpServlet {
private static final long serialVersionUID = 1L;/**
- Default constructor. */ public Servlet01() { // TODO Auto-generated constructor stub }
/**
-
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//响应用户请求
String username = request.getParameter("username");
String password = request.getParameter("password");
String date = request.getParameter("date");
//调用后台逻辑方法
UsersDao dao = new UsersDaoDamo();
Users u1= new Users(username,password,date);
dao.save(u1);List list = dao.list();
PrintWriter out = response.getWriter();
Insert title here");
response.setContentType("text/html");
out.println("<!DOCTYPE html>");
out.println("");
out.println("
out.println("");
for(int i=0;i Users u = (Users)list.get(i);
username = u.getName();
password = u.getPassword();
date = u.getDate();
out.println(username);
out.println(password);
out.println(date);
out.println("");
}
out.println("hello");
out.println("");
out.println("");
out.flush();
out.close();
}
/**
- @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }
}
package Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class UsersDaoDamo implements UsersDao{
public void save(Users u) {
// TODO Auto-generated method stub
DButil util = new DButil();
Connection conn = null;
PreparedStatement pstmt =null;
try {
conn = util.getConnection();
String sql = "insert into person(username,password,date) values(?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, u.getName());
pstmt.setString(2, u.getPassword());
pstmt.setString(3, u.getDate());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(pstmt!=null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public List list() {
DButil util = new DButil();
Connection conn = null;
Statement stmt =null;
ResultSet rs = null;
try {
conn = util.getConnection();
String sql = "select * from person";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
List list = new ArrayList();
while(rs.next()) {
String username = rs.getString(1);
String password = rs.getString(2);
String date = rs.getString(3);
Users u = new Users(username,password,date);
list.add(u);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
}
<!DOCTYPE html>
欢迎登陆
<form action = "/DB/Servlet01" method = "get">
<table>
<tr>
<td colspan = "2"><h3 align = "center">login</h3></td>
</tr>
<tr>
<td colspan = "2"><input type = "text" id = "username" name = "username" class = "up" value = "username" onclick = "usernameclose()"></td>
</tr>
<tr>
<td colspan = "2"><input type = "text" id = "password" name = "password" class = "up" value = "password" onclick = "passwordclose()"></td>
</tr>
<tr>
<td><input type = "submit" value = "login" id = "bulogin" class = "button"></td>
<td><a href = "#"><input type = "button" value = "regiest" id = "buregiest" class = "button" ></a></td>
</tr>
<tr>
<td>Sign<input type = "text" name = "date"></td>
</tr>
</table>
</form>
</div>
</div>