Java by API/java.sql/DriverManager — различия между версиями

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

Текущая версия на 14:46, 31 мая 2010

DriverManager: getConnection(String url) (jdbc:odbc:technical_library)

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      // Define the data source for the driver
      String sourceURL = "jdbc:odbc:technical_library";
      // Create a connection through the DriverManager 
      Connection databaseConnection = DriverManager.getConnection(sourceURL);
      System.out.println("Connection is: "+databaseConnection);
    } catch(ClassNotFoundException cnfe) {
      System.err.println(cnfe);
    } catch(SQLException sqle) {
      System.err.println(sqle);
    }
  }
}





DriverManager: getConnection(String url, Properties info)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Main {
  public static Connection getConnection(String dbURL, String user, String password)
      throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    Properties props = new Properties();
    props.put("user", user);
    props.put("password", password);
    props.put("autoReconnect", "true");
    return DriverManager.getConnection(dbURL, props);
  }
  public static void main(String[] args) {
    Connection conn = null;
    try {
      conn = getConnection("jdbc:mysql://localhost/empDB", "root", "pass");
    } catch (Exception ex) {
      System.out.println("SQLException: " + ex.getMessage());
    }
  }
}





DriverManager: getConnection(String url, String user, String password)

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





DriverManager: getDrivers()

 

import java.sql.Driver;
import java.sql.DriverManager;
public class Main {
  public static void main(String[] args) throws Exception {
    Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
    DriverManager.registerDriver((Driver) driverClass.newInstance());
    // Print out all loaded JDBC drivers.
    java.util.Enumeration e = java.sql.DriverManager.getDrivers();
    while (e.hasMoreElements()) {
      Object driverAsObject = e.nextElement();
      System.out.println("JDBC Driver=" + driverAsObject);
    }
  }
}





DriverManager: registerDriver(Driver driver)

 
import java.sql.Driver;
import java.sql.DriverManager;
public class Main {
  public static void main(String[] args) throws Exception {
    Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
    DriverManager.registerDriver((Driver) driverClass.newInstance());
    // Print out all loaded JDBC drivers.
    java.util.Enumeration e = java.sql.DriverManager.getDrivers();
    while (e.hasMoreElements()) {
      Object driverAsObject = e.nextElement();
      System.out.println("JDBC Driver=" + driverAsObject);
    }
  }
}





DriverManager: setLogWriter(PrintWriter out)

 
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;
public class Main {
  public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {
      // Load the jdbc-odbc bridge driver
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      // Enable logging
      DriverManager.setLogWriter(new PrintWriter((System.err)));
      System.out.println("Getting Connection");
      Connection conn = DriverManager.getConnection(dbURL, "user", "password");
      SQLWarning warn = conn.getWarnings();
      while (warn != null) {
        System.out.println("SQLState: " + warn.getSQLState());
        System.out.println("Message:  " + warn.getMessage());
        System.out.println("Vendor:   " + warn.getErrorCode());
        System.out.println("");
        warn = warn.getNextWarning();
      }
      conn.close();
    } catch (ClassNotFoundException e) {
      System.out.println("Can"t load driver " + e);
    } catch (SQLException e) {
      System.out.println("Database access failed " + e);
    }
  }
}