packagedao;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importentry.user;importutil.DBUtil;public classuserDao {// public booleanadd(user user) {
String sql= "insert into user2(id,name,password,sex,no,mail,address,class1,yuan,sheng,bei) "
+ "values('" + user.getId() + "','" + user.getName() + "','" + user.getPassword() + "','" + user.getSex() + "','" + user.getNo() + "','" + user.getMail() + "','" + user.getAddress() + user.getClass1() + "','" + user.getYuan() + "','" + user.getSheng()+ "','" + user.getBei()+"','"+"')";// Connection conn =DBUtil.getConn();
Statement state= null;boolean f = false;int a = 0;try{
state=conn.createStatement();
state.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}finally{//DBUtil.close(state, conn);
}if (a > 0) {
f= true;
}returnf;
}public booleanid(String id) {boolean f = false;
String sql= "select id from user2 where name = '" + id + "'";// Connection conn =DBUtil.getConn();
Statement state= null;
ResultSet rs= null;try{
state=conn.createStatement();
rs=state.executeQuery(sql);while(rs.next()) {
f= true;
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(rs, state, conn);
}returnf;
}
}packageentry;public classuser {privateString id;privateString password;privateString name;privateString sex;privateString address;privateString no;privateString mail;privateString class1;privateString yuan;privateString sheng;privateString bei;publicString getId() {returnid;
}public voidsetId(String id) {this.id =id;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getClass1() {returnclass1;
}public voidsetClass1(String class1) {this.class1 =class1;
}publicString getYuan() {returnyuan;
}public voidsetYuan(String yuan) {this.yuan =yuan;
}publicString getSheng() {returnsheng;
}public voidsetSheng(String sheng) {this.sheng =sheng;
}publicString getBei() {returnbei;
}public voidsetBei(String bei) {this.bei =bei;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}publicString getNo() {returnno;
}public voidsetNo(String no) {this.no =no;
}publicString getMail() {returnmail;
}public voidsetMail(String mail) {this.mail =mail;
}publicuser(){}publicuser(String name, String password) {this.name =name;this.password =password;
};publicuser(String id, String password, String name, String sex, String address, String no, String mail,String class1,String yuan,String sheng,String bei) {super();this.id =id;this.password =password;this.name =name;this.sex =sex;this.address =address;this.no =no;this.mail =mail;this.class1 =class1;this.yuan =yuan;this.sheng =sheng;this.bei =bei;
}
}packageservice;importdao.userDao;importentry.user;public classuserservice {
userDao cDao= newuserDao();/*添加*/
public booleanadd(user user) {boolean f = false;if(!cDao.id(user.getId())) {
cDao.add(user);
f= true;
}returnf;
}
}packageservlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importentry.user;importservice.userservice;/*** Servlet implementation class AddServlet*/@WebServlet("/AddServlet")public class AddServlet extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/
publicAddServlet() {super();//TODO Auto-generated constructor stub
}
userservice service= newuserservice();protected void service(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
req.setCharacterEncoding("utf-8");
String method= req.getParameter("method");if("add".equals(method))
add(req,resp);
}protected void add(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
request.setCharacterEncoding("utf-8");//鑾峰彇鏁版嵁
String id = request.getParameter("id");
String password= request.getParameter("password");
String name= request.getParameter("name");
String sex= request.getParameter("sex");
String address= request.getParameter("address");
String no= request.getParameter("no");
String mail= request.getParameter("mail");
String class1= request.getParameter("class1");
String yuan= request.getParameter("yuan");
String sheng= request.getParameter("sheng");
String bei= request.getParameter("bei");
user user= newuser(id,password,name,sex,address,no,mail,class1,yuan,sheng,bei);
System.out.println(id);//娣诲姞鍚庢秷鎭樉绀�
if(service.add(user)) {
request.setAttribute("message", "添加成功");
request.getRequestDispatcher("NewFile.jsp").forward(request,response);
}else{
request.setAttribute("message", "添加失败");
request.getRequestDispatcher("NewFile.jsp").forward(request,response);
}
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
doGet(request, response);
}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/}packageutil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/** 鏁版嵁搴撹繛鎺ュ伐鍏�*/
public classDBUtil {public static String db_url = "jdbc:mysql://localhost:3306/user?useSSL=false";public static String db_user = "root";public static String db_pass = "root";public staticConnection getConn () {
Connection conn= null;try{
Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩
conn =DriverManager.getConnection(db_url, db_user, db_pass);
}catch(Exception e) {
e.printStackTrace();
}returnconn;
}/*10鍏抽棴杩炴帴*/
public static voidclose (Statement state, Connection conn) {if (state != null) {try{
state.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (conn != null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}public static voidclose (ResultSet rs, Statement state, Connection conn) {if (rs != null) {try{
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (state != null) {try{
state.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (conn != null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}public static void main(String[] args) throwsSQLException {
Connection conn=getConn();
PreparedStatement pstmt= null;
ResultSet rs= null;
String sql="select * from user";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();if(rs.next()){
System.out.println("绌�");
}else{
System.out.println("涓嶇┖");
}
}
}