標籤:
java提供了一系列用於資料庫編程的API--JDBC;實際上,是通過介面定義了一套訪問資料庫的規範,而實現這些規範的是各資料庫廠商,並且將介面實現封裝成.jar包;而我們程式員只需要得到資料庫廠商封裝的.jar包,調用JDBC API就可以串連對應的資料庫.其中調用調用JDBC API這個動作是統一的.
JDBC中常見的API :
1.DriverManager類 - 驅動管理器
2.Connection類 - 串連程式與資料庫
3.Statement類 - SQL語句類
4.PrepareStatement類 - 此類是Statement類的子類
5.ResultSet類 - 結果集,Result是結果的意思,Set是集合
具體操作如下:
//1、載入驅動---JDK1.8可以省略這個代碼try {Class.forName("com.mysql.jdbc.Driver");//反射代碼---類載入} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}//2、擷取串連Connection con = null;String url = "jdbc:mysql://localhost:3306/test129?useSSL=true&useUnicode=true&characterEncoding=utf8";//協議://ip地址:連接埠號碼/資料庫名String username = "root";String password = "root";try {con = DriverManager.getConnection(url,username,password);//串連資料庫需要三個參數:url/帳號/密碼//3、構建語句對象String sql = "";//書寫sql語句Statement state = con.createStatement();//通過串連擷取語句對象//4、語句對象執行SQLint r = state.executeUpdate(sql);//所有的DML語句都調用這個方法,該方法預設返回影響了多少行/* * 新增資料時,有時候會需要得到自動產生的主索引值. int r = state.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);ResultSet rs = state.getGeneratedKeys();if(rs.next()){int id = rs.getInt(1);}*/} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{//5、關閉串連if(con != null){try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
以上是串連MySQL資料庫以及對其中表格進行增/刪/改(DML)操作,而要進行查詢(DQL)操作只需對代碼進行如下改動:
String sql = "select * from t_group";Statement state = con.createStatement();ResultSet rs = state.executeQuery(sql);//DQL語句通用這個方法while(rs.next()){GroupBean gb = new GroupBean();gb.setId( rs.getInt("pk_groupid"));gb.setGroupName(rs.getString("f_groupName"));gb.setGroupAddress(rs.getString("f_groupAddress"));gb.setGroupNum(rs.getInt("f_groupNum"));allGroup.add(gb);}
串連和關閉不變,變的是sql語句,查詢語句返回的資料也是不同的,是以結果集ResultSet 的形式返回ResultSet 有一個.next()方法--探查是否還有下一條資訊,返回boolean值,可以利用迴圈將查詢結果裝入與資料表格對應的JavaBean對象中.
從設計上資料庫中表與表的關係和java中bean對象與bean的關係是相對應的.最常見的有三種關係:
| |
表 |
Bean |
| 一對一 |
兩張表的關係是其中一張表的外部索引鍵關聯另一張表,外鍵必須有唯一約束;也可以是兩張表互相外部索引鍵關聯,加唯一約束 |
兩個類,其中一個類has a另一個類;或者互相has |
| 一對多 |
一張主表,多張從表,所有的外鍵都是在多的那一方的 |
單一的一方has a多方的集合,多方has a一個單方 |
| 多對多 |
兩張(或多張)主表,一張關係表所有的外鍵都在關係表上 |
兩個類互相has a 對方類型的集合 |
具體的關係要視業務而定;
很多時候需要用使用者輸入內容來構建sql語句,這時就會遇到sql注入的風險,可以在此事使用PrepareStatement類代替Statement類來裝sql語句,假如有一個使用者表格:
String sql = "select * from t_user where f_username = ? and f_password = ?";//PrepareStatement的特點,將標準的sql模版提前書寫好,將未知的部分用?號代替PreparedStatement ps = con.prepareStatement(sql);//先將模版傳入PrepareStatement構造方法產生對象ps.setString(1, inputUsername);//傳入問號的位置,和取代?的語句,在這裡第一個?的位置是1,而不是0ps.setString(2, inputPassword);ResultSet rs = ps.executeQuery();//將完整的sql語句提交UserBean user = null;while(rs.next()){user = new UserBean();user.setId(rs.getInt("pk_id"));user.setUsername(rs.getString("f_username"));user.setPassword(rs.getString("f_password"));}
事務 -- 由於某一業務,需要同時執行多條DML語句,一起成功,一起失敗:
try {con = DriverManager.getConnection(url,username,password); //1、將連線物件的自動認可設定為假 con.setAutoCommit(false); //在此編寫sql語句 con.commit();//2、手動提交 }catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();try {con.rollback();//3、復原,如果有一條失敗,就好全部回到修改前的樣子} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} } finally{//5、關閉串連if(con != null){try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }
JDBC串連MySQL