学生信息管理系统(增删改查)【代码不完整】

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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>青年志愿者服务网</title>

</head>
<div align="center">
<h1 style="color: black;">青年志愿者信息服务网</h1>
</div>
<body>
<div align="center">

<div class="a">
<a href="register.jsp">志愿者登记</a>
</div>
<div class="a">
<a href="UserServlet?method=list">修改志愿者信息</a>
</div>
<div class="a">
<a href="del.jsp">删除志愿者信息</a>
</div>
<div class="a">
<a href="search.jsp">查询志愿者信息</a>
</div>
<div class="a">
<a href="list.jsp">志愿者信息浏览</a>
</div>
</div>
</body>
</html>

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<meta charset="UTF-8">
<title>志愿者登记</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){

%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<center> <h6 style="color: black;">志愿者登记页面</h6></center>

<form name = "form1" action="servlet?method=insert" method="post" onsubmit="return check_submit()">
<table>

<tr>
<td>姓名</td>
<td colspan="2"><input type="text" id="username" name="username" /></td>
</tr>
<tr>
<td>性别</td>
<td colspan="3"><input type="radio" name="sex" value="男生">男生</td>
<td colspan="3"><input type="radio" name="sex" value="女生">女生</td>
</tr>
<tr>
<td>民族</td>
<td colspan="3"><input type="text" id="nation" name="nation" /></td>
</tr>
<tr>
<td>注册时间</td>
<td colspan="3"><input type="text" id="time" name="time" /></td>
</tr>
<tr>
<td>年龄</td>
<td colspan="3"><input type="text" id="year" name="year" /></td>
</tr>
<tr>
<td>政治面貌</td>
<td colspan="3"><select name="politicalstatus"id="politicalstatus" >
<option value="群众" >群众</option>
<option value="共青团员" >共青团员</option>
<option value="中共党员" >中共党员</option>
</select></td>
</tr>
<tr>
<td>服务类型(最多四项)</td>
<td colspan="4"><input type="checkbox" name="cos" value="0">扶危济困</td>
<td colspan="4"><input type="checkbox" name="cos" value="1">敬老助残</td>
<td colspan="4"><input type="checkbox" name="cos" value="2">社区服务</td>
<td colspan="4"><input type="checkbox" name="cos" value="3">秩序维护</td>
<td colspan="4"><input type="checkbox" name="cos" value="4">文体服务</td>
<td colspan="4"><input type="checkbox" name="cos" value="5">环境保护</td>
<td colspan="4"><input type="checkbox" name="cos" value="6">治安防范</td>
<td colspan="4"><input type="checkbox" name="cos" value="7">医疗救治</td>
<td colspan="4"><input type="checkbox" name="cos" value="8">法律援助</td>
<td colspan="4"><input type="checkbox" name="cos" value="9">大型活动</td>
<td colspan="4"><input type="checkbox" name="cos" value="10">心理疏导</td>
<td colspan="4"><input type="checkbox" name="cos" value="11">精神抚慰</td>
<td colspan="4"><input type="checkbox" name="cos" value="12">支教支医</td>
<td colspan="4"><input type="checkbox" name="cos" value="13">科学普及</td>
<td colspan="4"><input type="checkbox" name="cos" value="14">应急救援</td>
<td colspan="4"><input type="checkbox" name="cos" value="15">便民服务</td>
<td colspan="4"><input type="checkbox" name="cos" value="16">民事调解</td>
<td colspan="4"><input type="checkbox" name="cos" value="17">文明引导</td>
<td colspan="4"><input type="checkbox" name="cos" value="18">安全生产</td>
<td colspan="4"><input type="checkbox" name="cos" value="19">禁毒宣传</td>
</tr>
<tr>

<td colspan="3"> <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<button type="reset" class="b">重&nbsp;&nbsp;&nbsp;置</button></td>

</tr>
</table>
</form>
</div>


</head>
</body>
</html>

剩余部分待完成......

posted @ 2019-12-09 17:15  Spongbob  阅读(668)  评论(0编辑  收藏  举报