自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析一個簡單的JDBC例子

開發(fā) 后端
這里將為大家簡單介紹一個JDBC例子,通過實際代碼,希望能為大家了解提供幫助。JDBC是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成。

JDBC例子1,首先在配置文件(system.properties)中配置上如下內(nèi)容:

  1. driver=com.microsoft.sqlserver.jdbc.SQLServerDriver  
  2. url=jdbc:sqlserver://localhost:1433;databaseName=mp  
  3. username=sa  
  4. password=mengya 

JDBC例子2,寫了個SQLDB的工具類

  1. publicclassSQLDBConnection{  
  2. privateInputStreaminputstr;  
  3. privatePropertiespro;  
  4. privatestaticSQLDBConnectionsqldb=null;
  5. //私有構(gòu)造方法

    privateSQLDBConnection(){

    inputstr=this.getClass().getResourceAsStream("/system.properties");

    pro=newProperties();

    try{

    pro.load(inputstr);

    }catch(IOExceptione){

    e.printStackTrace();

    }

    try{

    Class.forName(pro.getProperty("driver"));//注冊驅(qū)動,只注冊一次

    }catch(ClassNotFoundExceptione){

    e.printStackTrace();

    }

    }

    //單例模式

    publicstaticSQLDBConnectiongetSQLDBConnection(){

    if(sqldb==null){

    synchronized(SQLDBConnection.class){

    if(sqldb==null){

    sqldb=newSQLDBConnection();

    }

    }

    }

    returnsqldb;

    }

    //得到與數(shù)據(jù)庫的連接

    publicConnectionGetConnection(){

    Connectionconn=null;

    try{

    conn=DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("username"),pro.getProperty("password"));

    }catch(SQLExceptione){

    e.printStackTrace();

    }

    returnconn;

    }

JDBC例子3,寫好Studao的接口

  1. //釋放資源  
  2. publicstaticvoidfree(ResultSetrs,Statementsta,Connectionconn){  
  3. try{  
  4. if(rs!=null){  
  5. rs.close();  
  6. }  
  7. }catch(Exceptione){  
  8. e.printStackTrace();  
  9. }finally{  
  10. try{  
  11. if(sta!=null){  
  12. sta.close();  
  13. }  
  14. }catch(Exceptione){  
  15. e.printStackTrace();  
  16. }finally{  
  17. if(conn!=null){  
  18. try{  
  19. conn.close();  
  20. }catch(SQLExceptione){  
  21. e.printStackTrace();  
  22. }  
  23. }  
  24. }  
  25. }  
  26. }  
  27. publicinterfaceStudaointer{  
  28. publicvoidaddStu(Stustu);  
  29. publicvoiddelStu(intsid);  
  30. publicvoidupdStu(Stustu);  
  31. publicStugetOneStu(intsid);  
  32. publicListgetAllStu();  

JDBC例子4,寫好自己定義的RuntimeException

  1. publicclassMySQLExceptionextendsRuntimeException{  
  2. privatestaticfinallongserialVersionUID=1L;  

JDBC例子5,寫好Studao的實現(xiàn)類

  1. publicclassStuDAOImpleimplementsStudaointer{  
  2. privateConnectionconn;  
  3. privatePreparedStatementpre;  
  4. privateResultSetrs;  
  5. publicvoidaddStu(Stustu){  
  6. Stringsql="insertintostuvalues(?,?,?)";  
  7. conn=SQLDBConnection.getSQLDBConnection().GetConnection();  
  8. try{  
  9. pre=conn.prepareStatement(sql);  
  10. pre.setString(1,stu.getSname());  
  11. pre.setString(2,stu.getSsex());  
  12. pre.setDate(3,newjava.sql.Date(stu.getSbrith().getTime()));  
  13. pre.executeUpdate();  
  14. }catch(SQLExceptione){  
  15. e.printStackTrace();  
  16. thrownewMySQLException();//異常向上拋  
  17. }finally{  
  18. SQLDBConnection.free(rs,pre,conn);  
  19. }  
  20. }  
  21. publicvoiddelStu(intsid){  
  22. Stringsql="deletestuwheres_id=?";  
  23. conn=SQLDBConnection.getSQLDBConnection().GetConnection();  
  24. try{  
  25. pre=conn.prepareStatement(sql);  
  26. pre.setInt(1,sid);  
  27. pre.executeUpdate();  
  28. }catch(SQLExceptione){  
  29. e.printStackTrace();  
  30. thrownewMySQLException();  
  31. }finally{  
  32. SQLDBConnection.free(rs,pre,conn); 

【編輯推薦】

  1. 使用JDBC的五個精華功能
  2. Tomcat5+MySQL JDBC連接池配置
  3. 在Weblogic中實現(xiàn)JDBC的功能
  4. 詳解JDBC與Hibernate區(qū)別
  5. JDBC連接MySQL數(shù)據(jù)庫關(guān)鍵四步
  6. 詳解JDBC驅(qū)動的四種類型
責任編輯:彭凡 來源: javaeye
相關(guān)推薦

2009-07-21 14:55:30

2010-04-19 17:21:36

Oracle寫文件

2020-03-26 17:00:53

HashMapputJava

2009-09-11 09:10:30

C#編寫游戲

2009-07-15 13:41:00

JDBC實例

2011-08-02 12:46:46

Oracle數(shù)據(jù)表建立索引

2009-09-14 17:08:02

WebFormView

2009-06-19 17:14:47

JDBC驅(qū)動設(shè)置

2009-06-19 15:08:23

JDBC驅(qū)動

2011-03-24 09:34:41

SPRING

2011-05-06 14:19:29

ExcelSQL Server

2020-10-26 13:12:00

多線程調(diào)度隨機性

2021-05-13 07:58:05

JDBC接口PreparedSta

2022-10-19 11:31:49

TDD開發(fā)

2020-11-09 06:38:00

ninja構(gòu)建方式構(gòu)建系統(tǒng)

2009-08-26 15:53:42

C#數(shù)據(jù)訪問XML

2023-02-07 10:40:30

gRPC系統(tǒng)Mac

2009-08-19 04:14:00

線性鏈表

2018-11-22 14:09:45

iOS架構(gòu)組件開發(fā)

2009-07-17 17:07:17

JDBC教程
點贊
收藏

51CTO技術(shù)棧公眾號