經過 Statement 執行 ,實際上是拼接 sql 語句的。 先拼接 sql 語句,而後在一塊兒執行。 java
package com.zze.test; import java.sql.*; public class Test1 { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { // 註冊驅動 Class.forName("com.mysql.jdbc.Driver"); // 創建鏈接:url:協議+訪問的數據庫 user:用戶名 password:密碼 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/1221?serverTimezone=GMT&characterEncoding=utf8", "root", "root"); // 建立statement statement = connection.createStatement(); // 執行查詢 String sql = "select * from user"; resultSet = statement.executeQuery(sql); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println(String.format("%s:%s", id, name)); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } finally { resultSet = null; } try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { statement = null; } try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } finally { connection = null; } } } }
相比較之前的 Statement, 預先處理給定的 sql 語句,對其執行語法檢查。 在 sql 語句裏面使用 ? 佔位符來替代後續要傳遞進來的變量。 後面進來的變量值,將會被當作是字符串,不會產生任何的關鍵字。mysql
package com.zze.test; import java.sql.*; public class Test3 { public static void main(String[] args) { Connection connection = null; PreparedStatement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT&characterEncoding=utf8", "root", "root"); String sql = "insert into user (name) values (?)"; statement = connection.prepareStatement(sql); statement.setString(1, "王五"); int flag = statement.executeUpdate(); System.out.println(flag); if (flag > 0) { System.out.println("添加成功"); } else { System.out.println("添加失敗"); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { statement = null; } try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } finally { connection = null; } } } }
package com.zze.util; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JDBCUtil { private static String driverName = null; private static String url = null; private static String user = null; private static String password = null; static { try { // 建立配置文件對象 Properties properties = new Properties(); // 使用類加載器讀取src下的資源文件 InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); // 讀取配置文件 properties.load(resourceAsStream); // 讀取屬性 driverName = properties.getProperty("driverName"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取鏈接 * * @return 鏈接對象 */ public static Connection getConnection() { try { // 註冊驅動 com.mysql.cj.jdbc.Driver 中有註冊的靜態代碼塊 Class.forName(driverName); // 創建鏈接:url:協議+訪問的數據庫 user:用戶名 password:密碼 Connection connection = DriverManager.getConnection(url, user, password); return connection; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 釋放鏈接資源 */ public static void release(Connection connection, Statement statement, ResultSet resultSet) { if (resultSet != null) closeResultSet(resultSet); if (statement != null) closeStatement(statement); if (connection != null) closeConnection(connection); } private static void closeResultSet(ResultSet rs) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { rs = null; } } private static void closeStatement(Statement st) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { st = null; } } private static void closeConnection(Connection conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn = null; } } }
driverName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/1221?serverTimezone=GMT user=root password=root
package com.zze.test; import com.zze.util.JDBCUtil; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class Test2 { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = JDBCUtil.getConnection(); // 建立statement statement = connection.createStatement(); // 執行查詢 String sql = "select * from user"; resultSet = statement.executeQuery(sql); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println(String.format("%s:%s", id, name)); } } catch (Exception ex) { ex.printStackTrace(); } finally { JDBCUtil.release(connection, statement, resultSet); } } }
連接:https://pan.baidu.com/s/1iJJ8l-xZ1KgKYuMAiXtxiQ 提取碼:pj53