以訪問一個學生信息表為例說明:
首先,建立access數據庫文件student.mdb,打開表設計器,分別輸入字段名稱和數據類型,并設置主鍵:
其次,建立測試數據:
| num | name | age | fromw | school | major | score |
|---|---|---|---|---|---|---|
| 200706001 | 張飛 | 19 | 吉林 | 吉林大學 | 語言學 | 388 |
| 200706002 | 劉備 | 20 | 吉林 | 東北師范大學 | 日語 | 328 |
| 200706003 | 關羽 | 19 | 北京 | 北京大學 | 計算機軟件與理論 | 421 |
| 200706004 | 貂禪 | 18 | 四川 | 四川大學 | 電子信息工程 | 376 |
| 200706005 | 關漢卿 | 18 | 浙江 | 浙江大學 | 計算數學 | 375 |
| 200706006 | 曲藝 | 20 | 浙江 | 浙江大學 | 應用化學 | 402 |
| 200706007 | 阿曼 | 19 | 北京 | 北京大學 | 圖書館管理學 | 314 |
| 200706008 | 阿沛 | 19 | 北京 | 北京航空航天大學 | 熱能與動力工程 | 348 |
| 200706009 | 武則天 | 19 | 上海 | 復旦大學 | 歐美文學 | 399 |
| 200706010 | 柳云龍 | 18 | 遼寧 | 沈陽工業大學 | 機械制造 | 416 |
| 200706011 | 方戈 | 19 | 遼寧 | 大連理工大學 | 應用數學 | 333 |
| 200706012 | 王達 | 21 | 天津 | 天津大學 | 網絡應用 | 342 |
| 200706013 | 西施 | 18 | 天津 | 南開大學 | 數據倉庫 | 411 |
| 200706014 | 李世民 | 18 | 吉林 | 長春理工大學 | 信息管理與信息系統 | 364 |
| 200706015 | 王莽 | 19 | 湖北 | 武漢大學 | 太陽能 | 294 |
| 200706016 | 朱元璋 | 18 | 湖北 | 武漢大學 | 地熱學 | 352 |
| 200706017 | 諸葛亮 | 19 | 北京 | 清華大學 | 數據挖掘技術 | 434 |
| 200706018 | 石達開 | 20 | 北京 | 北京科技大學 | 能機工程 | 319 |
| 200706019 | 洪豐燭 | 18 | 北京 | 北京外國語大學 | 外國語文化 | 347 |
| 200706020 | 柳艷 | 18 | 黑龍江 | 哈爾濱工業大學 | 電氣工程及其自動化 | 405 |
把student.db放置于D:/根目錄下。
再次,建立并配置數據源:數據源名稱為student。
然后,在Apache Software Foundation/Tomcat 5.5/webapps路徑下創建一個Student文件目錄,并且在該目錄下編寫jsp文件代碼:

<%...@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*"%>
<html>
<body>
<center>
<h1>學生錄取信息< SPAN>h1>
<hr>
<table border=1>
<tr><td>學號< SPAN>td><td>姓名< SPAN>td><td>年齡< SPAN>td><td>來自< SPAN>td><td>學校< SPAN>td><td>專業< SPAN>td><td>成績< SPAN>td>< SPAN>tr>
<%...
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:/student.mdb";
Connection con=DriverManager.getConnection(url) ;
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from student");
while(rst.next())
{
out.PRintln("
out.println("
out.println("
out.println("
out.println("
out.println("
out.println("
out.println("
out.println("
}
rst.close();
stmt.close();
con.close();
%>
< SPAN>table>
< SPAN>center>
< SPAN>body>
< SPAN>html>接著,在Tomcat 5.5.20中設置配置文件:
在Student目錄下創建一個WEB-INF目錄,在WEB-INF目錄下創建一個web.xml文件,其配置內容為:
xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<display-name>Student< SPAN>display-name>
<description>
Query the information of students from Access Database
< SPAN>description>
< SPAN>web-app>最后,啟動Tomcat服務器,在地址欄中鍵入如下URL進行對數據庫Student.mdb的訪問:
http://127.0.0.1:8080/Student/Student.jsp
訪問結果如圖所示:
這就實現了對Access數據庫的訪問。
新聞熱點
疑難解答