JDBC中的statement對象用于向數據庫發送SQL語句,想完成對數據庫的增刪改查,只需要通過這個對象向數據庫發送增刪改查語句即可。
Statement對象的executeUpdate方法,用于向數據庫發送增、刪、改的sql語句,executeUpdate執行完成后,將會返回一個整數(即增刪改語句導致了數據庫幾行數據發生了變化)。
Statement.executeQuery方法用于向數據庫發送查詢語句,executeQuery方法返回代表查詢結果的ResultSet對象。
CRUD操作-create
?使用executeUpdate(String sql)方法完成數據添加操作,示例操作:
Statement st = conn.createStatement();
String sql = "insert into user(... .) values(... . .)"
int num = st.executeUpdate(sql);
if(num > 0){System.out.println("插入成功!");
}
CRUD操作-delete
?使用executeUpdate(String sql)方法完成數據刪除操作,示例操作:
Statement st = conn.createStatement();
String sql = "delete from user where id=1"
int num = st.executeUpdate(sql);
if(num > 0){System.out.println("刪除成功!");
}
CRUD操作-update
?使用executeUpdate(String sql)方法完成數據修改操作,示例操作:
Statement st = conn.createStatement();
String sql = "update user set name='' where name=''"
int num = st.executeUpdate(sql);
if(num > 0){System.out.println("修改成功!");
}
CRUD操作-read
使用executeQuery(String sql)方法完成數據查詢操作,示例操作:
Statement st = conn.createStatement();
String sql = "select * from user where id=1"
ResultSet rs = st.executeQuery(sql);
while(rs.next()){// 根據獲取列的數據類型,分別調用rs的相應方法映射到java對象中
}
代碼實現
1. 提取工具類
public class jdbcUtils {private static String driver = null;private static String url = null;private static String username = null;private static String password = null;static {try {InputStream in = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");// 獲取具體資源Properties properties = new Properties();properties.load(in);driver = properties.getProperty("driver");url = properties.getProperty("url");username = properties.getProperty("username");password = properties.getProperty("password");// 1.驅動只用加載一次Class.forName(driver);} catch (IOException | ClassNotFoundException e) {throw new RuntimeException(e);}}// 獲取連接public static Connection getConnection() throws SQLException {return DriverManager.getConnection(url,username,password);}// 釋放連接資源public static void release(Connection conn, Statement stmt, ResultSet rs) throws SQLException {if (rs!=null){rs.close();}if (stmt!=null){stmt.close();}if (conn!=null){conn.close();}}}
2. 編寫增刪改的方法,executeUpdate
增:
public class TestInsert {public static void main(String[] args) throws SQLException {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection(); // 獲取數據庫連接st = conn.createStatement(); // 獲得SQL的執行對象String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,`email`,`birthday`)" +"VALUES(4,'chenyang','123456','888@qq.com','2024-7-5')";int i = st.executeUpdate(sql);if (i>0){System.out.println("插入成功!");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs);}}
}
刪:
public class TestDelete {public static void main(String[] args) throws SQLException {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();st = conn.createStatement();String sql = "DELETE FROM users WHERE id=4";int i = st.executeUpdate(sql);if (i>0){System.out.println("刪除成功!");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs);}}
改:
public class TestUpdate {public static void main(String[] args) throws SQLException {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();st = conn.createStatement();String sql = "UPDATE users SET `NAME`='chenyang8' WHERE id=4";int i = st.executeUpdate(sql);if (i>0){System.out.println("修改成功!");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs);}}
}
3. 查詢,executeQuery?
public class TestSelect {public static void main(String[] args) throws SQLException {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();st = conn.createStatement();//SQlString sql = "select * from users where id = 1";rs = st.executeQuery(sql); //查詢完畢會返回一個結果集if (rs.next()){System.out.println(rs.getString("NAME"));}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs);}}
}