Java数据库

package Dao;

import java.sql.Connection;
import java.sql.Statement;

import DBUtil.DBUtil;

import Entity.User;

public class Dao {

public boolean add(User user) {
// TODO Auto-generated method stub
String sql = "insert into users(username,password,name,sex,address,tel,email,stnumber,school,classes,xi,time,beizhu) values('"+ user.getUsername() + "','"+ user.getPassword() +"','"+ user.getName() +"','" + user.getSex() +"','"+ user.getAddress() +"','"+ user.getTel() +"','"+user.getEmail()+"','" + user.getStnumber() +"','" + user.getSchool() +"','" + user.getClasses() +"','" + user.getXi() +"','" + user.getTime() +"','" + user.getBeizhu() +"')";
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;

try {
state = conn.createStatement();
a=state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {

DBUtil.close(state, conn);
}

if (a > 0) {
f = true;
}
return f;

}
}

 

 

package DBUtil;

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* ���ݿ����ӹ���
* @author W
*
*/
public class DBUtil {

public static String db_url = "jdbc:mysql://localhost:3306/user";
public static String db_user = "root";
public static String db_pass = "root";

public static Connection 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();
}

return conn;
}

public static void close (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 void close (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) throws SQLException {
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql ="select * from users";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("空");
}else{
System.out.println("不空");
}
}
}

package Entity;

public class User {

private String username;
private String password;
private String name;
private String sex;
private String address;
private String tel;
private String email;
private String stnumber;
private String school;
private String classes;
private String xi;
private String time;
private String beizhu;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}
public void setPassssword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStnumber() {
return stnumber;
}
public void setStnumber(String stnumber) {
this.stnumber=stnumber;
}public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school=school;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes=classes;
}
public String getXi() {
return xi;
}
public void setXi(String xi) {
this.xi=xi;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time=time;
}public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu=beizhu;
}
public User() {}

public User(String username,String password,String name,String sex,String address,String tel,String email,String stnumber,String school,String classes,String xi,String time,String beizhu) {


this.username=username;
this.password=password;
this.name=name;
this.sex=sex;
this.address=address;
this.tel=tel;
this.email=email;
this.stnumber=stnumber;
this.school=school;
this.classes=classes;
this.xi=xi;
this.time=time;
this.beizhu=beizhu;
}

}

package Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.Dao;

import Entity.User;

 


@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;


public Servlet() {
super();

}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");//璁剧疆璇锋眰鐨勫瓧绗︾紪鐮佹牸寮�
String method = req.getParameter("method");//鑾峰緱璇锋眰琛ㄥ崟涓殑淇℃伅
if ("add".equals(method)) {
add(req, resp);
}
}


private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String address = req.getParameter("address");
String tel = req.getParameter("tel");
String email = req.getParameter("email");
String stnumber = req.getParameter("stnumber");
String school = req.getParameter("school");
String classes = req.getParameter("classes");
String xi = req.getParameter("xi");
String time = req.getParameter("time");
String beizhu = req.getParameter("beihzhu");
User user = new User(username,password,name,sex,address,tel,email,stnumber,school,classes,xi,time,beizhu);

Dao dao =new Dao();
boolean f=dao.add(user);


if(f) {
req.setAttribute("message", "注册成功!");
req.getRequestDispatcher("user.jsp").forward(req,resp);
} else {
req.setAttribute("message", "注册失败!");
req.getRequestDispatcher("user.jsp").forward(req,resp);
}
}
}

 

posted @ 2019-10-21 22:05  程序员亮亮丫!!  阅读(1091)  评论(0编辑  收藏  举报