java:JDBC的使用与封装
生活随笔
收集整理的這篇文章主要介紹了
java:JDBC的使用与封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC連接過程:
1.導入相關的JDBC驅動jar包;
①:新建一個文件夾,命名為lib;
②:將jar包粘貼至此文件夾;
③:右鍵jar包,Add to Build Path;
④:完成。
2.注冊驅動,把mysql的Driver對象注冊到java.sql.Drivermanager中;
Class.forName("com.mysql.jdbc.Driver");3.建立連接;
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);4.SQL業務操作;
首先簡單介紹一下連接的數據庫book:
只有兩個表:user和bookmessage。
①:user
②:bookmessage
uid為外鍵,下面的業務操作只用到表bookmessage。
①:Statement;
(1)查詢操作:
Statement stmt = null;ResultSet rs = null;List<book> list = new ArrayList<book>();String sql ="select * from bookmessage";rs = stmt.executeQuery(sql);//查詢while(rs.next()) {int bid = rs.getInt(1);//bid數據類型為intString name = rs.getString(2);//bookname數據類型為Stringint uid = rs.getInt(3);//uid數據類型為int//System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3));直接打印表的信息;book b = new book(bid,name,uid);//構建book類打印表的信息list.add(b);//加入book類list}//打印for(int i=0;i<list.size();i++) {System.out.println(list.get(i).getBid()+"\t"+list.get(i).getBookname()+"\t\t"+list.get(i).getUid());}book類:
package JDBC;public class book {private int bid;private String bookname;private int uid;public book() {}public book(int bid,String bookname,int uid) {this.bid = bid;this.bookname = bookname;this.uid = uid;}public int getBid() {return bid;}public void setBid(int bid) {this.bid = bid;}public String getBookname() {return bookname;}public void setBookname(String bookname) {this.bookname = bookname;}public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;} }(2)刪除操作:
Statement stmt = null;String sql ="delete from bookmessage where bid =6";stmt = conn.createStatement();int v = stmt.executeUpdate(sql);//添加 修改 刪除時 返回受影響的行數;if(v>0) {System.out.println("success");}else {System.out.println("fail");}添加和修改就不一一進行演示了,接下來介紹PreparedStatement。
②:PreparedStatement。
PreparedStatement有預編譯功能,可以防止sql注入,安全性更高。
舉一個添加的例子:
String sql ="insert into bookmessage values(?,?,?)";int bid = 7;String bookname ="林清玄散文精選";int uid = 3;pstmt = conn.prepareStatement(sql);//給定參數位置從左到右依次為1,2,3,4 ~~pstmt.setInt(1,bid);//第一個?處設置參數bidpstmt.setString(2,bookname);//第二個?處設置參數booknamepstmt.setInt(3,uid);//第三個?處設置參數uidint v = pstmt.executeUpdate();if(v>0) {System.out.println("success");}else {System.out.println("fail");}5.關閉資源。
執行完sql業務后,關閉使用的資源,要進行判空。
if(null != rs) {rs.close();}if(null != stmt) {stmt.close();}if(null != pstmt) {pstmt.close();}if(null != conn) {conn.close();}JDBC封裝:
既然我們了解了JDBC連接的過程,那么我們大致可以將其分為三個部分:sql業務操作前為第一部分,準備部分,需要進行注冊和連接;sql業務操作為第二部分;最后一部分為關閉資源。那么除第二部分sql業務操作外,其余兩部分可以封裝在一個工具類中。
第一部分,準備:
注冊驅動可以放在靜態塊中,進行類的初始化:
static {try {Class.forName(DRIVERPATH);}catch(ClassNotFoundException e) {e.printStackTrace();}}獲取Connection對象:
public static Connection getConn() {synchronized(DBconUtil.class) {try {if(null == conn || conn.isClosed()) {synchronized(DBconUtil.class) {conn = DriverManager.getConnection(URL,USER,PASSWORD);}}}catch(SQLException e) {e.printStackTrace();}}return conn;}關閉資源:
用stmt可以把pstmt的位置填null,用pstmt可以把stmt的位置填null。
public static void close(Connection con,PreparedStatement pstmt,Statement stmt,ResultSet rs) {try {if(null != rs) {rs.close();}}catch(SQLException e) {e.printStackTrace();}finally {try {if(null != stmt) {stmt.close();}}catch(SQLException e) {e.printStackTrace();}finally {try {if(null != pstmt) {pstmt.close();}}catch(SQLException e) {e.printStackTrace();}finally {//關閉資源try {if(null != conn) {conn.close();}}catch(SQLException e) {e.printStackTrace();}}}}}DBconUtil類:
最后附上封裝類,整理不易。
package JDBC;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;public class DBconUtil {private static final String URL = "jdbc:mysql://127.0.0.1:3306/book";private static final String USER = "root";private static final String PASSWORD = "123456";private static final String DRIVERPATH = "com.mysql.jdbc.Driver";private static Connection conn = null;Statement stmt = null;static {try {Class.forName(DRIVERPATH);}catch(ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConn() {synchronized(DBconUtil.class) {try {if(null == conn || conn.isClosed()) {synchronized(DBconUtil.class) {conn = DriverManager.getConnection(URL,USER,PASSWORD);}}}catch(SQLException e) {e.printStackTrace();}}return conn;}public static void close(Connection con,PreparedStatement pstmt,Statement stmt,ResultSet rs) {try {if(null != rs) {rs.close();}}catch(SQLException e) {e.printStackTrace();}finally {try {if(null != stmt) {stmt.close();}}catch(SQLException e) {e.printStackTrace();}finally {try {if(null != pstmt) {pstmt.close();}}catch(SQLException e) {e.printStackTrace();}finally {//關閉資源try {if(null != conn) {conn.close();}}catch(SQLException e) {e.printStackTrace();}}}}} }總結
以上是生活随笔為你收集整理的java:JDBC的使用与封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity让图片动起来
- 下一篇: Hash一致性算法(分片机制)