Java Tutorial/Database/Access

Материал из Java эксперт
Перейти к: навигация, поиск

Create a connection to MS Access database

import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
  // private static final String URL = "jdbc:odbc:TestDB";
  private static final String USERNAME = "admin";
  private static final String PASSWORD = "welcome";
  private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
  private static final String URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\Database\\testdb.mdb;}";
  public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    connection.close();
  }
}





Using DSN-less connection

import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
  public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/data.MDB";
    Connection conn = DriverManager.getConnection(myDB, "", "");
  }
}