12.19
今天练习了一下web系统,主要是为了提升自己的编写web的速度,为明天的考试作准备。没有遇到什么问题,明天计划继续学习web系统。
下面是练习的快速增删改查的代码,速度已经有了,可以很快速的写出一个表的增删改查。
package com.pp;
import java.io.IOException;
import java.sql.SQLException;
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 People
*/
@WebServlet("/aa")
public class aa extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public aa() {
super();
// 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
request.setCharacterEncoding("utf-8");
String ab=request.getParameter("ab");
get login=new get(ab);
hand L=new hand();
try {
L.insert(login);
request.getRequestDispatcher("tijiao.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @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 com.pp;
import java.sql.*;
public class DBUtil{
private DBUtil() {}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection("jdbc:mysql://localhost:3306/user?serverTimezone=UTC&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8","root","123456");
}
public static void close(Connection conn,Statement ps,ResultSet rs) {
if(rs!=null) {
try {
rs.close();
}catch(SQLException s) {
s.printStackTrace();
}
}
if(ps!=null) {
try {
ps.close();
}catch(SQLException s) {
s.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException s) {
s.printStackTrace();
}
}
}
}
package com.pp;
public class get {
String ab;
public String getAb() {
return ab;
}
public void setAb(String x) {
this.ab= x;
}
public get(String a) {
ab=a;
}
}
package com.pp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class hand {
public void insert(get L) throws SQLException {
String sql = "insert into aa(ab) values(?)";
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,L.getAb());
preparedStatement.execute();
}
// public void update(String name,String ID,String sex,String nation,String educator) throws SQLException {
// String sql="UPDATE people SET ID=?,sex=?,nation=?,educator=? where name=?";
// Connection conn= DBUtil.getConnection();
// PreparedStatement papre=conn.prepareStatement(sql);
// papre.setString(1, ID);
// papre.setString(2, sex);
// papre.setString(3, nation);
// papre.setString(4, educator);
// papre.setString(5, name);
// papre.execute();
// }
// public void delete(String name) throws SQLException {
// String sql="delete from people where name=?";
// Connection conn= DBUtil.getConnection();
// PreparedStatement papre=conn.prepareStatement(sql);
// papre.setNString(1, name);
// papre.execute();
// }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="aa" method="post">
<table align="center" border="1">
<tr>
<td>受教育程度</td>
<td>
<input type="text" name="ab" list="elist">
<datalist id="elist">
<option>研究生</option>
<option>大学本科</option>
<option>大学专科</option>
<option>高中</option>
<option>初中</option>
<option>小学</option>
<option>未上过学</option>
</datalist>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>