新聞中心
用Jdbc-odbc橋來連接,不過這可是犧牲了速度來的。用Jdbc-odbc橋的話,和連接access是一樣的,先要設(shè)置一個(gè)數(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)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。涇川網(wǎng)站建設(shè)公司,為涇川等地區(qū)提供建站服務(wù)。全流程按需策劃,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
進(jìn)行數(shù)據(jù)庫的鏈接是比較耗時(shí)的,如果頻繁刷新頁面,那就會(huì)不停的訪問數(shù)據(jù)庫,大大耗去了數(shù)據(jù)庫的資源。JSP提供了這樣一對(duì)函數(shù),JSPInit(),JSPDestory();如果要在JSP網(wǎng)頁開始執(zhí)行時(shí),進(jìn)行某些數(shù)據(jù)的初始化,則可以利用JSPInit函數(shù)來完成。此函數(shù)將在JSP網(wǎng)頁被執(zhí)行時(shí)調(diào)用,且當(dāng)JSP網(wǎng)頁重新整理時(shí),并不會(huì)被再度執(zhí)行。當(dāng)關(guān)閉服務(wù)器時(shí),JSPDestory函數(shù)將被調(diào)用,可利用該函數(shù)來完成數(shù)據(jù)的善后處理。
可以利用JSPInit和JSPDestory函數(shù)來完成數(shù)據(jù)庫的鏈接和關(guān)閉。在JSPInit中進(jìn)行數(shù)據(jù)庫的鏈接,可以避免每次刷新頁面時(shí)都要鏈接數(shù)據(jù)庫,提高了工作效率。
以下是代碼實(shí)例:
- <%!
- Connection conn=null;
- Statement st=null;
- ResultSet rs=null;
- Public void jspInit()
- {
- Try
- {
- //加載驅(qū)動(dòng)程序類
- Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
- //連接數(shù)據(jù)庫
- Connection conn=DriverManager.getConnection("jdbc:odbc:strurl",”
- username”,”password”);
- //建立Statement對(duì)象
- St=conn.CreateStatement();
- }
- Catch(Exception ex)
- {
- System.out.println(ex.toString());
- }
- }
- Public void jspDestroy()
- {
- try
- {
- rs.close();
- st.,close();
- conn.close();
- }
- catch(Exception ex)
- {
- System.out.println(ex.toString());
- }
- }
- %>
當(dāng)JSP網(wǎng)頁從數(shù)據(jù)庫中取得數(shù)據(jù)時(shí),最耗費(fèi)服務(wù)器時(shí)間的是建立數(shù)據(jù)庫鏈接。用JSPInit
和JSPDestory函數(shù)并不是非常好的辦法,畢竟每瀏覽一次新網(wǎng)頁,就要建立數(shù)據(jù)庫鏈
接。這個(gè)時(shí)候可以為一個(gè)聯(lián)機(jī)者建立一個(gè)數(shù)據(jù)庫鏈接。這里我們利用Bean對(duì)象來建立數(shù)
據(jù)庫鏈接。
以下是代碼實(shí)例:
- //定義bean所屬的套件
- package com.test;
- import java.io.*;
- import java.sql.*;
- import javax.servlet.http.*;
- public class conn implements HttpSessionBindingListener
- {
- private Connection con=null;
- public conn() //在構(gòu)造函數(shù)中完成數(shù)據(jù)庫鏈接
- {
- BulidConnection();
- }
- private void BulidConnection()
- {
- try
- {
- //載入驅(qū)動(dòng)程序
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- }
- catch(java.lang.ClassNotFoundException e1)
- {
- System.out.println("數(shù)據(jù)庫驅(qū)動(dòng)加載失敗
");- }
- try
- {
- //建立數(shù)據(jù)庫鏈接
- con=DriverManager.getConnection("jdbc:odbc:test","test","test");
- }
- catch(SQLException e2)
- {
- System.out.println("數(shù)據(jù)庫連接失敗");
- }
- }
- //取得Connection對(duì)象
- public Connection getConnection()
- {
- if(con==null)
- BulidConnection();
- return this.con;
- }
- public void close()
- {
- try
- {
- con.close();
- con=null;
- }
- catch(SQLException sex)
- {
- System.out.println(sex.toString());
- }
- }
- //當(dāng)物體加入session時(shí),將自動(dòng)執(zhí)行此函數(shù)
- public void valueBound(HttpSessionBindingEvent event){}
- //當(dāng)session對(duì)象刪除時(shí),將自動(dòng)執(zhí)行此函數(shù)
- public void valueUnbound(HttpSessionBindingEvent event)
- {
- if(con!=null)
- close();//調(diào)用close方法
- }
- }
新聞名稱:JSP的Init和Destory函數(shù)提高工作效率
文章網(wǎng)址:http://www.fisionsoft.com.cn/article/cdhescp.html


咨詢
建站咨詢
