JSP連接Access數(shù)據(jù)庫
更新時(shí)間:2007年02月07日 00:00:00 作者:
一.建立數(shù)據(jù)庫及ODBC數(shù)據(jù)源
1.建立jcc.mdb數(shù)據(jù)庫及user表
?。?添加測試數(shù)據(jù)
?。?配置ODBC數(shù)據(jù)源
二.在<%wwwroot%>/下,新建Access數(shù)據(jù)庫連接文件Select.jsp
Select.jsp源碼如下:
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<html>
<body>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print(e);
}
try{
String url = "jdbc:odbc:jcc";
Connection conn = DriverManager.getConnection(url,"jcc","jsp.com.cn");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM user");
out.println("User-list"+"<br>");
while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+"<br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
out.print(ex);
}
%>
</body>
</html>
四.運(yùn)行http://localhost/Select.jsp,顯示結(jié)果如下:
User-list
1 Corebit
2 Ivan
則表示數(shù)據(jù)庫連接成功!恭喜!恭喜!
否則請(qǐng)檢查數(shù)據(jù)源相關(guān)設(shè)置,出錯(cuò)可能性比較高!
附言:
常有人問起,如何在不做ODBC數(shù)據(jù)源的情況下讓JSP訪問Access數(shù)據(jù)庫,為解開這個(gè)迷團(tuán),特寫以下連接代碼,以供參考!其中,jcc.mdb與Select.jsp同位于<%wwwroot%>(根目錄)下。
改寫后的Select.jsp源碼如下:
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<html>
<body>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print(e);
}
try{
String strDirPath=application.getRealPath(request.getRequestURI());
strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\";
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath+"jcc.mdb";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM user");
out.println("User-list"+"<br>");
while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+"<br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
out.print(ex);
}
%>
</body>
</html>
運(yùn)行結(jié)果應(yīng)該與使用ODBC時(shí)的運(yùn)行結(jié)果相同!
?。ⅲ何募鸖elect.jsp區(qū)分大小寫!
希望本文能對(duì)你的JSP連接Access數(shù)據(jù)庫有所幫助!
==========================================
只能使用jdbc-odbc橋來連接
想要設(shè)置odbc數(shù)據(jù)源
然后連接
String dbdriver = "oracle.jdbc.driver.OracleDriver";
String dbname = "jdbc:oracle:thin:@192.168.0.101:1521:orcl";//根據(jù)你的情況修改
String user = "system";//用戶名
String password = "manager";//密碼
Connection conn = null;
Statement stmt = null;
ResultSet rs =null;
String sql="select * from 表名";//根據(jù)實(shí)際情況修改
try
{
Class.forName(dbdriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("Class access_dbconnect not fount!"+e.getMessage());
}
conn=DriverManager.getConnection(dbname,user,password);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
=========================================
sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
sConnStr = "jdbc:odbc:odbc名稱";
conn = null;
rs = null;
try
{
Class.forName(sDBDriver);
}
conn = DriverManager.getConnection(sConnStr);
Statement statement = conn.createStatement();
rs = statement.executeQuery(s);
你在odbc數(shù)據(jù)源中建一個(gè)access連接,然后把上面的代碼中的odbc名稱改成你的odbc數(shù)據(jù)源連接名稱就可以了。
1.建立jcc.mdb數(shù)據(jù)庫及user表
?。?添加測試數(shù)據(jù)
?。?配置ODBC數(shù)據(jù)源
二.在<%wwwroot%>/下,新建Access數(shù)據(jù)庫連接文件Select.jsp
Select.jsp源碼如下:
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<html>
<body>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print(e);
}
try{
String url = "jdbc:odbc:jcc";
Connection conn = DriverManager.getConnection(url,"jcc","jsp.com.cn");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM user");
out.println("User-list"+"<br>");
while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+"<br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
out.print(ex);
}
%>
</body>
</html>
四.運(yùn)行http://localhost/Select.jsp,顯示結(jié)果如下:
User-list
1 Corebit
2 Ivan
則表示數(shù)據(jù)庫連接成功!恭喜!恭喜!
否則請(qǐng)檢查數(shù)據(jù)源相關(guān)設(shè)置,出錯(cuò)可能性比較高!
附言:
常有人問起,如何在不做ODBC數(shù)據(jù)源的情況下讓JSP訪問Access數(shù)據(jù)庫,為解開這個(gè)迷團(tuán),特寫以下連接代碼,以供參考!其中,jcc.mdb與Select.jsp同位于<%wwwroot%>(根目錄)下。
改寫后的Select.jsp源碼如下:
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<html>
<body>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print(e);
}
try{
String strDirPath=application.getRealPath(request.getRequestURI());
strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\";
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath+"jcc.mdb";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM user");
out.println("User-list"+"<br>");
while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+"<br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
out.print(ex);
}
%>
</body>
</html>
運(yùn)行結(jié)果應(yīng)該與使用ODBC時(shí)的運(yùn)行結(jié)果相同!
?。ⅲ何募鸖elect.jsp區(qū)分大小寫!
希望本文能對(duì)你的JSP連接Access數(shù)據(jù)庫有所幫助!
==========================================
只能使用jdbc-odbc橋來連接
想要設(shè)置odbc數(shù)據(jù)源
然后連接
String dbdriver = "oracle.jdbc.driver.OracleDriver";
String dbname = "jdbc:oracle:thin:@192.168.0.101:1521:orcl";//根據(jù)你的情況修改
String user = "system";//用戶名
String password = "manager";//密碼
Connection conn = null;
Statement stmt = null;
ResultSet rs =null;
String sql="select * from 表名";//根據(jù)實(shí)際情況修改
try
{
Class.forName(dbdriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("Class access_dbconnect not fount!"+e.getMessage());
}
conn=DriverManager.getConnection(dbname,user,password);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
=========================================
sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
sConnStr = "jdbc:odbc:odbc名稱";
conn = null;
rs = null;
try
{
Class.forName(sDBDriver);
}
conn = DriverManager.getConnection(sConnStr);
Statement statement = conn.createStatement();
rs = statement.executeQuery(s);
你在odbc數(shù)據(jù)源中建一個(gè)access連接,然后把上面的代碼中的odbc名稱改成你的odbc數(shù)據(jù)源連接名稱就可以了。
您可能感興趣的文章:
- JSP中操作數(shù)據(jù)庫的常用SQL標(biāo)簽用法總結(jié)
- jsp從數(shù)據(jù)庫獲取數(shù)據(jù)填充下拉框?qū)崿F(xiàn)二級(jí)聯(lián)動(dòng)菜單的方法
- JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
- Java實(shí)現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
- jsp讀取數(shù)據(jù)庫實(shí)現(xiàn)分頁技術(shù)簡析
- jsp 從web.xml讀取連接數(shù)據(jù)庫的參數(shù)
- JSP連接MySql/MS SQL Server/Oracle數(shù)據(jù)庫連接方法[整理]
- Jsp連接Access數(shù)據(jù)庫(不通過建立ODBC數(shù)據(jù)源的方法)
- 利用asp或jsp,flash怎樣把數(shù)據(jù)庫中的一張表中的所有記錄讀取并顯示出來
- JSP數(shù)據(jù)庫操數(shù)據(jù)分頁顯示
- 在JSP中訪問Oracle數(shù)據(jù)庫
- 如何使用JSP訪問MySQL數(shù)據(jù)庫
- JSP中的PreparedStatement對(duì)象操作數(shù)據(jù)庫的使用教程
相關(guān)文章
通過過濾器(Filter)解決JSP的Post和Request中文亂碼問題
這篇文章主要介紹了jsp中通過過濾器(Filter)解決JSP的Post和Request中文亂碼問題的方法,需要的朋友可以參考下2014-08-08JSP由淺入深(10)—— Beans and Forms處理
JSP由淺入深(10)—— Beans and Forms處理...2006-10-10用fileupload組件實(shí)現(xiàn)的大文件上傳簡單實(shí)例
下面小編就為大家?guī)硪黄胒ileupload組件實(shí)現(xiàn)的大文件上傳簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10application對(duì)象統(tǒng)計(jì)所有用戶對(duì)某網(wǎng)頁的訪問次數(shù)
使用application對(duì)象完成累計(jì)的功能統(tǒng)計(jì)所有用戶對(duì)某網(wǎng)頁的訪問次數(shù),具體實(shí)現(xiàn)如下,喜歡的朋友可以參考下2013-08-08