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()这个方法,会有问题。

  • 相关阅读:
    python函数
    文件操作
    python列表,元组,字典,集合简介
    python字符串(str)
    python数字类型 or 进制转换
    流程控制
    Python入门
    Python垃圾回收机制
    python简介&下载&安装
    DAY11
  • 原文地址:https://www.cnblogs.com/andgoo/p/2287574.html
Copyright © 2011-2022 走看看