jdbc操作mysql(二)
阿新 • • 發佈:2021-06-17
案例四:封裝共有操作
封裝一個數據庫的會話的類
點選檢視詳細程式碼
import java.sql.*; public class ConnectionUtil { /** * 獲取連線物件的方法,返回一個Connection * 方法體中是共有操作:載入驅動,建立連線 */ public static Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/jdbctest?serverTimezone=GMT"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); return connection; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 關閉連線物件的方法,傳入引數Connection物件 * 方法體中是共有操作:判斷該物件是否為連線狀態,是則關閉 */ public static void close(Connection conn) { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 關閉PreparedStatement物件的方法,傳入引數為該物件 * 方法體中是共有操作:判斷該物件是否為連線狀態,是則關閉 */ public static void close(PreparedStatement ps) { if(ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 關閉結果集物件的方法,傳入引數為ResultSet物件 * 方法體中是共有操作:判斷該物件是否為連線狀態,是則關閉 */ public static void close(ResultSet rs) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
封裝對使用者類的crud方法
點選檢視詳細程式碼
import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class UserManager { /** * 封裝新增使用者的方法 */ public void add(User user) { Connection conn = null; PreparedStatement ps = null; try { conn = ConnectionUtil.getConnection(); String sql = "insert into t_user(username, age, sex, birthday) value(?, ?, ?, ?)"; ps = conn.prepareStatement(sql); //setString 將指定的引數設定為給定的Java String值 ps.setString(1, user.getUsername()); // 第一個佔位符傳入的引數 ps.setInt(2, user.getAge()); ps.setString(3, user.getSex()); ps.setDate(4, new Date(user.getBirthday().getTime()), null); int rows = ps.executeUpdate(); // 受影響行 System.out.println("->插入成功" + rows + "條資訊"); } catch (Exception e) { e.printStackTrace(); } finally { ConnectionUtil.close(ps); ConnectionUtil.close(conn); } } /** * 根據id刪除使用者 */ public void del(int id) { Connection conn = null; PreparedStatement ps = null; try { conn = ConnectionUtil.getConnection(); String sql = "delete from t_user where id=?"; ps = conn.prepareCall(sql); ps.setInt(1, id); int i = ps.executeUpdate(); System.out.println("刪除成功" + i + "條資訊"); }catch (Exception e){ e.printStackTrace(); }finally { ConnectionUtil.close(ps); ConnectionUtil.close(conn); } } /** * 根據id查詢使用者資訊 */ public User getUser(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = ConnectionUtil.getConnection(); String sql = "select * from t_user where id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, id); rs = ps.executeQuery(); while(rs.next()) { User u = new User(); // getInt 檢索的當前行中指定列的值 u.setId(rs.getInt("id")); // getString 檢索的當前行中指定列的值 u.setUsername(rs.getString("username")); u.setAge(rs.getInt("age")); u.setBirthday(rs.getDate("birthday")); u.setSex(rs.getString("sex")); return u; } } catch (Exception e) { e.printStackTrace(); } finally { ConnectionUtil.close(rs); ConnectionUtil.close(ps); ConnectionUtil.close(conn); } return null; } /** * 查詢所有使用者的方法 */ public List<User> getAll() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = ConnectionUtil.getConnection(); String sql = "select * from t_user"; ps = conn.prepareStatement(sql); // 返回結果集 rs = ps.executeQuery(); List<User> lists = new ArrayList<User>(); while(rs.next()) { User u = new User(); u.setId(rs.getInt("id")); u.setUsername(rs.getString("username")); u.setAge(rs.getInt("age")); u.setBirthday(rs.getDate("birthday")); u.setSex(rs.getString("sex")); lists.add(u); } return lists; } catch (Exception e) { e.printStackTrace(); } finally { ConnectionUtil.close(rs); ConnectionUtil.close(ps); ConnectionUtil.close(conn); } return null; } /** * 更新使用者資訊的方法 */ public int update(User user){ Connection conn = null; PreparedStatement ps = null; try { conn = ConnectionUtil.getConnection(); String sql = "update t_user set age=?,username=?,sex=?,birthday=? where id=?"; ps = conn.prepareCall(sql); ps.setInt(1, user.getAge()); ps.setString(2, user.getUsername()); ps.setString(3, user.getSex()); ps.setDate(4, new Date(user.getBirthday().getTime()), null); ps.setInt(5, user.getId()); int i = ps.executeUpdate(); System.out.println("跟新成功" + i + "條資訊"); }catch (Exception e){ e.printStackTrace(); }finally { ConnectionUtil.close(ps); ConnectionUtil.close(conn); } return 0; } }
測試
點選檢視詳細程式碼
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; public class UserTest { public static void main(String[] args) throws ParseException { UserManager um = new UserManager(); User u = new User(); u.setUsername("大力"); u.setAge(20); u.setSex("男"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); u.setBirthday(sdf.parse("1990-10-1")); //插入一條資料 um.add(u); //查詢id為1的使用者資訊 User user = um.getUser(1); System.out.println("id=1 ->" + user.getUsername()); //查詢所有使用者 List<User> list = um.getAll(); for(User u1 : list) { System.out.println("User ->" + u1.getUsername()); } //更新id為1的使用者資訊 User user1 = new User(); user1.setId(1); user1.setUsername("狗剩"); user1.setAge(15); user1.setSex("男"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); user1.setBirthday(sdf1.parse("2021-1-1")); um.update(user1); //刪除id為1的使用者資訊 um.del(1); } }
- 測試結果
->插入成功1條資訊
id=1 ->小花
User ->小花
User ->大力
跟新成功1條資訊
刪除成功1條資訊