Java Tutorial/Database/Driver
Содержание
- 1 A List of JDBC Drivers: connection string, driver name
- 2 boolean java.sql.Driver.jdbcCompliant()
- 3 boolean java.sql.DriverPropertyInfo.required (Is property value required?)
- 4 DriverManager.getDrivers(): enumerate all the loaded JDBC drivers:
- 5 DriverPropertyInfo[] java.sql.Driver.getPropertyInfo(String url, Properties info)
- 6 Enable JDBC logging
- 7 Get a List of all Available Parameters for Creating a JDBC Connection
- 8 Getting Information about the Driver
- 9 int java.sql.Driver.getMajorVersion()
- 10 int java.sql.Driver.getMinorVersion()
- 11 Listing All Available Parameters for Creating a JDBC Connection
- 12 Setting the Login Timeout
- 13 Specify a CharSet when connecting to a DBMS
- 14 String[] java.sql.DriverPropertyInfo.choices (Get possible choices for property; if null, value can be any string)
- 15 String java.sql.DriverPropertyInfo.description (Get description of property)
- 16 String java.sql.DriverPropertyInfo.name (Get name of property)
- 17 String java.sql.DriverPropertyInfo.value (Get current value)
A List of JDBC Drivers: connection string, driver name
IBM DB2
jdbc:db2://<HOST>:<PORT>/<DB>
COM.ibm.db2.jdbc.app.DB2Driver
JDBC-ODBC Bridge
jdbc:odbc:<DB>
sun.jdbc.odbc.JdbcOdbcDriver
Microsoft SQL Server
jdbc:weblogic:mssqlserver4:<DB>@<HOST>:<PORT>
weblogic.jdbc.mssqlserver4.Driver
Oracle Thin
jdbc:oracle:thin:@<HOST>:<PORT>:<SID>
oracle.jdbc.driver.OracleDriver
PointBase Embedded Server
jdbc:pointbase://embedded[:<PORT>]/<DB>
com.pointbase.jdbc.jdbcUniversalDriver
Cloudscape
jdbc:cloudscape:<DB>
COM.cloudscape.core.JDBCDriver
Cloudscape RMI
jdbc:rmi://<HOST>:<PORT>/jdbc:cloudscape:<DB>
RmiJdbc.RJDriver
Firebird (JCA/JDBC Driver)
jdbc:firebirdsql:[//<HOST>[:<PORT>]/]<DB>
org.firebirdsql.jdbc.FBDriver
IDS Server
jdbc:ids://<HOST>:<PORT>/conn?dsn="<ODBC_DSN_NAME>"
ids.sql.IDSDriver
Informix Dynamic Server
jdbc:informix-sqli://<HOST>:<PORT>/<DB>:INFORMIXSERVER=<SERVER_NAME>
com.informix.jdbc.IfxDriver
InstantDB (v3.13 and earlier)
jdbc:idb:<DB>
jdbc.idbDriver
InstantDB (v3.14 and later)
jdbc:idb:<DB>
org.enhydra.instantdb.jdbc.idbDriver
Interbase (InterClient Driver)
jdbc:interbase://<HOST>/<DB>
interbase.interclient.Driver
Hypersonic SQL (v1.2 and earlier)
jdbc:HypersonicSQL:<DB>
hSql.hDriver
Hypersonic SQL (v1.3 and later)
jdbc:HypersonicSQL:<DB>
org.hsql.jdbcDriver
Microsoft SQL Server (JTurbo Driver)
jdbc:JTurbo://<HOST>:<PORT>/<DB>
com.ashna.jturbo.driver.Driver
Microsoft SQL Server (Sprinta Driver)
jdbc:inetdae:<HOST>:<PORT>?database=<DB>
com.inet.tds.TdsDriver
Microsoft SQL Server 2000 (Microsoft Driver)
jdbc:microsoft:sqlserver://<HOST>:<PORT>[;DatabaseName=<DB>]
com.microsoft.jdbc.sqlserver.SQLServerDriver
MySQL (MM.MySQL Driver)
jdbc:mysql://<HOST>:<PORT>/<DB>
org.gjt.mm.mysql.Driver
Oracle OCI 8i
jdbc:oracle:oci8:@<SID>
oracle.jdbc.driver.OracleDriver
Oracle OCI 9i
jdbc:oracle:oci:@<SID>
oracle.jdbc.driver.OracleDriver
PostgreSQL (v6.5 and earlier)
jdbc:postgresql://<HOST>:<PORT>/<DB>
postgresql.Driver
PostgreSQL (v7.0 and later)
jdbc:postgresql://<HOST>:<PORT>/<DB>
org.postgresql.Driver
Sybase (jConnect 4.2 and earlier)
jdbc:sybase:Tds:<HOST>:<PORT>
com.sybase.jdbc.SybDriver
Sybase (jConnect 5.2)
jdbc:sybase:Tds:<HOST>:<PORT>
com.sybase.jdbc2.jdbc.SybDriver
//To test your driver once it"s installed, try the following code:
Class.forName("Driver name");
Connection con = DriverManager.getConnenction("jdbcurl","username","password");
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);
}
}
}
boolean java.sql.DriverPropertyInfo.required (Is property value required?)
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;
}
}
}
DriverManager.getDrivers(): enumerate all the loaded JDBC drivers:
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);
}
}
}
JDBC Driver=org.hsqldb.jdbcDriver@19efb05 JDBC Driver=org.hsqldb.jdbcDriver@723d7c
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();
}
}
Get a List of all Available Parameters for Creating a JDBC Connection
Driver.getPropertyInfo() returns a list of all available properties that can be supplied when using the driver to create a JDBC connection.
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
public class Main {
public static void main(String[] args) throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
Driver driver = DriverManager.getDriver(url);
DriverPropertyInfo[] info = driver.getPropertyInfo(url, null);
for (int i = 0; i < info.length; i++) {
System.out.println(info[i].name);
// Is property value required?
System.out.println(info[i].required);
// Get current value
System.out.println(info[i].value);
// Get description of property
System.out.println(info[i].description);
// Get possible choices for property;
// if null, value can be any string
String[] choices = info[i].choices;
if (choices != null) {
for (int c = 0; c < choices.length; c++) {
System.out.println(choices[c]);
}
}
}
}
}
Getting Information about the Driver
import java.sql.Driver;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Driver myDriver = new org.hsqldb.jdbcDriver();
DriverManager.registerDriver(myDriver);
System.out.println(myDriver.acceptsURL("jdbc:mydriver://tutorial/"));
System.out.println("Major Version: " +
myDriver.getMajorVersion());
System.out.println("Minor Version: " +
myDriver.getMinorVersion());
System.out.println("JDBC COMPLIANT driver? "
+ myDriver.jdbcCompliant());
}
}
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);
}
}
}
Listing All Available Parameters for Creating a JDBC Connection
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;
}
}
}
Setting the Login Timeout
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getHSQLConnection();
conn.setAutoCommit(false);
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,name varchar(30));");
st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM survey");
outputResultSet(rs);
rs.close();
st.close();
conn.close();
}
private static void outputResultSet(ResultSet rs) throws Exception {
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
for (int i = 1; i < numberOfColumns + 1; i++) {
String columnName = rsMetaData.getColumnName(i);
System.out.print(columnName + " ");
}
System.out.println();
System.out.println("----------------------");
while (rs.next()) {
for (int i = 1; i < numberOfColumns + 1; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
DriverManager.setLoginTimeout(60); // fail after 60 seconds
return DriverManager.getConnection(url, "sa", "");
}
}
Specify a CharSet when connecting to a DBMS
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class Main {
public static void main(String[] argv) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Properties prop = new Properties();
prop.put("charSet", "iso-8859-7");
prop.put("user", "your username");
prop.put("password", "your password");
// Connect to the database
Connection con = DriverManager.getConnection("url", prop);
}
}
String[] java.sql.DriverPropertyInfo.choices (Get possible choices for property; if null, value can be any string)
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;
}
}
}
String java.sql.DriverPropertyInfo.description (Get description of property)
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;
}
}
}
String java.sql.DriverPropertyInfo.name (Get name of property)
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;
}
}
}
String java.sql.DriverPropertyInfo.value (Get current value)
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;
}
}
}