JDBC实现数据库的增删改查

186 阅读1分钟
话不多说,直接上代码!


1. 加载驱动 

 Class.forName(driver); //driver为驱动的全类名 


2. 创建连接 

 conn = DriverManager.getConnection(url,user,password); //数据库的URL,用户名和密 


3. 创建语句对象 

 stm = conn.createStatement(); //也可以使用preparedStatement 


4. 执行SQL语句 

 String sql = "DELETE FROM book WHERE book_id = " + bookId; int executeUpdate = stm.executeUpdate(sql); 


5. 处理结果 

 return executeUpdate; 


6. 释放资源 

 try{ 

 stm.close(); 

}catch(SQLException e){ 

 e.printStackTrace(); 

}finally{ 

 try{ conn.close(); 

 }catch(SQLException e){ 

 e.printStackTrace(); 

 };


下面是作者的一个JAVA面试经验的分享号,希望大家多多支持,谢谢!!!