在Servlet中连接Access

 Connection conn = null;          
Statement stmt = null;
ResultSet rs = null;
try{

String MdbPath = new java.io.File(this.getServletContext().getRealPath("DBDemo.mdb")).getParent() + "\\DB\\DBDemo.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url= "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + MdbPath ;
conn = DriverManager.getConnection(url);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch(SQLException e){
log.error(e);
} catch (ClassNotFoundException e) {

e.printStackTrace();
}



String ID = request.getParameter("modId");

try {
rs = stmt.executeQuery("select * from template_file where templateID="+ID);
while(rs.next()) {
try {
java.io.InputStream in = rs.getBinaryStream("filebody");
java.io.OutputStream outStream = response.getOutputStream();
byte[] buf = new byte[1024];
int bytes = 0;
while((bytes = in.read(buf)) != -1)
outStream.write(buf, 0, bytes);
in.close();
outStream.close();
}
catch(Throwable e) {
log.error(e);
}
}
rs.close();
stmt.close();
conn.close();

注意,不推荐使用request.getRealPaht()这个方法,会有问题。

原文地址:https://www.cnblogs.com/andgoo/p/2287574.html