Java/Database SQL JDBC/Driver

Материал из Java эксперт
Версия от 06:33, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

boolean java.sql.Driver.jdbcCompliant()

  
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Collections;
import java.util.List;
public class Main {
  public static void main(String[] argv) throws Exception {
    List drivers = Collections.list(DriverManager.getDrivers());
    for (int i = 0; i < drivers.size(); i++) {
      Driver driver = (Driver) drivers.get(i);

      String name = driver.getClass().getName();
      System.out.println(name);

      int majorVersion = driver.getMajorVersion();
      System.out.println(majorVersion);
      int minorVersion = driver.getMinorVersion();
      System.out.println(minorVersion);
      boolean isJdbcCompliant = driver.jdbcCompliant();
      System.out.println(isJdbcCompliant);
    }
  }
}





DriverPropertyInfo[] java.sql.Driver.getPropertyInfo(String url, Properties info)

  
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);
    String url = "jdbc:mysql://a/b";
    Driver driver = DriverManager.getDriver(url);
    DriverPropertyInfo[] info = driver.getPropertyInfo(url, null);
    for (int i = 0; i < info.length; i++) {
      String name = info[i].name;
      boolean isRequired = info[i].required;
      String value = info[i].value;
      String desc = info[i].description;
      String[] choices = info[i].choices;
    }
  }
}





Enable JDBC logging

  
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Main {
  public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection dbConn = DriverManager.getConnection(URL, "user", "pass");
    PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    DriverManager.setLogWriter(w);
    dbConn.close();
    PreparedStatement prepstmt;
    prepstmt = dbConn.prepareStatement("SELECT id FROM employee");
    prepstmt.execute();
    prepstmt.close();
    dbConn.close();
  }
}





Enumeration<Driver> java.sql.DriverManager.getDrivers()

  
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Collections;
import java.util.List;
public class Main {
  public static void main(String[] argv) throws Exception {
    List drivers = Collections.list(DriverManager.getDrivers());
    for (int i = 0; i < drivers.size(); i++) {
      Driver driver = (Driver) drivers.get(i);

      String name = driver.getClass().getName();
      System.out.println(name);

      int majorVersion = driver.getMajorVersion();
      System.out.println(majorVersion);
      int minorVersion = driver.getMinorVersion();
      System.out.println(minorVersion);
      boolean isJdbcCompliant = driver.jdbcCompliant();
      System.out.println(isJdbcCompliant);
    }
  }
}





Get Driver Name

 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    ResultSet rs = null;
    DatabaseMetaData meta = conn.getMetaData();
    System.out.println(meta.getDriverName());
    st.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





Get Driver Property Info

 
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.util.Properties;
public class Main {
  public static void main(String[] args) throws Exception {
    Class.forName("org.gjt.mm.mysql.Driver");
    Properties info = new Properties();
    Driver driver = DriverManager.getDriver("jdbc:mysql://localhost/demo2s");
    System.out.println("driver=" + driver);
    DriverPropertyInfo[] attributes = driver.getPropertyInfo("jdbc:mysql://localhost/demo2s", info);
    System.out.println("attributes=" + attributes);
    // zero length means a connection attempt can be made
    System.out.println("Resolving properties for: " + driver.getClass().getName());
    for (int i = 0; i < attributes.length; i++) {
      // get the property metadata
      String name = attributes[i].name;
      String[] choices = attributes[i].choices;
      boolean required = attributes[i].required;
      String description = attributes[i].description;
      // printout property metadata
      System.out.println(name + " (Required: " + required + ")");
      if (choices == null) {
        System.out.println(" No choices.");
      } else {
        System.out.print(" Choices are: ");
        for (int j = 0; j < choices.length; j++) {
          System.out.print(" " + choices[j]);
        }
      }
      System.out.println(" Description: " + description);
    }
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





Get Driver Version

 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    ResultSet rs = null;
    DatabaseMetaData meta = conn.getMetaData();
    System.out.println(meta.getDriverVersion());
    st.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





int java.sql.Driver.getMajorVersion()

  
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Collections;
import java.util.List;
public class Main {
  public static void main(String[] argv) throws Exception {
    List drivers = Collections.list(DriverManager.getDrivers());
    for (int i = 0; i < drivers.size(); i++) {
      Driver driver = (Driver) drivers.get(i);

      String name = driver.getClass().getName();
      System.out.println(name);

      int majorVersion = driver.getMajorVersion();
      System.out.println(majorVersion);
      int minorVersion = driver.getMinorVersion();
      System.out.println(minorVersion);
      boolean isJdbcCompliant = driver.jdbcCompliant();
      System.out.println(isJdbcCompliant);
    }
  }
}





int java.sql.Driver.getMinorVersion()

  
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Collections;
import java.util.List;
public class Main {
  public static void main(String[] argv) throws Exception {
    List drivers = Collections.list(DriverManager.getDrivers());
    for (int i = 0; i < drivers.size(); i++) {
      Driver driver = (Driver) drivers.get(i);

      String name = driver.getClass().getName();
      System.out.println(name);

      int majorVersion = driver.getMajorVersion();
      System.out.println(majorVersion);
      int minorVersion = driver.getMinorVersion();
      System.out.println(minorVersion);
      boolean isJdbcCompliant = driver.jdbcCompliant();
      System.out.println(isJdbcCompliant);
    }
  }
}





JDBC Driver Information

 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    DatabaseMetaData meta = conn.getMetaData();
    // Oracle (and some other vendors) do not support
    // some the following methods; therefore, we need
    // to use try-catch block.
    try {
      int jdbcMajorVersion = meta.getJDBCMajorVersion();
      System.out.println("jdbcMajorVersion:" + jdbcMajorVersion);
    } catch (Exception e) {
      System.out.println("jdbcMajorVersion unsupported feature");
    }
    try {
      int jdbcMinorVersion = meta.getJDBCMinorVersion();
      System.out.println("jdbcMinorVersion:" + jdbcMinorVersion);
    } catch (Exception e) {
      System.out.println("jdbcMinorVersion unsupported feature");
    }
    String driverName = meta.getDriverName();
    String driverVersion = meta.getDriverVersion();
    System.out.println("driverName=" + driverName);
    System.out.println("driverVersion=" + driverVersion);
    st.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





Listing All Loaded JDBC Drivers and gets information about each one.

  
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Collections;
import java.util.List;
public class Main {
  public static void main(String[] argv) throws Exception {
    List drivers = Collections.list(DriverManager.getDrivers());
    for (int i = 0; i < drivers.size(); i++) {
      Driver driver = (Driver) drivers.get(i);
      String name = driver.getClass().getName();
      System.out.println(name);
      int majorVersion = driver.getMajorVersion();
      System.out.println(majorVersion);
      int minorVersion = driver.getMinorVersion();
      System.out.println(minorVersion);
      boolean isJdbcCompliant = driver.jdbcCompliant();
      System.out.println(isJdbcCompliant);
    }
  }
}





Loading a JDBC Driver: call Class.forName() within the code

  
public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);
  }
}