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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

JDBC操作数据库的基本流程

發布時間:2024/4/14 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDBC操作数据库的基本流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

所有的JDBC應用程序都具有下面的基本流程:
  1、加載數據庫驅動并建立到數據庫的連接。
  2、執行SQL語句。
  3、處理結果。
  4、從數據庫斷開連接釋放資源。

下面我們就來仔細看一看每一個步驟:

其實按照上面所說每個階段都可得單獨拿出來寫成一個獨立的類方法文件。共別的應用來調用。

1、加載數據庫驅動并建立到數據庫的連接:

[html] view plaincopyprint?
  • String?driverName="com.mysql.jdbc.Driver";??
  • String?connectiionString="jdbc:mysql://10.5.110.239:3306/test?"+"user=root&password=chen&characterEncoding=utf-8";??
  • Connection?connection=null;??
  • try?{??
  • ????Class.forName(driverName);//這里是所謂的數據庫驅動的加載??
  • ????connection=(Connection)?DriverManager.getConnection(connectiionString);//這里就是建立數據庫連接??
  • ????System.out.println("數據庫連接成功");??
  • }?catch?(ClassNotFoundException?e)?{??
  • ????//?TODO?Auto-generated?catch?block??
  • ????e.printStackTrace();??
  • }??
  • return?connection;??
  • String driverName="com.mysql.jdbc.Driver";String connectiionString="jdbc:mysql://10.5.110.239:3306/test?"+"user=root&password=chen&characterEncoding=utf-8";Connection connection=null;try {Class.forName(driverName);//這里是所謂的數據庫驅動的加載connection=(Connection) DriverManager.getConnection(connectiionString);//這里就是建立數據庫連接System.out.println("數據庫連接成功");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return connection;

    2、執行SQL語句:

    ?

    在執行sql語句的時候,這里常見的有兩種類型的語句對象:

    Statement:它提供了直接在數據庫中執行SQL語句的方法。對于那些只執行一次的查詢、刪除或者一種固定的sql語句來說已經足夠了。

    [html] view plaincopyprint?
  • Statement?statement=(Statement)?dUtil.getConnection().createStatement();??
  • ??????????????
  • ????????????String?sql="delete?from?diary?where?title="+"'"+title+"'";??
  • ??????????????
  • ????????????int?count=statement.executeUpdate(sql);??
  • ??????????????
  • ????????????System.out.println("刪除成功");??
  • Statement statement=(Statement) dUtil.getConnection().createStatement();String sql="delete from diary where title="+"'"+title+"'";int count=statement.executeUpdate(sql);System.out.println("刪除成功");

    ?

    Preparedstatement:這種語句對象用于那些需要執行多次,每次僅僅是數據取值不同的SQL語句,它還提供了一些方法,以便指出語句所使用的輸入參數。

    [html] view plaincopyprint?
  • String?sql="insert?into?diary(title,content,authorname,time)?values(?,?,?,now())";??
  • ????????try?{??
  • ????????????PreparedStatement?preparedStatement=(PreparedStatement)?dUtil.getConnection().prepareStatement(sql);??
  • ????????????String?title=diary.getTitle();??
  • ????????????String?content=diary.getContent();??
  • ????????????String?authorname=diary.getAuthorName();??
  • ????????????preparedStatement.setString(1,?title);??
  • ????????????preparedStatement.setString(2,?content);??
  • ????????????preparedStatement.setString(3,?authorname);??
  • String sql="insert into diary(title,content,authorname,time) values(?,?,?,now())";try {PreparedStatement preparedStatement=(PreparedStatement) dUtil.getConnection().prepareStatement(sql);String title=diary.getTitle();String content=diary.getContent();String authorname=diary.getAuthorName();preparedStatement.setString(1, title);preparedStatement.setString(2, content);preparedStatement.setString(3, authorname);

      3、處理結果:

    ?

    [html] view plaincopyprint?
  • ResultSet?resultSet=statement.executeQuery(sql);??
  • ????????????while?(resultSet.next())?{??
  • ????????????????Diary?diary=new?Diary();??
  • ????????????????diary.setAuthorName(resultSet.getString("authorname"));??
  • ????????????????diary.setContent(resultSet.getString("content"));??
  • ????????????????diary.setTitle(resultSet.getString("title"));??
  • ????????????????diary.setId(resultSet.getInt("id"));??
  • ????????????????Date?time=resultSet.getDate("time");??
  • ResultSet resultSet=statement.executeQuery(sql);while (resultSet.next()) {Diary diary=new Diary();diary.setAuthorName(resultSet.getString("authorname"));diary.setContent(resultSet.getString("content"));diary.setTitle(resultSet.getString("title"));diary.setId(resultSet.getInt("id"));Date time=resultSet.getDate("time");

    此處,應該知道的是:Statement執行sql語句的方法:insert、Update、delete語句是使用了Statement的executeUpdate方法執行的,返回結果是插入、更新、刪除的個數。而select語句執行較為特別是使用了Statement的executeQuery方法執行的。返回的結果存放在resultset結果集中,我們可以調用next()方法來移到結果集中的下一條記錄。結果集由行和列組成,各列數據可以通過相應數據庫類型的一系列get方法(如getString,getInt,getDate等等)來取得。

    ?

    4、從數據庫斷開連接釋放資源:

    在結果集、語句和連接對象用完以后,我們必須正確地關閉它們。連接對象、結果集對象以及所有的語句對象都有close()方法,通過調用這個方法,我們可以確保正確釋放與特定數據庫系統相關的所有資源。

    [html] view plaincopyprint?
  • public?static?void?closeConnection(ResultSet?resultSet,PreparedStatement?preparedStatement,?Connection?connection)?throws?SQLException?{??
  • ??????????
  • ????????if?(resultSet!=null)?resultSet.close();??
  • ????????if?(preparedStatement!=null)?preparedStatement.close();??
  • ????????if(connection!=null&&connection.isClosed()==false)?connection.close();??
  • ????????System.out.println("數據庫關閉");??
  • ??????
  • ????}??
  • public static void closeConnection(ResultSet resultSet,PreparedStatement preparedStatement, Connection connection) throws SQLException {if (resultSet!=null) resultSet.close();if (preparedStatement!=null) preparedStatement.close();if(connection!=null&&connection.isClosed()==false) connection.close();System.out.println("數據庫關閉");}

    轉載于:https://www.cnblogs.com/heartstage/p/3416601.html

    總結

    以上是生活随笔為你收集整理的JDBC操作数据库的基本流程的全部內容,希望文章能夠幫你解決所遇到的問題。

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