<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <% String spath = "//db//test.mdb"; //Access 数据库路径 String dbPath = application.getRealPath(spath); //转化成物理路径
//在Servlet中无application,此时应采用下列方式
//String dbPath= this.getServletContext().getRealPath(spath) ; String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbPath; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection(url); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("SELECT * FROM roomclass"); out.print("<table border=1>"); out.print("<tr>"); out.print("<td>"+"列1"+"</td>"); out.print("<td>"+"列2"+"</td>"); out.print("<td>"+"列3"+"</td>"); out.print("<td>"+"列4"+"</td>"); out.print("<td>"+"列5"+"</td>"); out.print("</tr>"); while(rs.next()) { out.print("<tr>"); out.print("<td>"+rs.getInt(1)+" "+"</td>"); out.print("<td>"+rs.getString(2)+"</td>"); out.print("<td>"+rs.getString(3)+"</td>"); out.print("<td>"+rs.getString(4)+"</td>"); out.print("<td>"+rs.getString(5)+"</td>"); out.print("</tr>"); } out.print("</table>"); rs.close(); stmt.close(); conn.close(); } catch(Exception e) { out.print(e); } %> </body> </html>