熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Java 訪問 Access 的一般方法介紹

2022-06-13   來源: JSP教程 

  import javasql*;
  
  public class AccessDB {
   String driver = sunjdbcodbcJdbcOdbcDriver;
   String url = jdbc:odbc:Driver={MicroSoft Access Driver *mdb)};DBQ = Northwindmdb;
   String user = ;
   String pwd = ;
   Connection conn;
   Statement stmt;
   ResultSet rs;
  
   public MyDB() {
    try {
     ClassforName(driver);
    } catch (Exception e) {
     Systemoutprintln(e);
    }
   }
  
   //創建不可滾動的連接
   public void connect() {
    try {
     conn = DriverManagergetConnection(urluserpwd);
     stmt = conncreateStatement();
    } catch (Exception e) {
     Systemoutprintln(e);
    }
   }
   //創建可以滾動的連接
   public void connect() {
    try {
     conn = DriverManagergetConnection(urluserpwd);
     stmt = conncreateStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY);
    } catch (Exception e) {
     Systemoutprintln(e);
    }
   }
  
   //關閉連接
   public void close() {
    try {
     if (stmt != null) {
      stmtclose();
     }
     if (conn != null) {
      connclose();
     }
    } catch (Exception e) {
     Systemoutprintln(e);
    }
   }
  
   //查詢語句
   public ResultSet executeQuery(String sql) {
    try {
     if (stmt == null) {
      connect();
     }
     rs = stmtexecuteQuery(sql);
    } catch (Exception e) {
     Systemoutprintln(e);
    }
    return rs;
   }
  }
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19541.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.