日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JSP的Init和Destory函數(shù)提高工作效率

用Jdbc-odbc橋來連接,不過這可是犧牲了速度來的。用Jdbc-odbc橋的話,和連接access是一樣的,先要設置一個數(shù)據(jù)源DNS,然后就用:
Class.forName("sun.Jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("Jdbc:odbc:strurl",”
username”,”password”);

創(chuàng)新互聯(lián)專注于涇川企業(yè)網(wǎng)站建設,自適應網(wǎng)站建設,商城網(wǎng)站建設。涇川網(wǎng)站建設公司,為涇川等地區(qū)提供建站服務。全流程按需策劃,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

進行數(shù)據(jù)庫的鏈接是比較耗時的,如果頻繁刷新頁面,那就會不停的訪問數(shù)據(jù)庫,大大耗去了數(shù)據(jù)庫的資源。JSP提供了這樣一對函數(shù),JSPInit(),JSPDestory();如果要在JSP網(wǎng)頁開始執(zhí)行時,進行某些數(shù)據(jù)的初始化,則可以利用JSPInit函數(shù)來完成。此函數(shù)將在JSP網(wǎng)頁被執(zhí)行時調用,且當JSP網(wǎng)頁重新整理時,并不會被再度執(zhí)行。當關閉服務器時,JSPDestory函數(shù)將被調用,可利用該函數(shù)來完成數(shù)據(jù)的善后處理。

可以利用JSPInit和JSPDestory函數(shù)來完成數(shù)據(jù)庫的鏈接和關閉。在JSPInit中進行數(shù)據(jù)庫的鏈接,可以避免每次刷新頁面時都要鏈接數(shù)據(jù)庫,提高了工作效率。

以下是代碼實例:

 
 
 
  1. <%!  
  2. Connection conn=null;  
  3. Statement st=null;  
  4. ResultSet rs=null;  
  5. Public void jspInit()  
  6. {  
  7.  Try  
  8.  {  
  9. //加載驅動程序類  
  10.  
  11.   Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);  
  12.   //連接數(shù)據(jù)庫       
  13.  
  14. Connection conn=DriverManager.getConnection("jdbc:odbc:strurl",”  
  15. username”,”password”);  
  16. //建立Statement對象  
  17.  
  18. St=conn.CreateStatement();  
  19. }  
  20. Catch(Exception ex)  
  21. {  
  22. System.out.println(ex.toString());  
  23. }  
  24. }  
  25. Public void jspDestroy()  
  26. {  
  27. try  
  28. {  
  29.  rs.close();  
  30. st.,close();  
  31.    conn.close();  
  32.  }  
  33.  catch(Exception ex)  
  34.    {  
  35.    System.out.println(ex.toString());  
  36.  }  
  37. }  
  38. %> 

當JSP網(wǎng)頁從數(shù)據(jù)庫中取得數(shù)據(jù)時,最耗費服務器時間的是建立數(shù)據(jù)庫鏈接。用JSPInit
和JSPDestory函數(shù)并不是非常好的辦法,畢竟每瀏覽一次新網(wǎng)頁,就要建立數(shù)據(jù)庫鏈
接。這個時候可以為一個聯(lián)機者建立一個數(shù)據(jù)庫鏈接。這里我們利用Bean對象來建立數(shù)
據(jù)庫鏈接。

以下是代碼實例:

 
 
 
  1. //定義bean所屬的套件  
  2.  
  3. package com.test;  
  4. import java.io.*;  
  5. import java.sql.*;  
  6. import javax.servlet.http.*;  
  7. public class conn implements HttpSessionBindingListener  
  8. {  
  9. private Connection con=null;  
  10. public conn() //在構造函數(shù)中完成數(shù)據(jù)庫鏈接  
  11.  
  12. {  
  13. BulidConnection();  
  14. }  
  15. private void BulidConnection()  
  16. {  
  17. try  
  18. {  
  19. //載入驅動程序  
  20.  
  21. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  22. }  
  23. catch(java.lang.ClassNotFoundException e1)  
  24. {  
  25. System.out.println("數(shù)據(jù)庫驅動加載失敗
    ");  
  26. }  
  27. try  
  28. {  
  29. //建立數(shù)據(jù)庫鏈接  
  30.  
  31. con=DriverManager.getConnection("jdbc:odbc:test","test","test");  
  32. }  
  33. catch(SQLException e2)  
  34. {  
  35. System.out.println("數(shù)據(jù)庫連接失敗");  
  36. }  
  37. }  
  38. //取得Connection對象  
  39.  
  40. public Connection getConnection()  
  41. {  
  42. if(con==null)  
  43. BulidConnection();  
  44. return this.con;  
  45. }  
  46. public void close()  
  47. {  
  48. try  
  49. {  
  50. con.close();  
  51. con=null;  
  52. }  
  53. catch(SQLException sex)  
  54. {  
  55. System.out.println(sex.toString());  
  56. }  
  57. }  
  58. //當物體加入session時,將自動執(zhí)行此函數(shù)  
  59.  
  60. public void valueBound(HttpSessionBindingEvent event){}  
  61. //當session對象刪除時,將自動執(zhí)行此函數(shù)  
  62.  
  63. public void valueUnbound(HttpSessionBindingEvent event)  
  64. {  
  65. if(con!=null)  
  66. close();//調用close方法  
  67.  
  68. }  

分享名稱:JSP的Init和Destory函數(shù)提高工作效率
文章轉載:http://m.5511xx.com/article/cdhescp.html