乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JDBC

       收藏小管 2017-07-17

      JDBC(Java Data Base Connection)的作用是連接數(shù)據(jù)庫

       

      先看下jdbc連接SQLServer數(shù)據(jù)庫的簡單例子

      代碼實(shí)現(xiàn)(FirstJDBC):

       

      1. package com.jdbc;  
      2.   
      3. import java.sql.Connection;  
      4. import java.sql.DriverManager;  
      5. import java.sql.ResultSet;  
      6. import java.sql.Statement;  
      7.   
      8.   
      9. public class FirstJDBC {  
      10.       
      11.     public static void main(String[] args)  
      12.     {  
      13.         //調(diào)用連接數(shù)據(jù)庫的操作  
      14.         Connection con = createConnection();      
      15.           
      16.                   
      17.     }  
      18.       
      19.     /** 
      20.      * JDBC 建立 SQL Server數(shù)據(jù)庫連接 
      21.      */  
      22.     private static Connection createConnection() {  
      23.           
      24.         //定義加載驅(qū)動(dòng)程序  
      25.         String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
      26.           
      27.         //定義 連接 服務(wù)器 和 數(shù)據(jù)庫sample  
      28.         String dbURL = "jdbc:sqlserver://localhost:1433; DataBaseName = sample1" ;  
      29.       
      30.         //默認(rèn)用戶名,不要用windows默認(rèn)身份驗(yàn)證  
      31.         String userName = "sa" ;   
      32.         String userPassword = "zhichao" ;  
      33.         Connection connection = null ;  
      34.         Statement sta = null ;  
      35.           
      36.         try {  
      37.             //正式加載驅(qū)動(dòng)  
      38.             Class.forName(driverName);  
      39.             //開始連接  
      40.             connection = DriverManager.getConnection(dbURL, userName, userPassword);  
      41.             System.out.println("Connection Success !");  
      42.               
      43.             //向數(shù)據(jù)庫中執(zhí)行SQL語句  
      44.             sta = connection.createStatement() ;  
      45.             ResultSet rs = sta.executeQuery("SELECT id,name,height From Table_1");  
      46.             while(rs.next())  
      47.             {  
      48.                 int id = rs.getInt("id");  
      49.                 String name = rs.getString("name");  
      50.                 float height = rs.getFloat("height");  
      51.                   
      52.                 System.out.println("id = "+id+" name = "+name+" height = "+height);  
      53.             }  
      54.           
      55.         } catch (Exception e) {  
      56.               
      57.             System.out.println("Connection Fail !");  
      58.             e.printStackTrace() ;  
      59.         }  
      60.           
      61.         /** 
      62.          * 關(guān)閉數(shù)據(jù)庫 
      63.          * @param connection 
      64.          */  
      65.         finally  
      66.         {  
      67.             try {  
      68.                   
      69.                 if (null != sta)  
      70.                 {  
      71.                     sta.close() ;  
      72.                     sta = null;  
      73.                     System.out.println("Statement 關(guān)閉成功");  
      74.                 }  
      75.                   
      76.                 if (null != connection)  
      77.                 {  
      78.                     connection.close() ;  
      79.                     connection = null;  
      80.                     System.out.println("Connection 關(guān)閉成功");  
      81.                 }  
      82.                   
      83.             } catch (Exception e) {  
      84.                   
      85.                 e.printStackTrace() ;  
      86.             }         
      87.               
      88.         }         
      89.         return connection ;  
      90.     }  
      91. }  

       

      小結(jié):

          要寫一個(gè)jdbc程序,先要加載相應(yīng)數(shù)據(jù)庫的驅(qū)動(dòng)程序,驅(qū)動(dòng)程序最好放在你建的工程里面,可以在你的工程下面建一個(gè) lib文件夾以存儲外部的jar文件,這樣的話把你的工程拷貝到別的計(jì)算機(jī)運(yùn)行,仍能成功執(zhí)行。

       

      jdbc代碼一般步驟:

      1)加載外部驅(qū)動(dòng)程序(jar包)

      2)正式加載驅(qū)動(dòng)程序 (Class.forName(driverName) )

      3)獲取connection連接 (在jdk中的sql包中,只提供了一個(gè)類那就是DriverManeger,通過調(diào)用它的靜態(tài)方法getConnection(),可以得到以數(shù)據(jù)庫的連接

      4)創(chuàng)建sql語句的聲明(Statement),執(zhí)行sql語句(查詢),遍歷結(jié)果集

      5)關(guān)閉數(shù)據(jù)庫連接(一般用finally{}來處理,或者調(diào)用方法的形式來完成,關(guān)閉之前先判斷你要關(guān)閉的對象連接是否為空,如果空那會拋異常,所以先判斷)

       

      ------------------------------------- ------------------------------------- ------------------------Data Access Objects-------------------- ------------------------------------------- ---------------------------

      使用 DAO模式 來對數(shù)據(jù)庫做增刪改查操作


      這種模式可以大概分為三個(gè)層:1.DAO層  2.服務(wù)層  3.表現(xiàn)層

      1)表現(xiàn)層 :相當(dāng)于客戶端用來查看,提交信息的角色

      2)服務(wù)層 :是表現(xiàn)層和DAO層的紐帶,其實(shí)也沒干什么事就是通知消息的角色

      3)DAO   :真正要做事的角色(對數(shù)據(jù)庫的某些操作)

       

      舉個(gè)生活中的例子:

      就好比你去餐廳吃飯,你充當(dāng)一個(gè) (表現(xiàn)層)的角色,然后有美女服務(wù)員(服務(wù)層),問你需要吃什么東西,給你下一張訂單,讓你填。之后服務(wù)員把訂單傳到 廚師(DAO層)那里,具體操作廚師會搞定,一段時(shí)間后廚師把做好的食物傳給服務(wù)員,服務(wù)員把食物在傳給客戶,這些操作就算基本完成了。

       

      執(zhí)行順序: 表現(xiàn)層-->服務(wù)層-->DAO層-->返回服務(wù)層-->返回表現(xiàn)層

       

      來看看實(shí)現(xiàn)DAO模式的UML圖:

      代碼實(shí)現(xiàn):

      1.Bean文件,在這主要作用(有點(diǎn)像中介存儲的角色):當(dāng)從數(shù)據(jù)庫拿出數(shù)據(jù)后,一個(gè)個(gè)set到該類里,進(jìn)行賦值,然后把該對象放到集合中,之后再get出來

       

      Student.Java

      1. package com.myjdbc.bean;  
      2.   
      3. public class Student {  
      4.       
      5.     private Integer stuId;  
      6.     private String stuName ;  
      7.     private Integer stuAge;  
      8.     private String stuTel ;  
      9.     private String stuAddress ;  
      10.     private Integer groupId;  
      11.       
      12.     public Integer getStuId() {  
      13.         return stuId;  
      14.     }  
      15.     public void setStuId(Integer stuId) {  
      16.         this.stuId = stuId;  
      17.     }  
      18.     public String getStuName() {  
      19.         return stuName;  
      20.     }  
      21.     public void setStuName(String stuName) {  
      22.         this.stuName = stuName;  
      23.     }  
      24.     public Integer getStuAge() {  
      25.         return stuAge;  
      26.     }  
      27.     public void setStuAge(Integer stuAge) {  
      28.         this.stuAge = stuAge;  
      29.     }  
      30.     public String getStuTel() {  
      31.         return stuTel;  
      32.     }  
      33.     public void setStuTel(String stuTel) {  
      34.         this.stuTel = stuTel;  
      35.     }  
      36.     public String getStuAddress() {  
      37.         return stuAddress;  
      38.     }  
      39.     public void setStuAddress(String stuAddress) {  
      40.         this.stuAddress = stuAddress;  
      41.     }  
      42.     public Integer getGroupId() {  
      43.         return groupId;  
      44.     }  
      45.     public void setGroupId(Integer groupId) {  
      46.         this.groupId = groupId;  
      47.     }  
      48.           
      49. }  


      2.java連接數(shù)據(jù)庫的基本操作及關(guān)閉,封裝在一個(gè)類中

       

      JDBCUtils.java

      1. package com.myjdbc.utils;  
      2.   
      3. import java.sql.Connection;  
      4. import java.sql.DriverManager;  
      5. import java.sql.ResultSet;  
      6. import java.sql.Statement;  
      7.   
      8. public class JDBCUtils {  
      9.     /** 
      10.      * 獲取連接 
      11.      *  
      12.      */  
      13.     public static Connection getConnection()  
      14.     {  
      15.         String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
      16.   
      17.         String url = "jdbc:sqlserver://localhost:1433; DataBaseName = studentManager";  
      18.         String user = "sa" ;  
      19.         String password = "zhichao";  
      20.         Connection con = null ;  
      21.         try {  
      22.               
      23.             Class.forName(driverName);  
      24.             con = DriverManager.getConnection(url, user, password);  
      25.             System.out.println("success");  
      26.         } catch (Exception e) {  
      27.             e.printStackTrace();  
      28.         }  
      29.           
      30.         return con ;  
      31.           
      32.     }  
      33.       
      34.     /** 
      35.      * 關(guān)閉連接 
      36.      */  
      37.     public static void free(ResultSet rs, Statement sta , Connection con)  
      38.     {  
      39.         try {  
      40.             if(null != rs)  
      41.             {  
      42.                 rs.close();  
      43.                 rs = null ;  
      44.             }  
      45.               
      46.             if(null != sta)  
      47.             {  
      48.                 sta.close();  
      49.                 sta = null ;  
      50.             }  
      51.               
      52.             if(null != con)  
      53.             {  
      54.                 con.close();  
      55.                 con = null ;  
      56.             }  
      57.               
      58.         } catch (Exception e) {  
      59.             e.printStackTrace();  
      60.         }  
      61.     }  
      62. }  


       

      3.定義一個(gè)DAO接口

       

      StudentDAO.java

      1. package com.myjdbc.dao;  
      2.   
      3. import java.util.Set;  
      4.   
      5. import com.myjdbc.bean.Student ;  
      6.   
      7. public interface StudentDAO {  
      8.       
      9.     public int addStudent(Student student) ;  
      10.       
      11.     public int deleteStudent(String name);  
      12.       
      13.     public int updateStudent(String name);  
      14.       
      15.     public Student findStudent(String name);  
      16.       
      17.     public Set<Student> findAll();  
      18.   
      19.       
      20.       
      21.       
      22.       
      23.   
      24. }  


      4.實(shí)現(xiàn)DAO接口的類,具體DAO,做重要工作的類

      ConcreteStudentDao.java

      1. package com.myjdbc.dao;  
      2.   
      3. import java.sql.Connection;  
      4. import java.sql.PreparedStatement;  
      5. import java.sql.ResultSet;  
      6. import java.sql.SQLException;  
      7. import java.util.HashSet;  
      8. import java.util.Set;  
      9.   
      10. import com.myjdbc.bean.Student;  
      11. import com.myjdbc.dao.StudentDAO;  
      12. import com.myjdbc.utils.JDBCUtils;  
      13.   
      14. public class ConcreteStudentDao implements StudentDAO{  
      15.       
      16.     //增加一個(gè)學(xué)生  
      17.     public int addStudent(Student student)  
      18.     {  
      19.         Connection con = null ;  
      20.         PreparedStatement ps = null ;  
      21.         int i = 0 ;  
      22.         try  
      23.         {  
      24.             con = JDBCUtils.getConnection();  
      25.             String sql = "insert into student(stuName,stuAge,stuTel,stuAddress,groupId) values(?,?,?,?,?)";  
      26.             ps = con.prepareStatement(sql);  
      27.               
      28.             ps.setString(1, student.getStuName());  
      29.             ps.setInt(2, student.getStuAge());  
      30.             ps.setString(3, student.getStuTel());  
      31.             ps.setString(4, student.getStuAddress());  
      32.             ps.setInt(5, student.getGroupId());  
      33.               
      34.             i = ps.executeUpdate() ;  
      35.               
      36.         }  
      37.         catch(SQLException e)  
      38.         {  
      39.             throw new DAOException(e.getMessage(),e);  
      40.         }  
      41.         finally  
      42.         {  
      43.             JDBCUtils.free(null, ps, con);  
      44.         }  
      45.         return i;  
      46.     }  
      47.       
      48.     //刪除一個(gè)學(xué)生  
      49.     public int deleteStudent(String name)  
      50.     {  
      51.         Connection con = null ;  
      52.         PreparedStatement ps = null ;  
      53.         int i = 0 ;  
      54.         try  
      55.         {  
      56.             con = JDBCUtils.getConnection();  
      57.             String sql = "delete from student where stuName =?";  
      58.             ps = con.prepareStatement(sql);  
      59.             ps.setString(1, name);  
      60.               
      61.             i = ps.executeUpdate() ;  
      62.               
      63.         }  
      64.         catch(SQLException e)  
      65.         {  
      66.             throw new DAOException(e.getMessage(),e);  
      67.         }  
      68.         finally  
      69.         {  
      70.             JDBCUtils.free(null, ps, con);  
      71.         }  
      72.               
      73.         return i;  
      74.     }  
      75.       
      76.     //修改一個(gè)學(xué)生  
      77.     public int updateStudent(String name)  
      78.     {  
      79.         Connection con = null ;  
      80.         PreparedStatement ps = null ;  
      81.         int i = 0 ;  
      82.         try  
      83.         {  
      84.             con = JDBCUtils.getConnection();  
      85.             String sql = "update student set stuAge=stuAge+1  where stuName =?";  
      86.             ps = con.prepareStatement(sql);  
      87.             ps.setString(1, name);  
      88.               
      89.             i = ps.executeUpdate() ;  
      90.               
      91.         }  
      92.         catch(SQLException e)  
      93.         {  
      94.             throw new DAOException(e.getMessage(),e);  
      95.         }  
      96.         finally  
      97.         {  
      98.             JDBCUtils.free(null, ps, con);  
      99.         }  
      100.           
      101.         return i;  
      102.     }  
      103.     //查詢一行  
      104.     public Student findStudent(String name)  
      105.     {  
      106.         Connection con = null ;  
      107.         PreparedStatement ps = null ;  
      108.         Student stu = null ;  
      109.         ResultSet rs = null;  
      110.         try  
      111.         {  
      112.             con = JDBCUtils.getConnection();  
      113.             String sql = "select stuName,stuAge,stuTel,stuAddress,groupId from student where stuName =?";  
      114.             ps = con.prepareStatement(sql);  
      115.             ps.setString(1, name);  
      116.               
      117.             rs = ps.executeQuery() ;  
      118.             stu = new Student();  
      119.             while(rs.next())  
      120.             {  
      121.                 stu.setStuName(rs.getString(1));  
      122.                 stu.setStuAge(rs.getInt(2));  
      123.                 stu.setStuTel(rs.getString(3));  
      124.                 stu.setStuAddress(rs.getString(4));  
      125.                 stu.setGroupId(rs.getInt(5));  
      126.             }  
      127.               
      128.         }  
      129.         catch(SQLException e)  
      130.         {  
      131.             throw new DAOException(e.getMessage(),e);  
      132.         }  
      133.         finally  
      134.         {  
      135.             JDBCUtils.free(rs, ps, con);  
      136.         }  
      137.           
      138.         return stu;  
      139.     }  
      140.       
      141.     //查詢所有  
      142.     public Set<Student> findAll()  
      143.     {  
      144.         Connection con = null ;  
      145.         PreparedStatement ps = null ;  
      146.         Student stu = null ;  
      147.         ResultSet rs = null;  
      148.         Set<Student> set = null ;  
      149.         try  
      150.         {  
      151.             con = JDBCUtils.getConnection();  
      152.             String sql = "select stuName,stuAge,stuTel,stuAddress,groupId from student";  
      153.             ps = con.prepareStatement(sql);  
      154.               
      155.             set = new HashSet<Student>() ;  
      156.             rs = ps.executeQuery() ;  
      157.               
      158.             while(rs.next())  
      159.             {  
      160.                 stu = new Student();  
      161.                   
      162.                 stu.setStuName(rs.getString(1));  
      163.                 stu.setStuAge(rs.getInt(2));  
      164.                 stu.setStuTel(rs.getString(3));  
      165.                 stu.setStuAddress(rs.getString(4));  
      166.                 stu.setGroupId(rs.getInt(5));  
      167.                   
      168.                 set.add(stu);  
      169.             }  
      170.               
      171.         }  
      172.         catch(SQLException e)  
      173.         {  
      174.             throw new DAOException(e.getMessage(),e);  
      175.         }  
      176.         finally  
      177.         {  
      178.             JDBCUtils.free(rs, ps, con);  
      179.         }  
      180.           
      181.         return set;  
      182.     }  
      183.          
      184. }  


      5.自定義異常 繼承了運(yùn)行時(shí)異常,具體操作讓父類實(shí)現(xiàn)

       

      DAOException.java

      1. package com.myjdbc.dao;  
      2.   
      3. /** 
      4.  * 自定義異常 
      5.  * @author Administrator 
      6.  * 
      7.  */  
      8. public class DAOException extends RuntimeException {  
      9.       
      10.       
      11.     public DAOException()  
      12.     {  
      13.         super();  
      14.     }  
      15.       
      16.     public DAOException(String messege,Throwable cause)  
      17.     {  
      18.         super(messege,cause);  
      19.     }  
      20.       
      21.     public DAOException(String messege)  
      22.     {  
      23.         super(messege);  
      24.     }  
      25.       
      26.     public DAOException(Throwable cause)  
      27.     {  
      28.         super(cause);  
      29.     }  
      30.       
      31.       
      32.   
      33. }  


       

      6定義一個(gè)服務(wù)類(服務(wù)層),本來還要定義一個(gè)接口,這里簡寫了,客戶與DAO的紐帶,持有DAO對象的引用

       

      StudentService.java

      1. package com.myjdbc.service;  
      2.   
      3. import java.util.Set;  
      4.   
      5. import com.myjdbc.bean.Student;  
      6. import com.myjdbc.dao.StudentDAO;  
      7. import com.myjdbc.dao.ConcreteStudentDao;  
      8.   
      9. public class StudentService {  
      10.       
      11.     StudentDAO sd = new ConcreteStudentDao();  
      12.       
      13.     public int add(Student student)  
      14.     {  
      15.         return this.sd.addStudent(student);  
      16.     }  
      17.       
      18.     public int delete(String name)  
      19.     {  
      20.         return this.sd.deleteStudent(name);  
      21.     }  
      22.       
      23.     public int update(String name)  
      24.     {  
      25.         return this.sd.updateStudent(name);  
      26.     }  
      27.       
      28.     public Student find(String name)  
      29.     {  
      30.         return this.sd.findStudent(name);  
      31.     }  
      32.       
      33.     public Set<Student> findAll()  
      34.     {  
      35.         return this.sd.findAll();  
      36.     }  
      37.   
      38. }  


      7.定義一個(gè)測試類,相當(dāng)于 (表現(xiàn)層)

       

      Client.java

      1. package com.myjdbc.test;  
      2.   
      3. import java.util.HashSet;  
      4. import java.util.Iterator;  
      5. import java.util.Set;  
      6.   
      7. import com.myjdbc.bean.Student;  
      8. import com.myjdbc.service.StudentService;  
      9.   
      10. public class Client {  
      11.    public static void main(String[] args)  
      12.    {  
      13.        Student stu = new Student();  
      14.        Set<Student> set = new HashSet<Student>();  
      15. //     stu.setStuName("zhangsan");  
      16. //     stu.setStuAge(20);  
      17. //     stu.setStuTel("18779157911");  
      18. //     stu.setStuAddress("china");  
      19. //     stu.setGroupId(1);  
      20.        StudentService ss = new StudentService();  
      21.        //System.out.println(ss.add(stu));  
      22.        //System.out.println(ss.delete("aa"));  
      23.        //System.out.println(ss.update("bb"));  
      24.        //stu = ss.find("cc");  
      25.        //System.out.println(stu.getStuName() +" " +stu.getStuAge()+" "+stu.getStuTel()+" "+stu.getStuAddress()+" "+stu.getGroupId());  
      26.        set = ss.findAll() ;  
      27.        Iterator<Student> iterator = set.iterator();  
      28.        while(iterator.hasNext())  
      29.        {  
      30.           Student student =  (Student)iterator.next() ;  
      31.           System.out.println(student.getStuName() +" " +student.getStuAge()+" "+student.getStuTel()+" "+student.getStuAddress()+" "+student.getGroupId());  
      32.        }  
      33.    }  
      34. }  


       

       

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多