zoukankan      html  css  js  c++  java
  • 在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()这个方法,会有问题。

  • 相关阅读:
    蓝桥杯--算法训练 区间k大数查询
    vijos1782:借教室
    vijos1779国王游戏
    C++大数模板
    HDU1042(N!:设4为基数)
    HDU1026(延时迷宫:BFS+优先队列)
    POJ3984(迷宫问题)
    HDU3018:Ant Trip(欧拉回路)
    HDU5438:Ponds(拓扑排序)
    2008北航:字符串匹配
  • 原文地址:https://www.cnblogs.com/andgoo/p/2287574.html
Copyright © 2011-2022 走看看