前面複習了一些基礎知識以及如何抽取一些經常使用的代碼,接下來就結合junit4作一個增刪改查的小demojava
重點是這麼幾個步驟:1.建立鏈接 2.編寫sql語句 3.編寫sql語句的載體 4.若是是PreparedStatement的話要設置佔位符 5.執行sql語句 6.其餘mysql
在碼代碼過程當中,發現本身規範引錯了,正確的應該引java.sql下的,我引成了java.mysql.jdbc包下的web
此外,發現本身在建立sql載體的時候不熟練,忘記了。對於Statement的話,建立載體是 stmt = conn.createStatement() sql
對於PreparedStatement的話,建立載體是 pst= conn.prepareStatement(); ***注意不要加了個d,這裏是prepare數據庫
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; SET FOREIGN_KEY_CHECKS = 1;
package JDBC溫習; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 提供獲取鏈接和釋放資源的 方法 */ public class JDBCUtils { /* * 獲取鏈接方法 */ public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return conn; } /* * 釋放資源 */ public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package JDBC溫習; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; /* * 測試類 */ public class test { /* * 添加用戶 */ @Test public void add() { Connection conn = null; PreparedStatement pst = null; try { // 1.獲取鏈接 conn = (Connection) JDBCUtils.getConnection(); // 2.編寫sql語句(採用PreparedStatement) String sql = "insert into student values(?,?,?) "; // 3.獲取執行sql語句的載體 pst = conn.prepareStatement(sql); // 4.設置佔位符 pst.setInt(1, 6); pst.setString(2, "周東"); pst.setString(3, "男"); // 5.執行插入操做 int updateRow = pst.executeUpdate(); if (updateRow > 0) { System.out.println("插入成功"); } else { System.out.println("插入失敗"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { JDBCUtils.release(conn, pst, null); } } /* * 刪除用戶 */ @Test public void deleteById() { Connection conn = null; Statement stmt = null; try { // 1. 獲取鏈接 conn = JDBCUtils.getConnection(); // 2. 編寫sql語句(採用Statement方式) String sql = "delete from student where id=2 "; // 3. 獲取sql語句載體 stmt = conn.createStatement(); // 4. 執行sql語句 int updateRow = stmt.executeUpdate(sql); if (updateRow > 0) { System.out.println("刪除成功"); } else { System.out.println("刪除失敗"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { // 這裏由於我工具類提供的是preparedStatement的關閉. JDBCUtils.release(conn, null, null); } } /* * 修改用戶 */ @Test public void updateById() { Connection conn = null; PreparedStatement pst = null; try { // 1. 獲取鏈接 conn = JDBCUtils.getConnection(); // 2. 編寫sql語句(PreparedStatement方式) String sql = "update student set name=? , sex=? where id=? "; // 3. 建立sql載體 pst = conn.prepareStatement(sql); // 4. 設置佔位符 pst.setString(1, "小花"); pst.setString(2, "女"); pst.setInt(3, 4); // 5. 執行sql語句 int updateRow = pst.executeUpdate(); if (updateRow > 0) { System.out.println("更新成功"); } else { System.out.println("更新失敗"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { JDBCUtils.release(conn, pst, null); } } /* * 查詢用戶 PS:簡單查詢,根據ID查某個學生 */ @Test public void findById() { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 1. 獲取鏈接 conn = JDBCUtils.getConnection(); // 2.編寫sql語句 String sql = "select * from student where id = 6 "; // 3.編寫sql載體 stmt = conn.createStatement(); //4.執行sql語句 rs = stmt.executeQuery(sql); //5. 遍歷 while(rs.next()) { System.out.println(rs.getString(2)+"====="+rs.getString("sex")); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { JDBCUtils.release(conn, null, rs); } } }
以上是JDBC方面知識點的簡單回顧工具