Java by API/java.sql/Vendor Driver

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

org.hsqldb.jdbcDriver

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    setup();
  }
  static Connection conn;
  static Statement st;
  public static void setup() {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





sun.jdbc.odbc.JdbcOdbcDriver

import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
  public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:yourDBName";
    System.out.println("Attempting to connect to " + url);
    try {
      System.out.println("Loading the driver...");
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("Establishing a connection...");
      Connection connection = DriverManager.getConnection(url);
      System.out.println("Connect to " + connection.getCatalog() + " a success!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}