一个完整的代码
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/shujuku?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();
// }
}
2,jsp被我弄丢了
3,不过最后的结果没问题