Java/Database SQL JDBC/Access

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

Connect to a database via JDBC-ODBC

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
  public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c = DriverManager
        .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");
    ResultSet rs = c.createStatement().executeQuery("select objet from Employee");
    while (rs.next()) {
      System.out.println(rs.getString("objet"));
    }
    rs.close();
    c.close();
  }
}





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