package jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.DBConnection;
public class StudentCRUD {
//add
public static void add( String stuid,String sname,String sex,String age,String birthday,String classname) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "insert into student(stuid,sname,sex,age,birthday,classname) values(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, stuid);
ps.setString(2, sname);
ps.setString(3, sex);
ps.setString(4, age);
ps.setString(5, birthday);
ps.setString(6, classname);
ps.executeUpdate();
}finally{
DBConnection.close(rs, ps, conn);
}
}
//add with transaction
/*public static void addwithtrans() throws SQLException {
Connection conn = null;
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
conn.setAutoCommit(false);
String sql1 = "insert into user(id,name,password,sex,age,birthday) values(2,'zhangsan','123','man',21,'1990-12-31')";
String sql2="insert into student(stuid,name,sex,age,birthday) values(2,'zhangsan','man',21,'1990-12-31')";
ps1 = conn.prepareStatement(sql1);
ps1.executeUpdate();
ps2 = conn.prepareStatement(sql2);
ps2.executeUpdate();
conn.commit();
}catch(SQLException e){
conn.rollback();
}finally{
conn.setAutoCommit(true);
DBConnection.close(null, ps1, null);
DBConnection.close(rs, ps2, conn);
}
}*/
//update
/*public static void update() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "update student set sname='lisi' where stuid='stuid'";
st = conn.createStatement();
st.executeUpdate(sql);
}finally{
DBConnection.close(rs, st, conn);
}
}*/
//delete
/*public static void delete() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "delete from student where stuid='stuid'";
st = conn.createStatement();
st.executeUpdate(sql);
}finally{
DBConnection.close(rs, st, conn);
}
}*/
//get
/*public static void get() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "select stuid,sname, sex,age,birthday,classname from student";
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
String password=rs.getString("password");
String sex=rs.getString("sex");
int age=rs.getInt("age");
Date birthday = rs.getDate("birthday");
System.out.println("id=" + id + ";name=" + name + ";password="
+ password + ";sex=" + sex + ";age=" + age + ";birthday="
+ birthday);
}
}finally{
DBConnection.close(rs, st, conn);
}
}*/
public static List<Student> get()throws SQLException {
List<Student> stuList = new ArrayList<Student>();
Connection conn =null;
Statement st = null;
ResultSet rs = null;
try{
conn = DBConnection.getConnection();
String sql = "select * from student;";
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs!=null&&rs.next()) {
Student stu = new Student();
String stuid = rs.getString("stuid");
String sname = rs.getString("sname");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String birthday = rs.getString("birthday");
String classname = rs.getString("classname");
stu.setStuid(stuid);
stu.setSname(sname);
stu.setSex(sex);
stu.setAge(age);
stu.setBirthday(birthday);
stu.setClassname(classname);
stuList.add(stu);
}
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
return stuList;
}
public static Student get(String stuid)throws SQLException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student stu = new Student();
try{
conn = DBConnection.getConnection();
String sql = "select sname,sex,age,birthday,classname from student where stuid=?";
ps = conn.prepareStatement(sql);
ps.setString(1, stuid);
rs = ps.executeQuery();
while(rs.next()){
String sname = rs.getString("sname");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String birthday = rs.getString("birthday");
String classname = rs.getString("classname");
stu.setSname(sname);
stu.setSex(sex);
stu.setAge(age);
stu.setBirthday(birthday);
stu.setClassname(classname);
}
}catch(SQLException e){
System.out.println("错误:"+e.getMessage());
}finally{
DBConnection.close(rs, ps, conn);
}
return stu;
}
public static boolean update(String stuid,String sname,String sex,String age,String birthday,String classname) throws SQLException{
Connection conn = null;
PreparedStatement ps = null;
boolean successflag=false;
try{
conn = DBConnection.getConnection();
String sql = "update student set sname=?,sex=?,age=?,birthday=?,classname=? where stuid=?";
ps = conn.prepareStatement(sql);
ps.setString(1, sname);
ps.setString(2, sex);
ps.setString(3, age);
ps.setString(4, birthday);
ps.setString(5, classname);
ps.setString(6, stuid);
ps.executeUpdate();
successflag=true;
}catch(SQLException e){
System.out.println("错误"+e.getMessage());
}finally{
DBConnection.close(null, ps, conn);
}
return successflag;
}
public static boolean del(String stuid) throws SQLException{
boolean successflag = false;
Connection conn = null;
PreparedStatement ps = null;
try{
conn = DBConnection.getConnection();
String sql = "delete from student where stuid=?";
ps = conn.prepareStatement(sql);
ps.setString(1, stuid);
ps.executeUpdate();
successflag = true;
}catch(SQLException e){
System.out.println("错误"+e.getMessage());
}finally{
DBConnection.close(null, ps, conn);
}
return successflag;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
javaweb 学校管理系统 jsp+servlet+javabean.zip (66个子文件)
Graduation Design
.classpath 922B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 364B
org.eclipse.core.resources.prefs 177B
.jsdtscope 503B
org.eclipse.wst.common.component 458B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.jsdt.ui.superType.container 49B
src
servlet
modifyInformationServlet.java 2KB
getbyCourseid_do.java 2KB
savaTeacherdo.java 2KB
savaCourse.java 2KB
Bpo
TeacherBpo.java 6KB
ClassBpo.java 3KB
JavaBean
ClassBean.java 4KB
TeacherBean.java 4KB
util
DBConnection.java 1KB
jdbc
AdminOperation.java 6KB
bpo.java 987B
StudentCRUD.java 7KB
Student.java 1KB
WebContent
updateStudent_show.jsp 2KB
queryPassword.jsp 558B
updatePassword_do.jsp 791B
class
getCourse.jsp 4KB
magCourse.jsp 3KB
delCourse_do.jsp 545B
addCourse_do.jsp 856B
addCourse.jsp 3KB
updateCourse_show.jsp 3KB
query.jsp 2KB
WEB-INF
lib
mysql-connector-java-5.1.14-bin.jar 758KB
saveStudent_do.jsp 1KB
addCourse_do.jsp 853B
getBytname_do.jsp 3KB
image
01.jpg 49KB
getBytname.jsp 2KB
js
kuadmin.js 3KB
modifyInformation.jsp 1KB
addStudent.jsp 2KB
updateTeacher_show.jsp 3KB
delStudent_do.jsp 789B
error.jsp 329B
destroyAdmin.jsp 413B
addone.jsp 499B
magStudent.jsp 3KB
addCourse.jsp 1KB
queryPassword_do.jsp 625B
updatePassword.jsp 564B
LoginValid.jsp 1KB
css
styles.css 5KB
magTeacher.jsp 3KB
addTeacher.jsp 3KB
addTeacher_do.jsp 998B
index.html 1KB
getBytdept.jsp 2KB
adduser.jsp 2KB
main.jsp 2KB
adduser_do.jsp 857B
getBytdept_do.jsp 3KB
delTeacher_do.jsp 572B
query_do.jsp 4KB
META-INF
MANIFEST.MF 39B
addStudent_do.jsp 2KB
.project 1KB
.gitignore 7B
共 66 条
- 1
资源评论
学术菜鸟小晨
- 粉丝: 1w+
- 资源: 4940
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于QT+C++的智能云监护仪项目,能够实时显示使用者心电、血氧、血压波形及其它各种参数+源码(毕业设计&课程设计&项目开发)
- 基于java开发的app接收硬件端传输的心音信号,具有显示心音波形,发出心音的功能+源码(毕业设计&课程设计&项目开发)
- Python 程序语言设计模式思路-行为型模式:职责链模式:将请求从一个处理者传递到下一个处理者
- 9241703124789646.16健身系统2.apk
- postgresql-16.3-1-windows-x64.exe
- Python 程序语言设计模式思路-结构型模式:装饰器讲解及利用Python装饰器模式实现高效日志记录和性能测试
- 基于YOLOv5和DeepSORT的多目标跟踪仿真与记录
- Python 程序语言设计模式思路-创建型模式:原型模式:通过复制现有对象来创建新对象,面向对象编程
- 卸载软件geek卸载软件geek
- Python 程序语言设计模式思路-创建型模式:单例模式,确保一个类的唯一实例(装饰器)面向对象编程、继承
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功