Java访问Access的一般方法介绍

发表于:2007-06-22来源:作者:点击数: 标签:
import java . sql .*; public class AccessDB { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:Driver={MicroSoft Access Driver *.mdb)};DBQ = Northwind.mdb"; String user = ""; String pwd = ""; Connection conn; Stateme

   
  import java.sql.*;
  
  public class Aclearcase/" target="_blank" >ccessDB {
   String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
   String url = "jdbc:odbc:Driver={MicroSoft Access Driver *.mdb)};DBQ = Northwind.mdb";
   String user = "";

   String pwd = "";
   Connection conn;
   Statement stmt;
   ResultSet rs;
  
   public MyDB() {
    try {
     Class.forName(driver);
    } catch (Exception e) {
     System.out.println(e);
    }
   }
  
   //创建不可滚动的连接
   public void connect() {
    try {
     conn = DriverManager.getConnection(url,user,pwd);
     stmt = conn.createStatement();
    } catch (Exception e) {
     System.out.println(e);
    }
   }
   //创建可以滚动的连接
   public void connect2() {
    try {
     conn = DriverManager.getConnection(url,user,pwd);
     stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    } catch (Exception e) {
     System.out.println(e);
    }
   }
  
   //关闭连接
   public void close() {
    try {
     if (stmt != null) {
      stmt.close();
     }
     if (conn != null) {
      conn.close();
     }
    } catch (Exception e) {
     System.out.println(e);
    }
   }
  
   //查询语句
   public ResultSet executeQuery(String sql) {
    try {
     if (stmt == null) {
      connect();
     }
     rs = stmt.executeQuery(sql);
    } catch (Exception e) {
     System.out.println(e);
    }
    return rs;
   }
  }

原文转自:http://www.ltesting.net