dao.java
package test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import test.User;
import test.DBUtil;;
public class dao
{
public boolean insert(User user)
{
String sql="insert into resgist(username,sex,nation,time,year,politicalstatus,cos) values('"+user.getUsername()+"','"+user.getSex()+"','"+user.getNation()+"','"+user.getTime()+"','"+user.getYear()+"','"+user.getPoliticalstatus()+"','"+user.getCos()+"')";
Connection conn=DBUtil.getConn();
Statement state=null;
try
{
state=conn.createStatement();
state.executeUpdate(sql);
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(state, conn);
}
return false;
}
public boolean username(String username) {
boolean flag = false;
String sql = "select username from resgist where username = '" + username + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
}
catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}
}
DBUtil.java
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 鏁版嵁搴撹繛鎺ュ伐鍏�
* @author 鍚存灄绁�
*
*/
public class DBUtil {
//鑱旂粨瀛楃涓� //鏁版嵁搴撳悕test
public static String db_url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8";
//鏁版嵁搴撶敤鎴峰悕
public static String db_user = "root";
//鏁版嵁搴撳瘑鐮佸悕
public static String db_pass = "123";
public static Connection getConn () {
//澹版槑涓庢暟鎹簱鐨勮繛鎺ュ苟瀹炰緥鍖栦负null
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;
}
/**
* 鍏抽棴杩炴帴
* @param state
* @param 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();
}
}
}
}
service.java
package test;
import java.util.List;
import test.dao;
import test.User;
public class service
{
dao cdao=new dao();
/**
* 娣诲姞
* @param course
* @return
*/
public boolean insert(User user)
{
boolean f=false;
if(!cdao.username(user.getUsername()))
{
cdao.insert(user);
f=true;
}
return f;
}
}
servlet.java
package test;
import java.io.IOException;
import java.util.List;
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 test.User;
import test.service;
@WebServlet("/servlet")
public class servlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
service service=new service();
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException
{
req.setCharacterEncoding("utf-8");
String method=req.getParameter("method");
if ("insert".equals(method)) {
insert(req, resp);
}
}
private void insert(HttpServletRequest req,HttpServletResponse resp)throws IOException, ServletException
{
req.setCharacterEncoding("utf-8");
String name=req.getParameter("username");
String sex=req.getParameter("sex");
String nation=req.getParameter("nation");
String time=req.getParameter("time");
String year=req.getParameter("year");
String politicalstatus=req.getParameter("politicalstatus");
String cos=req.getParameter("cos");
User user=new User(name,sex,nation,time,year,politicalstatus,cos);
if(service.insert(user))
{
req.setAttribute("message", "娣诲姞鎴愬姛");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
else
{
req.setAttribute("message", "娣诲姞閲嶅,璇烽噸鏂拌緭鍏�");
req.getRequestDispatcher("fail.jsp").forward(req, resp);
}
}
}
User.java
package test;
public class User
{
private String username;
private String sex;
private String nation;
private String time;
private String year;
private String politicalstatus;
private String cos;
public User(String username, String sex,String nation,String time,String year,String politicalstatus,String cos) {
this.username = username;
this.sex = sex;
this.nation = nation;
this.time = time;
this.year = year;
this.politicalstatus = politicalstatus;
this.cos = cos;
}
public String getUsername() {
return username;
}
public void setUserame(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPoliticalstatus() {
return politicalstatus;
}
public void setPoliticalstatus(String politicalstatus) {
this.politicalstatus = politicalstatus;
}
public String getCos() {
return cos;
}
public void setCos(String cos) {
this.cos = cos;
}
}
face.jsp
//主页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
青年志愿者信息服务网
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){
%>
<%} %>