增删改查
本篇分享的是关于学生系统的简易版的增删改查,主要用到的工具有SQL SERVER、eclipse,
1、 首先在数据库中建数据库(db_dhm)表(学生表(tb_stu)、教师表(tb_teacher)、班级表(tb_clazz))
使用的jar包
2、数据库连接JAVA建包,如下
建Dao包,写需要的方法
package com.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.entity.Clazz;
import com.entity.Student;
import com.entity.Teacher;
import com.util.DBHelper;
public class StuDao {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
/**
* 查询全部
*/
public List<Student> getAll() {
List<Student> list=new ArrayList<Student>();
try {
con=DBHelper.getCon();
String sql="select * from tb_stu";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
int sid=rs.getInt(1);
String sname=rs.getString(2);
String sclass=rs.getString(3);
String sah=rs.getString(4);
String steacher=rs.getString(5);
Student s=new Student(sid, sname, sclass, sah, steacher);
list.add(s);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return list;
}
/**
* 查询教员
*/
public List<Teacher> getTeach() {
List<Teacher> list=new ArrayList<Teacher>();
try {
con=DBHelper.getCon();
String sql="select * from tb_teacher";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
int tid=rs.getInt(1);
String tname=rs.getString(2);
Teacher t=new Teacher(tid, tname);
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return list;
}
/**
* 查询班级
*/
public List<Clazz> getClazz(){
List<Clazz> list=new ArrayList<Clazz>();
try {
con=DBHelper.getCon();
String sql="select * from tb_clazz";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
String cid=rs.getString(1);
Clazz c=new Clazz(cid);
list.add(c);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return list;
}
/**
* 模糊查询
*/
public List<Student> getStu(String Teach,String Class,String ah){
List<Student> list=new ArrayList<Student>();
try {
con=DBHelper.getCon();
String sql="select * from tb_stu where sname like ? and sclazz like ? and sah like ?";
ps=con.prepareStatement(sql);
ps.setString(1, "%"+Teach+"%");
ps.setString(2, "%"+Class+"%");
ps.setString(3, "%"+ah+"%");
rs=ps.executeQuery();
while(rs.next()) {
int sid=rs.getInt(1);
String sname=rs.getString(2);
String sclass=rs.getString(3);
String sah=rs.getString(4);
String steacher=rs.getString(5);
Student s=new Student(sid, sname, sclass, sah, steacher);
list.add(s);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return list;
}
/**
* 删除
*/
public int del(int id) {
try {
con=DBHelper.getCon();
String sql="delete tb_stu where sid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, id);
int n=ps.executeUpdate();
return n;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return 0;
}
/**
* 修改学生信息
*/
public int upd(Student g) {
try {
con=DBHelper.getCon();
String sql= "update tb_stu set sname=?,sclass=?,sah=?,steacher=? where sid=?";
ps=con.prepareStatement(sql);
ps.setString(1, g.getSname());
ps.setString(2, g.getSclass());
ps.setString(3, g.getSah());
ps.setString(4, g.getSteacher());
ps.setInt(5, g.getSid());
int n=ps.executeUpdate();
return n;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return 0;
}
/**
* 根据id查询学生
*/
public Student getByid(int id) {
try {
con=DBHelper.getCon();
String sql="select * from tb_stu where sid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, id);
rs=ps.executeQuery();
while(rs.next()) {
Student g=new Student(id, rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5));
// System.out.println(g);
return g;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return null;
}
/**
* 增加学生的方法
*/
public int add(Student s) {
try {
con=DBHelper.getCon();
String sql= "insert into tb_stu values(?,?,?,?)";
ps=con.prepareStatement(sql);
ps.setString(1, s.getSname());
ps.setString(2, s.getSclass());
ps.setString(3, s.getSah());
ps.setString(4, s.getSteacher());
int n=ps.executeUpdate();
return n;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myclose(con, ps, rs);
}
return 0;
}
}
建 DBHelper类:(获得连接、关闭连接)
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class DBHelper {
private static final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=db_dhm";
//SQLServerDriver
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getCon() {
Connection con;
try {
con=DriverManager.getConnection(URL,"sa","123");
return con;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void myclose(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null&&!con.isClosed())
con.close();
if(ps!=null)ps.close();
if(rs!=null)rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、建界面(主界面、修改界面、增加界面)
(主界面主要功能:查看所有学生、根据条件(教员、班级、爱好)查询学生、删除学生、跳转修改界面、跳转增加界面)
调用查询教员及班级的方法加入下拉框
查询与删除
例如:教员为aaa 班级为1001 爱好为打篮球
主界面代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<style>
table,tr{
border-collapse: collapse;
}
td{text-align: center;}
#table{
width: 600px;
height: 400px;
}
</style>
<body>
<form action="key.do">
<table border id="table">
<tr>
<td colspan="6">
教员 <select name="teacher">
<option value="null">--请选择教员--</option>
<c:forEach var="li" items="${li }">
<option value="${li.tname }" ${li.tname==teacher?"selected":"" }>${li.tname }</option>
</c:forEach>
</select>
班级 <select name="clazz">
<option value="null">--请选择班级--</option>
<c:forEach var="l" items="${l }">
<option value="${l.cid }" ${l.cid==clazz?"selected":"" }>${l.cid }</option>
</c:forEach>
</select>
学生爱好 <input type="checkbox" name="sah" value="打篮球" <c:forEach var="i" items="${ ah }">
<c:if test="${ i=='打篮球' }"> checked
</c:if>
</c:forEach>>篮球
<input type="checkbox" name="sah" value="敲代码" <c:forEach var="i" items="${ ah }">
<c:if test="${ i=='敲代码' }"> checked
</c:if>
</c:forEach>>足球
<input type="checkbox" name="sah" value="羽毛球" <c:forEach var="i" items="${ ah }">
<c:if test="${ i=='羽毛球' }"> checked
</c:if>
</c:forEach>>羽毛球
<button>查询</button>
</form>
<a href="add.jsp">增加</a>
</td>
</tr>
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>所在班级</td>
<td>爱好</td>
<td>学生的教员</td>
<td>进行操作</td>
</tr>
<c:forEach var="g" items="${list}">
<tr>
<td>${g.sid}</td>
<td>${g.sname}</td>
<td>${g.sclass}</td>
<td>${g.sah}</td>
<td>${g.steacher}</td>
<td>
<a href="del.do?id=${g.sid}">删除</a>
<a href="qu.do?id=${g.sid }">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
根据id删除(由主界面传id值,servlet中调用方法)
增加
增加界面
调用(获得增加界面输入框的值,调用方法,将值加入,爱好用集合的形式遍历添加)
修改(根据id修改)
修改界面(有初始值)
先根据id获得学生所有信息并添加到修改界面中,再调用修改方法
用到的所有包
结束了 我的宝