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;
}
}
LeapMay
- 粉丝: 5w+
- 资源: 2303
最新资源
- 工作备忘录【按日期显示备忘录】.xlsm
- ibm 磁带库的驱动程序
- docker-hadoop-spark-hive 快速构建你的大数据环境.zip
- 基于LOS制导与自适应反步控制律的欠驱动无人船 艇路径跟踪控制(Path Following)系统仿真:LOS艏向角与速度的双重制导方案;基于自适应模糊逻辑系统逼近外界环境以及自身建模不确定项和参数摄
- 正点原子脱机下载软件及驱动
- ibm 磁带库的驱动程序
- echarts市县级地图数据大概334个.zip
- HFSS 3D LAYOUT v2.2:PCB与封装的全波三维电磁场仿真用户手册
- ibm 磁带库的驱动程序 for win 2012 2016
- 采用离散元(pfc)建立考虑颗粒破碎的cluster土石混合体松散地基贯入振动密实模拟二维模型 土石级配可调整、块石形状可调整,可监测应力、位移、孔隙比等参数变化
- ibm 磁带库的驱动程序 for win 2008
- 基于ASP.NET的MVC网上商城系统(对信息的增删改查)源码+数据库(高分项目)
- ibm 磁带库的驱动程序
- 毕设-java-swing-汽车租赁管理系统(详细文档+视频+源码)4.zip
- 基于Learning MPC(LMPC)的四旋翼飞行器(VAU)的避障路径规划 本模型建立一种学习模型预测控制(LMPC)的四旋翼飞行器VAU的控制算法 该控制器可以从数据中学习并找到最佳路径轨迹
- FBP项目全称FootBallPrediction,历经9个月完成的足球比赛预测项目 项目结合大数据+机器学习,不断摸索开发了一个程序 .zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈