|
如下圖:

2.在站點(E:/Javatest)下建立測試連接數據庫文件“AccessTest.jsp”
代碼如下:
<%@ page contentType="text/html; charset=gb2312" language="Java" import="Java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ACCESS連接測試</title>
</head>
<body>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print(e);
}
try{
//絕對路徑
//String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:/Javatest/AccessDb/test.mdb";
//其中 E:/Javatest/AccessDb/test.mdb 為數據庫所在絕對路徑
//相對路徑
String strDirPath=getServletContext().getRealPath("/"); //獲得所在站點的絕對路徑:E:/Javatest/
//out.print(strDirPath+"<br>");
strDirPath=strDirPath.replace('//','/'); //將“/”替換為“/” E:/Javatest/
//out.print(strDirPath+"<br>");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath+"AccessDb/test.mdb";
//out.print(url+"<br>");
//String url = "jdbc:odbc:accesstest";
//建立ODBC數據源連接
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM stu"); //取得stu表中的記錄
out.println("Table-list"+"<br>");
while(rs.next()){
out.print(rs.getInt(1)+" ");
out.print(rs.getString(2)+"<br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
out.print(ex);
}
%>
</body>
</html>
運行結果如下:

jsp技術:Jsp連接Access數據庫(不通過建立ODBC數據源的方法),轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。