国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

適用初學(xué)者的JSP連接Access數(shù)據(jù)庫代碼

瀏覽:42日期:2024-07-04 13:03:35

Web開發(fā)編程軟件 界面設(shè)計(jì)軟件!

適用初學(xué)者的JSP連接Access數(shù)據(jù)庫代碼,剛剛寫的一個(gè)Java Web 中連接Access數(shù)據(jù)庫的代碼。這些天講到數(shù)據(jù)庫操作,但是我的本子是XP home版,安裝SQL Server比較麻煩,干脆弄個(gè)ACCESS來演示好了。有時(shí)小型桌面數(shù)據(jù)庫還是蠻實(shí)用的嘛,哈~

復(fù)制內(nèi)容到剪貼板代碼:

<%@ 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("數(shù)據(jù)庫驅(qū)動(dòng)程序裝入錯(cuò)誤");

}

try{

String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+request.getRealPath("/")+"test.mdb";

//特別注意上面的Driver和(*.mdb)之間是有空格的

/*這個(gè)test.mdb文件是存放在Web Module目錄下的,當(dāng)然可以自行改變路徑*/

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表中三個(gè)字段,主鍵是ID,自增的。username和password是文本類型。

out.println("<br>");

}

rs.close();

stmt.close();

conn.close();

}

catch(Exception ex){

out.print(ex);

}

%>

</form>

</body>

</html>

下面是網(wǎng)上搜的一些JSP或JavaBean連接ACCESS的代碼,摘自【http://blog.csdn.net/rimoer/archive/2007/04/06/1554842.aspx】

我寫的一個(gè)用jsp連接Access數(shù)據(jù)庫的代碼。

要正確的使用這段代碼,你需要首先在Access數(shù)據(jù)庫里創(chuàng)建一username表,表里面創(chuàng)建兩個(gè)字符型的字段,字段名分別為:uid,pwd,然后插入幾條測試數(shù)據(jù)。

歡迎各位提出改進(jìn)的意見。

以下用兩種方式來實(shí)現(xiàn)jsp連接access數(shù)據(jù)庫。

第一種JSP形式。

復(fù)制內(nèi)容到剪貼板代碼:

<%@ page contentType="text/html; charset=gb2312" language="java"

import="java.sql.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<%

; /*********************************

; ;********* ;JDBC_ODBC連接Access數(shù)據(jù)庫,不需要設(shè)置數(shù)據(jù)源

; ;********* ;Date: 2005.8

; ;********* ;Email:fanwsp@126.com

; ;********* ;Author: DreamTime [夢想年華]

; ;********* ;有任何歡迎提出指正 ;;

; ;**********************************/

; // ******* 數(shù)據(jù)庫連接代碼 開始 ;*****

; //異常處理語句

; try

; {

//以下幾項(xiàng)請(qǐng)自行修改

String spath = "data/test.mdb";//Access 數(shù)據(jù)庫路徑

String dbpath = application.getRealPath(spath);//轉(zhuǎn)化成物理路徑

String dbname = "" ; ; ;//Acvess 數(shù)據(jù)庫用戶名,沒有則為空

String user = "" ; ; ;//Access 數(shù)據(jù)庫密碼,沒有則為空

//數(shù)據(jù)庫連接字符串;

String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath;;

//加載驅(qū)動(dòng)程序

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//建立連接

Connection conn= DriverManager.getConnection(url);;

//創(chuàng)建語句對(duì)象

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

// **** ; 數(shù)據(jù)庫連接代碼 結(jié)束 ******

//********* ;測試數(shù)據(jù)代碼開始 ;******

//請(qǐng)?jiān)跀?shù)據(jù)庫中建立username表,表中建立兩個(gè)字段uid和pwd,類型為文本型

String sql = "select * from username";

ResultSet rs = stmt.executeQuery(sql);

while(rs.next())

{

; out.print("用戶名:" + rs.getString("uid"));

; out.print(" ;密碼:" + rs.getString("pwd") + "<br>");

}

out.print("<br>恭喜你!數(shù)據(jù)庫連接成功!");

rs.close(); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;//關(guān)閉記錄集對(duì)象

stmt.close(); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;//關(guān)閉語句對(duì)象

conn.close(); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;//關(guān)閉連接對(duì)象

; }catch(Exception e){

out.print("數(shù)據(jù)庫連接錯(cuò)誤!,錯(cuò)誤信息如下:<br>");

out.print(e.getMessage());

}

; //******* ; 測試數(shù)據(jù)代碼結(jié)束 ;*******

%>

第二種,JavaBean的形式。

復(fù)制內(nèi)容到剪貼板代碼:

/*

***************************************

* ; ; ; ; ; ;作用: java連接Access數(shù)據(jù)庫代碼 ;

* ; ;作者:夢想年華

* ; ;Email:fanwsp@126.com

* ; ;Author:夢想年華

* ; ; CopyRight(c)2005-2006 by DreamTime;

******** *******************************

*/

[/color]

package conn; ; ; ; ; ; ; ; ; ; ; ; ; ; ;//導(dǎo)入包

import java.sql.*; ; ; ; ; ; ; ; ; ; ; ; ;//導(dǎo)入數(shù)據(jù)庫操作的類

public class DBConnAccess ; ; ; ; ; ; ; ;//構(gòu)造方法,初始化

{

; private Connection conn; ; ; ; ; ; ;//連接對(duì)象

; private Statement stmt; ; ; ; ; ; ; ; ;//語句對(duì)象

; private ResultSet rs; ; ; ; ; ; ; ; ;//結(jié)果集對(duì)象

; private String accessdriver; ; ; ; ;//保存Access驅(qū)動(dòng)程序字符串

; private String accessURL; ; ; ; ; ; ; //保存Access連接字符串

; public DBConnAccess()

; {

//Access驅(qū)動(dòng)程序

accessdriver = "sun.jdbc.odbc.JdbcOdbcDriver" ; ; ;

//連接字符串

accessURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";

conn = null;

; }

; //該方法從參數(shù)得到數(shù)據(jù)庫路徑,并加到連接字符串后面,然后再建立連接

; public Connection getConnToAccess(String dbpath){

try{

; accessURL=accessURL+dbpath;

; Class.forName(accessdriver);

; conn = DriverManager.getConnection(accessURL);

; }catch(Exception e){

; ; ; System.out.println("操作數(shù)據(jù)庫出錯(cuò),請(qǐng)仔細(xì)檢查");

; ; ; System.err.println(e.getMessage());

; }

return conn;

; }

;;

;;

; ; ;;

;//關(guān)閉數(shù)據(jù)庫連接

; public void close()

; {

try{

; //rs.close();

; //stmt.close();

; conn.close();;

}catch(SQLException sqlexception){

; sqlexception.printStackTrace();

}

; }

}

調(diào)用方法如下:

復(fù)制內(nèi)容到剪貼板代碼:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<%@ page contentType="text/html; charset=gb2312" language="java";

import="java.sql.*" ;%>

<jsp:useBean id="DBConn" scope="page" class="conn.DBConnAccess"/>

<%;

; //連接Access 數(shù)據(jù)庫

; String dbpath="data/test.mdb" ; ; ; ; ;//數(shù)據(jù)庫的路徑,請(qǐng)自行修改

; Connection conn=DBConn.getConnToAccess(application.getRealPath(dbpath));

; Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

String sql="select * from username order by id";

; //String sql1="insert into username (uid,pwd) values('wsp','wsp')";

; //stmt.executeUpdate(sql1);

; ResultSet rs=stmt.executeQuery(sql);;

; while(rs.next()){

; out.print("用戶名:");

; out.print(rs.getString("uid")+" 密碼:");

; out.println(rs.getString("pwd")+"<br>");

; }

; DBConn.close();

%>

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 萨嘎县| 曲沃县| 威信县| 武陟县| 海丰县| 德庆县| 和龙市| 平江县| 久治县| 三亚市| 治县。| 安康市| 瑞丽市| 黔西县| 岳普湖县| 天长市| 镇康县| 嘉义县| 齐河县| 长丰县| 包头市| 庆元县| 柳河县| 密山市| 古浪县| 竹北市| 灵宝市| 马公市| 富裕县| 竹北市| 津南区| 沂源县| 三江| 宁武县| 通江县| 兴业县| 隆昌县| 江西省| 邯郸县| 浦江县| 乃东县|