當前位置:首頁 » 數據倉庫 » jsp連接access資料庫代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

jsp連接access資料庫代碼

發布時間: 2022-08-16 07:45:16

⑴ dreamweaver 中jsp動態頁面如何連接access資料庫

jsp連接access資料庫的實現方法如下:
<%@ page contentType="text/html; charset=GB18030" %>
<%@page import="java.sql.*" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<form method="POST" action="Webindex.jsp">
<%
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e) {
out.print("資料庫驅動程序裝入錯誤");
}
try {
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + request.getRealPath("/") + "test.mdb";
//特別注意上面的Driver和(*.mdb)之間是有空格的
/*這個test.mdb文件是存放在Web Mole目錄下的,當然可以自行改變路徑*/
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM log"); //log為表名,和SQL一樣
while (rs.next()) {
out.print(rs.getInt("ID") + " ");
out.print(rs.getString("username") + " ");
out.print(rs.getString("password") + " "); //log表中三個欄位,主鍵是ID,自增的。username和password是文本類型。
out.println("<br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex) {
out.print(ex);
}
%>
</form>
</body>
</html>

⑵ 簡單jsp+servlet+access資料庫測試代碼

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<%
try {
// 1.注冊驅動 構造驅動實例
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// new oracle.jdbc.driver.OracleDriver();
// 2.指定連接字元串 @機器IP 埠號 資料庫名稱
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
// 3.打開資料庫聯接
Connection conn = DriverManager
.getConnection(url, "sa", "111111");
// 4.構造查詢語句對象
Statement stme = conn.createStatement();
// 5.提交查詢語句並獲得結果集
ResultSet rs = stme.executeQuery("select * from person");
// 6.遍歷結果集
while (rs.next()) {
out.print(rs.getString(1));
out.print(rs.getString(2));
}
rs.close();
stme.close();
conn.close();

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
</BODY>
</HTML>

⑶ jsp連接access資料庫(不是用數據源)代碼出錯

先確認驅動包有沒有導入,程序的執行和IDE沒有太多關系,最好把異常信息貼出來

⑷ jsp 連接access資料庫

stat.setString(1,user1);
stat.setString(1,passw);

參數重了stat.setString(2,passw);

⑸ 我想把JSp連接access資料庫的代碼改寫成連接SQL2005資料庫的代碼,需要改哪裡

1.先換2005的jar包
2,ODBC數據源管理器,即為你的:String sConnStr = "jdbc:odbc:vote";中的vote.

網上找的(win7 32位)
1、開始菜單搜索框中輸入odbcad32.exe,打開ODBC數據源管理器;
xp的道理是一樣的

2、點擊「添加」,選擇「SQL Server」,點擊「完成」;

3、配置窗口輸入如下(名稱和描述可隨意):

名稱:HAO

描述:HAOODBC

伺服器(下拉框):(local)

4、勾選「使用用戶輸入登錄ID和密碼的SQL Server驗證」,並輸入登錄ID:sa和密碼:123(是安裝SQL 2005時已經設好的);

5、然後全部默認下一步直到最後完成前的安裝界面,點擊「測試數據源」,檢查是否配置正確連接正常;

6、打開MyEclipse測試:

package testODBC;

import java.sql.*;

public class TestODBC {
public static void main(String[] args){
Connection connection=null;
Statement stmt=null;
try{
connection=DriverManager.getConnection("jdbc:odbc:HAO","sa","123");
System.out.println("Success eastablishing the Connection...");
stmt=connection.createStatement();
System.out.println("Success Creating the Statement...");
connection.close();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace(System.out);
}

}
}

運行結果:

Success eastablishing the Connection...
Success Creating the Statement...

⑹ jsp怎樣連接access資料庫

先要創建數據源,步驟是 我的電腦-控制面板-管理工具-數據源-添加
在彈出的對話框里選擇access驅動程序,按完成後,會出現一個對話框,在這輸一個數據 源名,然後按下面的「選擇」按紐,選擇你建的資料庫。完成數據源的創建。
下面是一個例子,數據源的名稱是cust

⑺ jsp連接access,相同的代碼和文件,在別人電腦上能讀出內容,在我的電腦上就無法和資料庫建立聯系。

對於這種問題,有如下幾種情況:
1。在人家的電腦上,它把ACCESS資料庫加入到ODBC中,然後JSP用ODBC連接的,
解決方案:你在電腦上也建一個ODBC,數據源指向那ACCESS資料庫;
2。你電腦上沒裝ACCESS資料庫源,這是JSP直連的情況下。
解決方案:你裝一個OFFICE搞掂;
3。你的電腦的ACCESS軟體版本過高,而你的JAVA直連ACCESS驅動低了,
解決方案:升級資料庫連接驅動。

希望你能幫到你!

⑻ JSP連接Access資料庫!

<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
/*********************************
********* JDBC_ODBC連接Access資料庫,不需要設置數據源
********* Date: 2005.8
********* Email:[email protected]
********* Author: DreamTime [夢想年華]
********* 有任何歡迎提出指正
**********************************/

// ******* 資料庫連接代碼開始 *****
//異常處理語句
try
{
//以下幾項請自行修改
String spath = "db.mdb";//Access 資料庫路徑
String dbpath = application.getRealPath(spath);//轉化成物理路徑
String dbname = ""; //Acvess 資料庫用戶名,沒有則為空
String user = ""; //Access 資料庫密碼,沒有則為空
//資料庫連接字元串
String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath;
//載入驅動程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立連接
Connection conn= DriverManager.getConnection(url);
//創建語句對象
Statement stmt=conn.createStatement

(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// **** 資料庫連接代碼結束 ******

//********* 測試數據代碼開始 ******
//請在資料庫中建立username表,表中建立兩個欄位uid和pwd,類型為文本型
String sql = "select * from db";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
out.print("用戶名:" + rs.getString("name")+"<br>");
}
out.print("<br>恭喜你!資料庫連接成功!");
rs.close(); //關閉記錄集對象
stmt.close(); //關閉語句對象
conn.close(); //關閉連接對象
}catch(Exception e){
out.print("資料庫連接錯誤!,錯誤信息如下:<br>");
out.print(e.getMessage());
}
%>

⑼ 關於jsp連access資料庫的代碼

<%
String spath = "data/test.mdb";//Access 資料庫路徑
String dbpath = application.getRealPath(spath);//轉化成物理路徑
String dbname = ""; //Access 資料庫用戶名,沒有則為空
String user = ""; //Access 資料庫密碼,沒有則為空
//資料庫連接字元串
String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath;
//載入驅動程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立連接
Connection conn= DriverManager.getConnection(url);
//創建語句對象
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// **** 資料庫連接代碼 結束 ******
%>

⑽ jsp怎麼連接access資料庫

只需要在首頁或者登陸界面,加入以下代碼
① driverClass=」sun.jdbc.odbc.JdbcOdbcDriver」
② url=」jdbc:odbc:driver={Microsoft Access Driver(*.mdb)}:DBQ=E:/mydata.mdb」

2
mydata.mdb 是資料庫名 具體信息看下圖

3
舉一個例子
連接資料庫查詢表的相關語句(表userinfo中欄位有username,password,下同):
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:/mydata.mdb","","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from userinfo");
while(rs.next())
{
out.print("<br>用戶名:"+rs.getString("username")+"密碼:"+rs.getString("password"));
}
rs.close();
stmt.close();
conn.close();

END
通過JDBC-ODBC橋接器連接

首先設置odbc數據源,具體步驟為
打開控制面板,」性能與維護—》管理工具—》數據源(ODBC)」,打開數據源,如圖1所示:

點擊「系統DSN」,界面如圖

點擊添加,出現「創建新數據源」對話框

選擇「Driver do Microsoft Access (*.mdb)」,點擊完成,出現「ODBC Microsoft Access安裝」對話框

點擊資料庫的「選擇」按鈕,出現「選擇資料庫」對話框,選擇資料庫(如:E:\mydata.mdb)

點擊「確定」,返回圖4所示對話框,在「數據源名」處輸入數據源名稱(名稱可隨便起,如:myaccess),點擊「確定」。然後返回 「ODBC 數據源管理器」對話框,系統數據源中出現新建的數據源

相應的驅動和連接資料庫的url為:
classDriver=」sun.jdbc.odbc.JdbcOdbcDriver」
url=」jdbc:odbc:myaccess

舉個例子

接資料庫查詢表的相關語句:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:myaccess","","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from userinfo");
while(rs.next())
{
out.print("<br>用戶名:"+rs.getString("username")+"密碼:"+rs.getString("password"));
}
rs.close();
stmt.close();
conn.close();