1个回答
2017-05-22 · 做品质教育,用事实说话
关注
展开全部
//用户新增
public boolean addUser(Users user){
try {
conn = ConnDB.getConnection();
String sql = "insert into tb_users values(default,?,?,?,?,?,?)";
System.out.println(sql);
ps = conn.prepareStatement(sql);
ps.setInt(1, user.getDepID());
ps.setString(2, user.getUserName());
ps.setString(3, user.getUserPwd());
ps.setString(4, user.getUserCode());
ps.setString(5, user.getUserSex());
ps.setInt(6, user.getUserAge());
if(ps.executeUpdate()==1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{//关闭当前页打开的相关对象
ConnDB.close(conn, ps, null);
}
return false;
}
//用户删除
public boolean delUser(int id){
try {
conn = ConnDB.getConnection();
String sql = "delete from tb_users where id = ?";
System.out.println(sql);
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
if(ps.executeUpdate()==1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{//关闭当前页打开的相关对象
ConnDB.close(conn, ps, null);
}
return false;
}
//用户编辑
public boolean updateUser(Users user){
try {
conn = ConnDB.getConnection();
String sql = "update tb_users set depID=?,userName=?,userPwd=?,userCode=?,userSex=?,userAge=? where id=?";
System.out.println(user.getDepID()+ user.getUserName()+user.getUserPwd()+user.getUserCode()+user.getUserSex()+user.getUserAge()+user.getId());
ps = conn.prepareStatement(sql);
ps.setInt(1, user.getDepID());
ps.setString(2, user.getUserName());
ps.setString(3, user.getUserPwd());
ps.setString(4, user.getUserCode());
ps.setString(5, user.getUserSex());
ps.setInt(6, user.getUserAge());
ps.setInt(7, user.getId());
if(ps.executeUpdate()==1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{//关闭当前页打开的相关对象
ConnDB.close(conn, ps, null);
}
return false;
}
//根据id查询用户
public Users findAllUserById(int id){
Users u = null;
DepDao depd = null;
try {
conn = ConnDB.getConnection();
String sql = "select * from tb_users where id=?";
System.out.println(sql);
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if(rs.next()){
depd = new DepDao();
Department dep = depd.findAllDepById(rs.getInt("depID"));
System.out.println(dep.getDepName());
u = new Users();
u.setId(rs.getInt("id"));
u.setDepID(rs.getInt("depID"));
u.setUserName(rs.getString("userName"));
u.setUserPwd(rs.getString("userPwd"));
u.setUserCode(rs.getString("userCode"));
u.setUserSex(rs.getString("userSex"));
u.setUserAge(rs.getInt("userAge"));
u.setDep(dep);
}
} catch (Exception e) {
e.printStackTrace();
} finally{//关闭当前页打开的相关对象
ConnDB.close(conn, ps, rs);
}
return u;
}
这是我在dao层写的代码,都调用了ConnDB这个类,这个类完成了驱动的注册,及连接数据库的功能,代码如下:
package com.asjy.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnDB {
private static String url = "jdbc:mysql://localhost:3306/news";
private static String user = "root";
private static String pass = "root";
//1.加载驱动
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
}
}
//2.建立数据库连接对象
public static Connection getConnection() throws Exception{
return DriverManager.getConnection(url,user,pass);
}
//3.关闭数据库
public static void close(Connection conn,Statement ps,ResultSet rs){
try {
if(rs!=null){
rs.close();
rs = null;
}
if(ps!=null){
ps.close();
ps = null;
}
if(conn!=null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询