日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDBC详解系列之流程

發布時間:2024/4/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDBC详解系列之流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC概述定義:JDBC(Java DataBase Connectivity,java數據庫連接)是一種用于執行SQL語句的Java API, 可以為多種關系數據庫提供統一訪問,它由一組用Java語言編寫的類和接口組成。JDBC提供了一種基準, 據此可以構建更高級的工具和接口,使數據庫開發人員能夠編寫數據庫應用程序。JDBC是SUN公司提出來的一系列規范,但它只定義了接口規范,具體的實現則交給各個數據庫的廠商去做。 這類似以前的軟件要調用打印機的時候都要自己去給各種類型的打印機實現驅動,但微軟在操作系統定義了 打印機驅動接口,由各個打印機廠商去實現,而軟件供應商只需要調用接口即可(不然每個數據庫都要自己 去實現驅動,會累死的)。我想這也符合JAVA跨平臺的思想,實現“Write once, run anywhere!”。JDBC使用詳解/*** 如果你要使用我的代碼,在此之前請在mysql創建jdbc_test數據庫,并建立student表,兩個字段,name和age*/ public class JDBCTest {/*** 數據庫相關參數*///這是驅動名稱,此例子中我們加載的是mysql的驅動,在之前需要導入mysql的驅動jar包public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";//連接數據庫的url,各個數據庫廠商不一樣,此處為mysql的;后面是創建的數據庫名稱public static final String JDBC_URL = "jdbc:mysql://localhost:3306/jdbc_test";//連接數據庫所需賬戶名public static final String JDBC_USERNAME = "root";//用戶名對應的密碼,我的mysql密碼是123456public static final String JDBC_PASSWORD ="123456";public static void main(String[] args) {List<Student> students = new ArrayList<Student>();Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;try {//第一步:加載Driver類,注冊數據庫驅動Class.forName(JDBC_DRIVER);//第二步:通過DriverManager,使用url,用戶名和密碼建立連接(Connection)connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);//第三步:通過Connection,使用sql語句打開Statement對象;preparedStatement = connection.prepareStatement("select * from student where age =?");//傳入參數,之所以這樣是為了防止sql注入preparedStatement.setInt(1, 18);//第四步:執行語句,將結果返回resultSetresultSet = preparedStatement.executeQuery();//第五步:對結果進行處理while (resultSet.next()){String name = resultSet.getString("name");int age = resultSet.getInt("age");Student student = new Student();student.setAge(age);student.setName(name);students.add(student);}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally {//第六步:倒敘釋放資源resultSet-》preparedStatement-》connectiontry {if (resultSet!=null && !resultSet.isClosed()){resultSet.close();}} catch (SQLException e) {e.printStackTrace();}try {if(preparedStatement!=null && !preparedStatement.isClosed()){preparedStatement.close();}} catch (SQLException e) {e.printStackTrace();}try {if(connection!=null && connection.isClosed()){connection.close();}} catch (SQLException e) {e.printStackTrace();}}for (Student student:students) {System.out.println(student.getName()+"="+student.getAge());}} }以上就是JDBC查詢的基本連接過程,后續一些復雜的數據庫操作過程只不過是在上面進行一些增改而已。 大體步驟如注釋:JDBC流程: 第一步:加載Driver類,注冊數據庫驅動; 第二步:通過DriverManager,使用url,用戶名和密碼建立連接(Connection); 第三步:通過Connection,使用sql語句打開Statement對象; 第四步:執行語句,將結果返回resultSet; 第五步:對結果resultSet進行處理; 第六步:倒敘釋放資源resultSet-》preparedStatement-》connection。如果是刪除,修改和插入,使用executeUpdate()即可:/*** 如果你要使用我的代碼,在此之前請在mysql創建jdbc_test數據庫,并建立student表,兩個字段,name和age*/ public class JDBCTest {/*** 數據庫相關參數*///這是驅動名稱,此例子中我們加載的是mysql的驅動,在之前需要導入mysql的驅動jar包public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";//連接數據庫的url,各個數據庫廠商不一樣,此處為mysql的;后面是創建的數據庫名稱public static final String JDBC_URL = "jdbc:mysql://localhost:3306/jdbc_test";//連接數據庫所需賬戶名public static final String JDBC_USERNAME = "root";//用戶名對應的密碼,我的mysql密碼是123456public static final String JDBC_PASSWORD = "123456";@Testpublic void testUpdate() {Connection connection = null;PreparedStatement preparedStatement = null;try {//第一步:加載Driver類,注冊數據庫驅動Class.forName(JDBC_DRIVER);//第二步:通過DriverManager,使用url,用戶名和密碼建立連接(Connection)connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);//第三步:通過Connection,使用sql語句打開Statement對象;preparedStatement = connection.prepareStatement("UPDATE student SET age=20 WHERE name=?");//傳入參數,之所以這樣是為了防止sql注入preparedStatement.setString(1, "xiaoming");//第四步:執行語句,將結果返回resultSetint count = preparedStatement.executeUpdate();//第五步:對結果進行處理System.out.println(count);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} finally {//第六步:倒敘釋放資源resultSet-》preparedStatement-》connectiontry {if (preparedStatement != null && !preparedStatement.isClosed()) {preparedStatement.close();}} catch (SQLException e) {e.printStackTrace();}try {if (connection != null && connection.isClosed()) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}} }以及批量操作:@Test/*** 測試批量操作*/public void testBatch() {Connection connection = null;PreparedStatement preparedStatement = null;String insertSql = "insert into student values(?,?)";try {//第一步:加載Driver類,注冊數據庫驅動Class.forName(JDBC_DRIVER);//第二步:通過DriverManager,使用url,用戶名和密碼建立連接(Connection)connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);//第三步:通過Connection,使用sql語句打開Statement對象;preparedStatement = connection.prepareStatement(insertSql);//傳入參數,之所以這樣是為了防止sql注入for(int i=0;i<10;i++){preparedStatement.setString(1,100+i+"user");preparedStatement.setInt(2,100+i);preparedStatement.addBatch();}//第四步:執行語句,將結果返回resultSetint[] count = preparedStatement.executeBatch();//第五步:對結果進行處理System.out.println(count);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} finally {//第六步:倒敘釋放資源resultSet-》preparedStatement-》connectiontry {if (preparedStatement != null && !preparedStatement.isClosed()) {preparedStatement.close();}} catch (SQLException e) {e.printStackTrace();}try {if (connection != null && connection.isClosed()) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}}

?

總結

以上是生活随笔為你收集整理的JDBC详解系列之流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。