Jsp连接Access数据库(不通过建立ODBC数据源的方法)
1. 在站点(我的站点为:E:\javatest)下建立文件夹“AccessDb”并在文件夹下建立数据库“test.mdb”以及表“stu”,stu字段为 id,stuname test.mdb所在路径为:E:\javatest\AccessDb 下面
如下图:
2.在站点(E:\javatest)下建立测试连接数据库文件“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 为数据库所在绝对路径
//相对路径
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数据源连接
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>
运行结果如下:
如下图:
2.在站点(E:\javatest)下建立测试连接数据库文件“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 为数据库所在绝对路径
//相对路径
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数据源连接
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>
运行结果如下:
相关推荐
风之羽翼 2020-09-14
daillo 2020-04-20
踩风火轮的乌龟 2020-04-19
hithyc 2020-04-17
snowguy 2020-04-10
yhyily 2019-12-15
yangliuhbhd 2020-09-24
luotuofeile 2020-07-28
Strongding 2020-06-25
xuanwenchao 2020-06-14
昊 2020-06-04
ysmh00 2020-05-14
ErixHao 2020-05-08
数据库之扑朔迷离 2020-05-06
whyname 2020-04-29
hitxueliang 2020-04-18
zengfanpei 2020-03-08