DAO:
package DAO;
public class dao {
public static java.sql.Connection getconn(){
try {
Class.forName("com.mysql.jdbc.Driver");
String jdurl = ("jdbc:mysql://localhost:3306/exa");
java.sql.Connection conn = java.sql.DriverManager.getConnection(jdurl, "root", "93694264");
System.out.println("数据库连接成功");
return conn;
} catch (Exception e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
return null;
}
public static void insert(int id,String name){
java.sql.Connection conn = getconn();
String sqlinsert = "insert into exa(id,name) values(?,?);";
java.sql.PreparedStatement pst = null;
try{
pst = conn.prepareStatement(sqlinsert);
pst.setInt(1, id);
pst.setString(2,name);
pst.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
}
public static void delete(int id) throws Exception{
java.sql.Connection conn = getconn();
conn.setAutoCommit(false);
String sqldelete = "delete from exa where id = "+id;
try{
java.sql.Statement st = conn.createStatement();
st.execute(sqldelete);
conn.commit();
}catch(Exception e){
e.printStackTrace();
}
}
public static void update(int id,String name){
java.sql.Connection conn = getconn();
String sqlupdate = "update exa set name = ? where id = ?;";
java.sql.PreparedStatement pst = null;
try{
pst = conn.prepareStatement(sqlupdate);
pst.setString(1,name);
pst.setInt(2, id);
pst.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
}
}
index:
<%@page import="DAO.*"%>
<%@ page language="java" import="java.util.*,java.sql.*" 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">
-->
</head>
<body>
<%
Connection conn = dao.getconn();
String sql = "select * from exa;";
ResultSet rs = null;
Statement st = null;
try{
st = conn.createStatement();
rs = st.executeQuery(sql);
}catch(Exception e){
e.printStackTrace();
}
%>
总表:
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>操作</td>
</tr>
<%
while(rs.next()){
Mess mess = new Mess();
mess.setId(rs.getInt(1));
mess.setName(rs.getString(2));
%>
<tr>
<td><%=mess.getId() %></td>
<td><%=mess.getName() %></td>
<td><a href="serv_Servlet?action=delete&id=<%=mess.getId() %>&name=<%=mess.getName()%>">删除</a></td>
</tr>
<%
}
rs.close();
st.close();
conn.close();
%>
</table>
添加信息:
<form id="form" name="form" action="serv_Servlet">
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>操作</td>
</tr>
<tr>
<td><input type="text" id="id" name="id" /></td>
<td><input type="text" id="name" name="name" /></td>
<td><input type="submit" name=action value="add"/></td>
</tr>
</table>
</form>
修改信息:
<form id="form" name="form" action="serv_Servlet">
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>操作</td>
</tr>
<tr>
<td><input type="text" id="id" name="id" /></td>
<td><input type="text" id="name" name="name" /></td>
<td><input type="submit" name=action value="change"/></td>
</tr>
</table>
</form>
</body>
</html>
servlet:
package Myservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DAO.dao;
public class serv_Servlet extends HttpServlet {
/**
* Constructor of the object.
*/
public serv_Servlet() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String sid = request.getParameter("id");
String name = request.getParameter("name");
String action = request.getParameter("action");
int id = Integer.parseInt(sid);
if(action.equals("login")){
if(id==1&name.equals("123456")){
response.sendRedirect("index.jsp");
}
}else if(action.equals("add")){
try{
dao.insert(id, name);
response.sendRedirect("index.jsp");
}catch(Exception e){
e.printStackTrace();
}
}else if(action.equals("change")){
try{
dao.update(id, name);
response.sendRedirect("index.jsp");
}catch(Exception e){
e.printStackTrace();
}
}else if(action.equals("delete")){
try {
dao.delete(id);
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("error!!");
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
login:
<%@ 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 'login.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>
<form id="form" name="form" action="serv_Servlet">
<table border="1">
<tr>
<td>用户名</td>
<td><input type="text" id="id" name="id"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" id="name" name="name"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name=action value="login" /></td>
</tr>
</table>
</form>
</body>
</html>
?