数据库链接类
package Servlet;
import java.sql.*;
public class Dao {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getconn() throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root");
return conn;
}
public static void closeconn(Connection conn) throws SQLException {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
方法类
package Servlet;
import java.sql.*;
public class GetDao {
public ResultSet GetAllStudents() throws SQLException {//显示数据
String sql = "select *from user";
Connection conn = null;
ResultSet rs = null;
conn = Dao.getconn();
if (conn != null) {
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
}
return rs;
}
public int SaveUser(String StuNo, String Name, String Sex,String Year,String JG,String Dept)//保存数据
throws SQLException {
String sql = "insert into user values(?,?,?,?,?,?)";
Connection conn = null;
int i = 0;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, StuNo);
ps.setString(2, Name);
ps.setString(3, Sex);
ps.setInt(4, (Integer.parseInt(Year)));
ps.setString(5, JG);
ps.setString(6, Dept);
i = ps.executeUpdate();
}
return i;
}
public ResultSet FineOne(String StuNo) throws SQLException {//查找数据
String sql = "select *from user where StuNo=?";
Connection conn = null;
ResultSet rs = null;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, StuNo);
rs = ps.executeQuery();
}
return rs;
}
public int UpDate(String StuNo, String Name, String Sex,String Year,String JG,String Dept)//插入数据
throws SQLException {
String sql = "update user set Name=?,Sex=?,Year=?,JG=?,Dept=? where StuNo=?";
Connection conn = null;
int i = 0;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, Name);
ps.setString(2, Sex);
ps.setInt(3, (Integer.parseInt(Year)));
ps.setString(4, JG);
ps.setString(5, Dept);
ps.setString(6, StuNo);
i = ps.executeUpdate();
}
return i;
}
public int DeleteOne(String StuNo) throws SQLException {//删除方法
String sql = "delete from user where StuNo=?";
Connection conn = null;
int i = 0;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, StuNo);
i = ps.executeUpdate();
}
return i;
}
}
显示页面
<%@ page language="java" import="java.sql.*,Servlet.*"
pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
用户信息
<%
GetDao dao = new GetDao();
ResultSet rs = dao.GetAllStudents();
%>
学籍管理
学号
姓名
性别
年龄
籍贯
系别
修改
删除
<%
if (rs != null) {
while (rs.next()) {
%>
<%=rs.getString(1)%>
<%=rs.getString(2)%>
<%=rs.getString(3)%>
<%=rs.getInt(4)%>
<%=rs.getString(5)%>
<%=rs.getString(6)%>
修改
删除
<%
}
rs.close();
}
%>
添加用户
添加用户
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
添加用户
jsp保存页面
<%@ page language="java" import="java.sql.*,Servlet.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
插入数据
<%
GetDao dao = new GetDao();
request.setCharacterEncoding("UTF-8");//中文读取处理
String StuNo = request.getParameter("StuNo");
String Name = request.getParameter("Name");
String Sex = request.getParameter("Sex");
String Year = request.getParameter("Year");
String JG = request.getParameter("JG");
String Dept = request.getParameter("Dept");
int i = dao.SaveUser(StuNo, Name, Sex, Year, JG, Dept);
if (i > 0) {
response.sendRedirect("Students.jsp");
} else {
out.print("添加失败");
}
%>
修改页面
<%@ page language="java" import="java.sql.*,Servlet.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
修改信息
<%
GetDao dao = new GetDao();
String StuNo = request.getParameter("StuNo");
ResultSet rs = dao.FineOne(StuNo);
%>
<%@ page language="java" import="java.sql.*,Servlet.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
保存数据
<%
GetDao dao = new GetDao();
request.setCharacterEncoding("UTF-8");//中文读取处理
String StuNo = request.getParameter("StuNo");
String Name = request.getParameter("Name");
String Sex = request.getParameter("Sex");
String Year = request.getParameter("Year");
String JG = request.getParameter("JG");
String Dept = request.getParameter("Dept");
int i = dao.UpDate(StuNo, Name, Sex, Year, JG, Dept);
if (i > 0) {
response.sendRedirect("Students.jsp");
} else {
out.print("存入失败!");
}
%>
删除页面
<%@ page language="java" import="java.sql.*,Servlet.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
Delete
<%
GetDao dao = new GetDao();
String StuNo = request.getParameter("StuNo");
int i = dao.DeleteOne(StuNo);
if (i > 0) {
response.sendRedirect("Students.jsp");
} else {
out.print("删除失败!");
}
%>