生活随笔
收集整理的這篇文章主要介紹了
JDBC操作数据库的基本流程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
所有的JDBC應用程序都具有下面的基本流程: 1、加載數(shù)據(jù)庫驅(qū)動并建立到數(shù)據(jù)庫的連接。 2、執(zhí)行SQL語句。 3、處理結(jié)果。 4、從數(shù)據(jù)庫斷開連接釋放資源。
下面我們就來仔細看一看每一個步驟:
其實按照上面所說每個階段都可得單獨拿出來寫成一個獨立的類方法文件。共別的應用來調(diào)用。
1、加載數(shù)據(jù)庫驅(qū)動并建立到數(shù)據(jù)庫的連接:
[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);//這里是所謂的數(shù)據(jù)庫驅(qū)動的加載?? ????connection =(Connection)?DriverManager.getConnection(connectiionString);//這里就是建立數(shù)據(jù)庫連接?? ????System.out.println("數(shù)據(jù)庫連接成功");?? }?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);//這里是所謂的數(shù)據(jù)庫驅(qū)動的加載connection=(Connection) DriverManager.getConnection(connectiionString);//這里就是建立數(shù)據(jù)庫連接System.out.println("數(shù)據(jù)庫連接成功");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return connection;
2、執(zhí)行SQL語句:
?
在執(zhí)行sql語句的時候,這里常見的有兩種類型的語句對象:
Statement:它提供了直接在數(shù)據(jù)庫中執(zhí)行SQL語句的方法。對于那些只執(zhí)行一次的查詢、刪除或者一種固定的sql語句來說已經(jīng)足夠了。
[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:這種語句對象用于那些需要執(zhí)行多次,每次僅僅是數(shù)據(jù)取值不同的SQL語句,它還提供了一些方法,以便指出語句所使用的輸入?yún)?shù)。
[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、處理結(jié)果:
?
[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執(zhí)行sql語句的方法:insert、Update、delete語句是使用了Statement的executeUpdate方法執(zhí)行的,返回結(jié)果是插入、更新、刪除的個數(shù)。而select語句執(zhí)行較為特別是使用了Statement的executeQuery方法執(zhí)行的。返回的結(jié)果存放在resultset結(jié)果集中,我們可以調(diào)用next()方法來移到結(jié)果集中的下一條記錄。結(jié)果集由行和列組成,各列數(shù)據(jù)可以通過相應數(shù)據(jù)庫類型的一系列get方法(如getString,getInt,getDate等等)來取得。
?
4、從數(shù)據(jù)庫斷開連接釋放資源:
在結(jié)果集、語句和連接對象用完以后,我們必須正確地關閉它們。連接對象、結(jié)果集對象以及所有的語句對象都有close()方法,通過調(diào)用這個方法,我們可以確保正確釋放與特定數(shù)據(jù)庫系統(tǒng)相關的所有資源。
[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("數(shù)據(jù)庫關閉");?? ?????? ????}?? 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("數(shù)據(jù)庫關閉");}
轉(zhuǎn)載于:https://www.cnblogs.com/heartstage/p/3416601.html
總結(jié)
以上是生活随笔 為你收集整理的JDBC操作数据库的基本流程 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。