JDBC操作數(shù)據(jù)庫(kù)的增加、刪除、更新、查找實(shí)例分析
更新時(shí)間:2015年10月19日 14:11:32 作者:煙大洋仔
這篇文章主要介紹了JDBC操作數(shù)據(jù)庫(kù)的增加、刪除、更新、查找方法,以完整實(shí)例形式分析了Java基于JDBC連接數(shù)據(jù)庫(kù)及進(jìn)行數(shù)據(jù)的增刪改查等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了JDBC操作數(shù)據(jù)庫(kù)的增加、刪除、更新、查找方法。分享給大家供大家參考,具體如下:
package cn.com.JDBC; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class CRUD { public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub //create(); //update(); delete(); read(); } static void delete() throws SQLException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計(jì)模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語(yǔ)句 st=conn.createStatement(); //4.執(zhí)行語(yǔ)句 String sql="delete from user where id>5"; int i=st.executeUpdate(sql); System.out.println("i="+i); } finally { JdbcUtils.free(resultset, st, conn); } } static void update() throws SQLException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計(jì)模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語(yǔ)句 st=conn.createStatement(); //4.執(zhí)行語(yǔ)句 String sql="update user set money=money+20"; int i=st.executeUpdate(sql); System.out.println("i="+i); } finally { JdbcUtils.free(resultset, st, conn); } } static void create() throws SQLException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計(jì)模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語(yǔ)句 st=conn.createStatement(); //4.執(zhí)行語(yǔ)句 String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')"; int i=st.executeUpdate(sql); System.out.println("i="+i); } finally { JdbcUtils.free(resultset, st, conn); } } static void read() throws SQLException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立連接 conn=JdbcUtils.getConnection(); //單例設(shè)計(jì)模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.創(chuàng)建語(yǔ)句 st=conn.createStatement(); //4.執(zhí)行語(yǔ)句 resultset=st.executeQuery("select id,name,birthday,money from user"); //5.處理結(jié)果 while(resultset.next()) { System.out.println(resultset.getObject("id")); System.out.println(resultset.getObject("name")); System.out.println(resultset.getObject("birthday")); System.out.println(resultset.getObject("money")); } } finally { JdbcUtils.free(resultset, st, conn); } } } package cn.com.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtils { private static String url="jdbc:mysql://localhost:3306/jdbc"; private static String user="root"; private static String password="123"; private JdbcUtils() { } static { try { Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public static void free(ResultSet resultset,Statement st,Connection conn) { //6.釋放資源 try{ if(resultset!=null) resultset.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(st!=null) st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- jsp+jdbc實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)的方法
- JSP中使用JDBC訪問(wèn)SQL Server 2008數(shù)據(jù)庫(kù)示例
- 加快JDBC設(shè)計(jì)中JSP訪問(wèn)數(shù)據(jù)庫(kù)
- jdbc操作mysql數(shù)據(jù)庫(kù)實(shí)例
- JDBC鏈接數(shù)據(jù)庫(kù)的幾個(gè)步驟
- JDBC對(duì)MySQL數(shù)據(jù)庫(kù)布爾字段的操作方法
- Java使用JDBC連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- JSP基于JDBC的數(shù)據(jù)庫(kù)連接類(lèi)實(shí)例
相關(guān)文章
Java虛擬機(jī)調(diào)用Java主類(lèi)的main()方法
這篇文章主要介紹了Java虛擬機(jī)調(diào)用Java主類(lèi)的main()方法,前一篇文章我們介紹了關(guān)于Java虛擬機(jī)HotSpot2021-11-11linux環(huán)境下java程序打包成簡(jiǎn)單的hello world輸出jar包示例
這篇文章主要介紹了linux環(huán)境下java程序打包成簡(jiǎn)單的hello world輸出jar包,結(jié)合簡(jiǎn)單hello world輸出程序示例分析了Linux環(huán)境下的java可執(zhí)行jar包文件的生成相關(guān)操作技巧,需要的朋友可以參考下2019-11-11詳解Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性
Spring Boot項(xiàng)目中常有一些相對(duì)穩(wěn)定的參數(shù)設(shè)置項(xiàng),其作用范圍是系統(tǒng)級(jí)的或模塊級(jí)的,這些參數(shù)稱(chēng)為系統(tǒng)參數(shù)。這些變量以參數(shù)形式進(jìn)行配置,從而提高變動(dòng)和擴(kuò)展的靈活性,保持代碼的穩(wěn)定性2021-06-06