class User{
private long id;
private String name;
private String gender;
public User(){
super();
}
public User(long id,String name,String gender){
super();
this.id = id;
this.name = name;
this.gender = gender;
}
//get,set方法
}
//DAO类
public class jdbcDao{
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
e.printStackTrace();
}
}
private Connection getConn(){
try{
return DriverManager.getConnection("jdbc:mysql://localhost:3306:xe","root","password");
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
private void release(ResultSet rs,Statement ps,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(ps!=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//用ID获取用户对象
public User getUserById(long id){
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
String sql = "select * from user where id = ?";
try{
conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.setLong(1,id);
rs = ps.executeQuery();
if(rs.next()){
//如果存在,则直接构建并返回用户对象
User user = new User(rs.getLong("id"),rs.getString("name"),rs.getString("gender"));
return user;
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.release(rs,ps,conn);
}
return null;
}
//查询所有用户
public List<User> getAllUsers(){
List<User> list = new ArrayList<User>();
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
String sql = "select * from user ";
try{
conn = this.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//循环添加用户对象
while(rs.next()){
User user = new User(rs.getLong("id"),rs.getString("name"),rs.getString("gender"));
list.add(user);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.release(rs,ps,conn);
}
return list;
}
//修改用户数据
public User updateUser(User user){
PreparedStatement ps = null;
Connection conn = null;
String sql = "update user set id =?,name=?,gender=?";
try{
conn = this.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setLong(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getGender());
int rst = ps.executeUpdate();
if(rst>0){
return new User(user.getId(),user.getName(),user.getGender());
}
conn.commit();
}catch(Exception e){
e.printStackTrace();
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
}finally{
this.release(null,ps,conn);
}
return null;
}
}
//删除用户数据
public boolean deleteUser(long id){
PreparedStatement ps = null;
Connection conn = null;
String sql = "delete from user where id =?;
try{
conn = this.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setLong(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getGender());
int rst = ps.executeUpdate();
if(rst>0){
return user;
}
conn.commit();
}catch(Exception e){
e.printStackTrace();
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
}finally{
this.release(null,ps,conn);
}
return null;
}
}
//插入用户数据
public User insertUser(User user){
PreparedStatement ps = null;
Connection conn = null;
String sql = "insert into user values(?,?,?)";
try{
conn = this.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setLong(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getGender());
int rst = ps.executeUpdate();
if(rst>0){
return user;
}
conn.commit();
}catch(Exception e){
e.printStackTrace();
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
}finally{
this.release(null,ps,conn);
}
return null;
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有