当前位置: 代码迷 >> J2EE >> JSP如何连接Access数据库
  详细解决方案

JSP如何连接Access数据库

热度:630   发布时间:2013-11-16 17:57:23.0
<%@page contentType="text/html;charset=gb2312"%> 
<%@page import="java.sql.*"%> 
<html> 
<body> 
<% 
try{ 
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
} 
catch(ClassNotFoundException e){ 
  out.print(e); 
} 
try{ 
  String strDirPath=application.getRealPath(request.getRequestURI()); 
  strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\"; 
  String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath+"jcc.mdb"; 
  Connection conn = DriverManager.getConnection(url); 
  Statement stmt = conn.createStatement(); 
  ResultSet rs = stmt.executeQuery("Select * FROM user"); 
  out.println("User-list"+"<br>"); 
  while(rs.next()){ 
    out.print(rs.getString(1)+" "); 
    out.print(rs.getString(2)+"<br>"); 
  } 
  rs.close(); 
  stmt.close(); 
  conn.close(); 
} 
catch(Exception ex){ 
  out.print(ex); 
} 
%> 
</body> 
</html>

  相关解决方案