偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

簡單介紹JSP數(shù)據(jù)庫鏈接

開發(fā) 后端
本文簡單介紹JSP數(shù)據(jù)庫鏈接,以及介紹JSP Destory和JSP Init函數(shù)。

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

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

可以利用JSP Init和JSP Destory函數(shù)來完成數(shù)據(jù)庫的鏈接和關(guān)閉。在JSP Init中進(jìn)行數(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. //加載驅(qū)動程序類  
  10. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);  
  11. //連接數(shù)據(jù)庫       
  12. Connection conn=DriverManager.getConnection("jdbc:odbc:strurl",”  
  13. username”,”password”);  
  14. //建立Statement對象  
  15. St=conn.CreateStatement();  
  16. }  
  17. Catch(Exception ex)  
  18. {  
  19. System.out.println(ex.toString());  
  20. }  
  21. }  
  22. Public void jspDestroy()  
  23. {  
  24. try  
  25. {  
  26. rs.close();  
  27. st.,close();  
  28.  conn.close();  
  29.  }  
  30. catch(Exception ex)  
  31. {  
  32. System.out.println(ex.toString());  
  33. }  
  34. }  
  35. %> 

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

以下是代碼實例:

  1. conn.java  
  2. //定義bean所屬的套件  
  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() //在構(gòu)造函數(shù)中完成數(shù)據(jù)庫鏈接  
  11. {  
  12. BulidConnection();  
  13. }  
  14. private void BulidConnection()  
  15. {  
  16. try  
  17. {  
  18. //載入驅(qū)動程序  
  19. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  20. }  
  21. catch(java.lang.ClassNotFoundException e1)  
  22. {  
  23. System.out.println("數(shù)據(jù)庫驅(qū)動加載失敗<br>");  
  24. }  
  25. try  
  26. {  
  27. //建立數(shù)據(jù)庫鏈接  
  28. con=DriverManager.getConnection("jdbc:odbc:test","test","test");  
  29. }  
  30. catch(SQLException e2)  
  31. {  
  32. System.out.println("數(shù)據(jù)庫連接失敗");  
  33. }  
  34. }  
  35. //取得Connection對象  
  36. public Connection getConnection()  
  37. {  
  38. if(con==null)  
  39. BulidConnection();  
  40. return this.con;  
  41. }  
  42. public void close()  
  43. {  
  44. try  
  45. {  
  46. con.close();  
  47. con=null;  
  48. }  
  49. catch(SQLException sex)  
  50. {  
  51. System.out.println(sex.toString());  
  52. }  
  53. }  
  54. //當(dāng)物體加入session時,將自動執(zhí)行此函數(shù)  
  55. public void valueBound(HttpSessionBindingEvent event){}  
  56. //當(dāng)session對象刪除時,將自動執(zhí)行此函數(shù)  
  57. public void valueUnbound(HttpSessionBindingEvent event)  
  58. {  
  59. if(con!=null)  
  60. close();//調(diào)用close方法  
  61. }  

【編輯推薦】

  1. JSP bean代碼優(yōu)化
  2. 詳細(xì)介紹JSP環(huán)境配置方案
  3. 在JSP JSTL中使用存儲過程
  4. ASP.NET、JSP和PHP究竟哪個好
  5. JSP相關(guān)軟件介紹
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-06-30 15:15:30

JSP數(shù)據(jù)庫

2009-07-02 12:56:01

JSP技術(shù)

2011-03-29 09:40:31

SQL Server數(shù)據(jù)庫鏈接

2009-07-03 16:21:43

2009-07-06 14:43:30

JSP元素

2009-07-02 08:50:01

JSP標(biāo)簽庫

2011-08-15 15:40:57

SQL Server 系統(tǒng)數(shù)據(jù)庫

2011-08-11 16:08:55

Oracle數(shù)據(jù)庫ASHAWR

2009-07-02 10:13:47

ASP和JSPJSP頁面

2009-06-30 17:26:56

JSP頁面

2011-07-26 11:12:05

DBXML數(shù)據(jù)庫

2013-06-07 19:04:15

測試

2009-06-30 13:38:37

SERVLET和JSP

2011-08-12 12:59:33

Oracle數(shù)據(jù)庫同義詞

2011-08-03 15:14:17

Excel XP數(shù)據(jù)庫功能

2011-03-17 14:51:33

數(shù)據(jù)庫自我調(diào)整

2011-04-14 09:27:37

內(nèi)存數(shù)據(jù)庫

2009-07-03 13:56:21

JSP編程技巧

2009-07-06 15:57:56

獲取數(shù)據(jù)庫連接JSP

2011-07-04 17:27:42

JSP
點贊
收藏

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