日期:2014-05-18  浏览次数:20705 次

JSP连接Access的代码
JSP连接Access的代码怎么写?

------解决方案--------------------
String url = "jdbc:odbc:DNSName ";
String user = " ";
String password = " ";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
Connection conn = DriverManager.getConnection(url, user, password);
String sql= "select * from tablename where XXXX "
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{ .....
}
rs.close();
stmt.close();
conn.close();
在系统中控制面板-管理工具-数据源要配置access数据源。

------解决方案--------------------
String url = "jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};DBQ = c:\\test.mdb ";
------解决方案--------------------
<%@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>