Jsp連接Access數(shù)據(jù)庫(不通過建立ODBC數(shù)據(jù)源的方法)
更新時間:2007年02月07日 00:00:00 作者:
1. 在站點(我的站點為:E:\javatest)下建立文件夾“AccessDb”并在文件夾下建立數(shù)據(jù)庫“test.mdb”以及表“stu”,stu字段為 id,stuname test.mdb所在路徑為:E:\javatest\AccessDb 下面
如下圖:

2.在站點(E:\javatest)下建立測試連接數(shù)據(jù)庫文件“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 為數(shù)據(jù)庫所在絕對路徑
//相對路徑
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數(shù)據(jù)源連接
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>
運行結(jié)果如下:

如下圖:

2.在站點(E:\javatest)下建立測試連接數(shù)據(jù)庫文件“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 為數(shù)據(jù)庫所在絕對路徑
//相對路徑
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數(shù)據(jù)源連接
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>
運行結(jié)果如下:

您可能感興趣的文章:
- JSP中操作數(shù)據(jù)庫的常用SQL標簽用法總結(jié)
- jsp從數(shù)據(jù)庫獲取數(shù)據(jù)填充下拉框?qū)崿F(xiàn)二級聯(lián)動菜單的方法
- JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
- Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
- jsp讀取數(shù)據(jù)庫實現(xiàn)分頁技術(shù)簡析
- jsp 從web.xml讀取連接數(shù)據(jù)庫的參數(shù)
- JSP連接MySql/MS SQL Server/Oracle數(shù)據(jù)庫連接方法[整理]
- JSP連接Access數(shù)據(jù)庫
- 利用asp或jsp,flash怎樣把數(shù)據(jù)庫中的一張表中的所有記錄讀取并顯示出來
- JSP數(shù)據(jù)庫操數(shù)據(jù)分頁顯示
- 在JSP中訪問Oracle數(shù)據(jù)庫
- 如何使用JSP訪問MySQL數(shù)據(jù)庫
- JSP中的PreparedStatement對象操作數(shù)據(jù)庫的使用教程
相關(guān)文章
教你怎么用JSP統(tǒng)計網(wǎng)站訪問人數(shù)
這篇文章主要介紹了教你怎么用JSP統(tǒng)計網(wǎng)站訪問人數(shù),文中有非常詳細的代碼示例,對正在學習JSP的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04如何在Jsp中使用JDBC來聯(lián)結(jié)MySql
如何在Jsp中使用JDBC來聯(lián)結(jié)MySql...2006-10-10基于EJB技術(shù)的商務預訂系統(tǒng)的開發(fā)
基于EJB技術(shù)的商務預訂系統(tǒng)的開發(fā)...2006-10-10通過JDBC連接oracle數(shù)據(jù)庫的十大技巧
通過JDBC連接oracle數(shù)據(jù)庫的十大技巧...2006-10-10關(guān)于IIS連接數(shù)和在線人數(shù)的詳細說明
這篇文章主要為大家介紹下IIS連接數(shù)和在線人數(shù)的詳細說明需要的朋友可以參考下2006-10-10SSM框架整合JSP中集成easyui前端ui項目開發(fā)示例詳解
這篇文章主要介紹了SSM框架JSP中集成easyui前端ui項目開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10