服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java访问access的一般方法介绍


  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;
   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;
   }
  }

扫描关注微信公众号