日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析一個簡單的JDBC例子

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

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比玄武網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式玄武網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務覆蓋玄武地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

 
 
 
  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); 

標題名稱:淺析一個簡單的JDBC例子
轉(zhuǎn)載源于:http://www.dlmjj.cn/article/djdjhho.html