String sql="DELETE from `user` where userId="+userId;
//保存一条数据
public boolean saveUser(User user) {
boolean flag = false;
//获取连接
Connection conn = null;
//产生执行sql语句的对象
PreparedStatement ps =null;
//加载驱动
try {
Class.forName(jdbDriver);
conn = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword);
//插入一条记录
String sql="INSERT into user (username,password) VALUES (?,?)";
ps=conn.prepareStatement(sql);
//参数赋值
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
//执行sql语句
int rows = ps.executeUpdate();
if (rows > 0) {
flag=true;
System.out.println("插入数据成功!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(st !=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
return flag;
}
//根据UserId查询数据
public User selectByUserId(int userId){
User user =null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(jdbDriver);
conn = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword);
String sql ="select * from user where userId =?";
ps=conn.prepareStatement(sql);
ps.setInt(1,userId);
//执行一个sql语句返回结果集
rs=ps.executeQuery();
if (rs.next()){
user=new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getInt("sex"));
user.setFlag(rs.getInt("flag"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭资源,先产生的后关闭
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(st !=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
} }
return user;
}
这里可以把创建连接和关闭连接等进行提取
public class DBConnectionUtil {
private static String jdbDriver = "com.mysql.jdbc.Driver";
private static String jdbcURL = "jdbc:mysql://localhost:3306/jdbctest?characterEncoding=utf-8";
private static String jdbcUser = "root";
private static String jdbcPassword = "123456";
public static Connection getConnection() {
//获取连接
Connection conn = null;
//产生执行sql语句的对象
Statement st = null;
//加载驱动
try {
Class.forName(jdbDriver);
conn = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword);
System.out.println(conn);
st = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs,Statement st, Connection conn){
//关闭资源,先产生的后关闭
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(st !=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
我们封装工具类之后,就要使用工具类的方法了,把以前重复的代码替换掉。
//保存一条数据
public boolean saveUser(User user) {
boolean flag = false;
//获取连接
Connection conn = null;
//产生执行sql语句的对象
PreparedStatement ps =null;
//加载驱动
try {
conn = DBConnectionUtil.getConnection();
System.out.println(conn);
//插入一条记录
String sql="INSERT into user (username,password) VALUES (?,?)";
ps=conn.prepareStatement(sql);
//参数赋值
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
//执行sql语句
int rows = ps.executeUpdate();
if (rows > 0) {
flag=true;
System.out.println("插入数据成功!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭资源,先产生的后关闭
DBConnectionUtil.closeAll(null,ps,conn);
}
return flag;
}
//根据UserId查询数据
public User selectByUserId(int userId){
User user =null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
conn=DBConnectionUtil.getConnection();
String sql ="select * from user where userId =?";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1,userId);
//执行一个sql语句返回结果集
rs=ps.executeQuery();
if (rs.next()){
user=new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getInt("sex"));
user.setFlag(rs.getInt("flag"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭资源,先产生的后关闭
DBConnectionUtil.closeAll(rs,ps, conn);
}
return user;
}
public class Test02 {
public static void main(String[] args) {
UserDao userDao =new UserDao();
User user=new User();
user.setUsername("xiaoka");
user.setPassword("123456");
boolean flag =userDao.saveUser(user);
System.out.println(flag?"插入成功!":"插入失败");
}
}
测试查询
User user=userDao.selectByUserId(6);
System.out.println(user);