標籤:成功 util imp 增刪改查 auto als val 一個 ext
先寫一個工具類,有實現MySQL資料庫連接的方法,和關閉資料庫連接、關閉ResultSet 結果集、關閉PreparedStatement 的方法。代碼如下:
package com.swift;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class DBUtil { //串連MySQL資料庫工具 public static Connection getConn() { Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); try { conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } //關閉資料庫連接、sql串連、結果集 public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs) { if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } if(ps!=null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }}
接下來實現登入,已有帳號在資料庫中檢驗存在與否。代碼如下:
package com.swift;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class JDBC_Login { public static void main(String[] args) { User user=new User("swift","abc123"); if(login(user)) { System.out.println("成功"); }else { System.out.println("失敗"); } } public static boolean login(User user) { Connection conn=DBUtil.getConn(); PreparedStatement ps=null; ResultSet rs=null; try { ps=conn.prepareStatement("select * from sw_user where username=? and password=?"); ps.setString(1, user.getUsername()); ps.setString(2, user.getPassword()); rs=ps.executeQuery(); if(rs.next()) { return true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.closeAll(conn, ps, rs); return false; }}
實現在資料庫增加資料,資料庫表sw_user中插入新資料,代碼如下:
package com.swift;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBC_Add { public static void main(String[] args) { User user=new User("duoduo","abc123"); if(add(user)) { System.out.println("帳號註冊成功"); }else { System.out.println("註冊失敗"); } } private static boolean add(User user) { Connection conn=DBUtil.getConn(); PreparedStatement ps=null; try { ps=conn.prepareStatement("insert into sw_user(username,password) values(?,?)"); ps.setString(1, user.getUsername()); ps.setString(2, user.getPassword()); int count=ps.executeUpdate(); if(count==1) { return true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.closeAll(conn, ps, null); return false; }}
實現在資料庫中刪除資料,資料庫表sw_user中刪除已存在的資料,代碼如下:
package com.swift;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBC_Delete { public static void main(String[] args) { User user=new User("duoduo","abc123"); if(JDBC_Login.login(user)) { if(delete(user)) { System.out.println("帳號登出成功"); }else { System.out.println("登出失敗"); } }else { System.out.println("賬戶不存在"); } } private static boolean delete(User user) { Connection conn=DBUtil.getConn(); PreparedStatement ps=null; try { ps=conn.prepareStatement("delete from sw_user where username=? and password=?"); ps.setString(1, user.getUsername()); ps.setString(2, user.getPassword()); int count=ps.executeUpdate(); if(count==1) { return true; } } catch (SQLException e) { e.printStackTrace(); } DBUtil.closeAll(conn, ps, null); return false; }}
實現在資料庫中修改資料,資料庫表sw_user中更新已存在的資料,代碼如下:
package com.swift;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBC_Update { public static void main(String[] args) { User user=new User("swift","abc123"); String setPassword="abc12345"; if(JDBC_Login.login(user)) { if(update(user,setPassword)) { System.out.println("密碼修改成功"); }else { System.out.println("修改失敗"); } }else { System.out.println("賬戶不存在"); } } private static boolean update(User user,String setPassword) { Connection conn=DBUtil.getConn(); PreparedStatement ps=null; try { ps=conn.prepareStatement("update sw_user set password=? where username=?"); ps.setString(1, setPassword); ps.setString(2, user.getUsername()); int count=ps.executeUpdate(); if(count==1) { return true; } } catch (SQLException e) { e.printStackTrace(); } DBUtil.closeAll(conn, ps, null); return false; }}
工具類的使用可以提高代碼的複用性,提高代碼閱讀性便於修改。
JDBC中 mysql資料庫的串連工具類 Java登入 及增刪改查 整理