適用初學者的jsp連接access數據庫代碼,剛剛寫的一個java web 中連接access數據庫的代碼。這些天講到數據庫操作,但是我的本子是xp home版,安裝sql server比較麻煩,干脆弄個access來演示好了。有時小型桌面數據庫還是蠻實用的嘛,哈~
復制內容到剪貼板代碼:
<%@ page contenttype="text/html; charset=gb18030" %>
<%@page import="java.sql.*" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<form method="post" action="/web/index.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 module目錄下的,當然可以自行改變路徑*/
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或javabean連接access的代碼,摘自【http://blog.csdn.net/rimoer/archive/2007/04/06/1554842.aspx】
我寫的一個用jsp連接access數據庫的代碼。
要正確的使用這段代碼,你需要首先在access數據庫里創建一username表,表里面創建兩個字符型的字段,字段名分別為:uid,pwd,然后插入幾條測試數據。
歡迎各位提出改進的意見。
以下用兩種方式來實現jsp連接access數據庫。
第一種jsp形式。
復制內容到剪貼板代碼:
<%@ 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 = "data/test.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 username";
resultset rs = stmt.executequery(sql);
while(rs.next())
{
out.print("用戶名:" + rs.getstring("uid"));
out.print(" 密碼:" + rs.getstring("pwd") + "<br>");
}
out.print("<br>恭喜你!數據庫連接成功!");
rs.close(); //關閉記錄集對象
stmt.close(); //關閉語句對象
conn.close(); //關閉連接對象
}catch(exception e){
out.print("數據庫連接錯誤!,錯誤信息如下:<br>");
out.print(e.getmessage());
}
//******* 測試數據代碼結束 *******
%>
第二種,javabean的形式。
復制內容到剪貼板代碼:
/*
***************************************
* 作用: java連接access數據庫代碼
* 作者:夢想年華
* email:[email protected]
* author:夢想年華
* copyright(c)2005-2006 by dreamtime
******** *******************************
*/
[/color]
package conn; //導入包
import java.sql.*; //導入數據庫操作的類
public class dbconnaccess //構造方法,初始化
{
private connection conn; //連接對象
private statement stmt; //語句對象
private resultset rs; //結果集對象
private string accessdriver; //保存access驅動程序字符串
private string accessurl; //保存access連接字符串
public dbconnaccess()
{
//access驅動程序
accessdriver = "sun.jdbc.odbc.jdbcodbcdriver";
//連接字符串
accessurl = "jdbc:odbc:driver={microsoft access driver (*.mdb)};dbq=";
conn = null;
}
//該方法從參數得到數據庫路徑,并加到連接字符串后面,然后再建立連接
public connection getconntoaccess(string dbpath){
try{
accessurl=accessurl+dbpath;
class.forname(accessdriver);
conn = drivermanager.getconnection(accessurl);
}catch(exception e){
system.out.println("操作數據庫出錯,請仔細檢查");
system.err.println(e.getmessage());
}
return conn;
}
//關閉數據庫連接
public void close()
{
try{
//rs.close();
//stmt.close();
conn.close();
}catch(sqlexception sqlexception){
sqlexception.printstacktrace();
}
}
}
調用方法如下:
復制內容到剪貼板代碼:
<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 數據庫
string dbpath="data/test.mdb"; //數據庫的路徑,請自行修改
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();
%>
新聞熱點
疑難解答