package com.gyarmy.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.gyarmy.Utils.JdbcUtils; import com.gyarmy.domain.Users; public class UserDao { public void insert(Users user){ Connection conn = null; Statement stmt =null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); //插入操作 String sql = "insert into users values(null,'"+user.getUsername()+"','"+user.getPassword()+"','"+user.getNickname()+"')"; System.out.println(sql); stmt.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JdbcUtils.releaseRes(null, stmt, conn); } } public void update(Users user){ Connection conn = null; Statement stmt =null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); //插入操作 String sql = "update users set username='"+user.getUsername()+"' ,password = '"+user.getPassword()+"' where nickname='"+user.getNickname()+"'"; System.out.println(sql); stmt.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JdbcUtils.releaseRes(null, stmt, conn); } } public void delete(Users user){ Connection conn = null; Statement stmt =null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); //插入操作 String sql = "delete from users where nickname = '"+user.getNickname()+"'"; System.out.println(sql); stmt.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JdbcUtils.releaseRes(null, stmt, conn); } } public List selectAll(){ List list = new ArrayList(); Connection conn = null; Statement stmt =null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); //插入操作 String sql = "select * from users"; rs = stmt.executeQuery(sql); Users user = null; while(rs.next()) { user = new Users(); String username = rs.getString("username"); user.setUsername(username); String password = rs.getString("password"); user.setUsername(username); String nickname = rs.getString("nickname"); user.setUsername(username); list.add(user); } return list; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; }finally{ JdbcUtils.releaseRes(rs, stmt, conn); } } }
0则评论给“DAO测试案例(一)”