/**
* Original version of this file was part of InterClient 2.01 examples
*
* Copyright InterBase Software Corporation, 1998. Written by
* com.inprise.interbase.interclient.r&d.PaulOstler :-)
*
* Code was modified by Roman Rokytskyy to show that Firebird JCA-JDBC driver
* does not introduce additional complexity in normal driver usage scenario.
*
* A small application to demonstrate basic, but not necessarily simple, JDBC
* features.
*
* Note: you will need to hardwire the path to your copy of employee.fdb as well
* as supply a user/password in the code below at the beginning of method
* main().
*/
public class DriverExample {
private static final String URL_DEFAULT = "jdbc:firebirdsql://localhost:3050/c:/database/employee.fdb";
private static final String USER_DEFAULT = "sysdba";
private static final String PASSWORD_DEFAULT = "masterkey";
private static final int REGISTER_CLASS_FOR_NAME = 1;
private static final int REGISTER_PROPERTIES = 2;
private static final int REGISTER_JDBC4 = 3;
private static final int CONNECT_DRIVERMANAGER = 1;
private static final int CONNECT_DRIVER = 2;
/**
* Make a connection to an employee.fdb on your local machine, and
* demonstrate basic JDBC features.
* <p>
* On the commandline a JDBC-url, username and password can be passed,
* otherwise defaults are used.
*/
public static void main(String args[]) throws Exception {
/* If localhost is not recognized, try using your local machine's name
* or the loopback IP address 127.0.0.1 in place of localhost.
*
* The Firebird JDBC driver recognizes a number of different URLs:
*
* Pure Java / Type 4:
* jdbc:firebirdsql://<host>[:<port>]/<alias-or-path-to-db>
* NOTE: On linux <alias-or-path-to-db> MUST include the root /
* Alternative URL format:
* jdbc:firebirdsql:<host>[/port]:<alias-or-path-to-db>
* Alternative prefix:
* jdbc:firebirdsql:java:
* Prefixes specifically for OpenOffice / LibreOffice Base:
* jdbc:firebirdsql:oo:
* jdbc:firebird:oo:
*
* Native / Type 2:
* jdbc:firebirdsql:native://<host>[:<port>]/<alias-or-path-to-db>
* Alternative URL format:
* jdbc:firebirdsql:native:<host[/port]:<alias-or-path-to-db>
*
* Native / Type 2 local connection:
* jdbc:firebirdsql:local:<alias-or-path-to-db>
*
* Firebird Embedded:
* jdbc:firebirdsql:embedded:alias-or-path-to-db>
*
* The URL can be extended with properties to configure the connection
* (similar to a HTTP query string) by adding to the end of the string:
* ?<property>[=<value>](&<property>[=<value>])*
*
* Examples:
* Connecting to employee.fdb on localhost using the type4 driver with UTF8:
* jdbc:firebirdsql://localhost/C:/database/employee.fdb?lc_ctype=UTF8
*
* Connecting to dialect1.fdb using native with WIN1252 and resultset tracking disabled:
* jdbc:firebirdsql://localhost/C:/database/dialect1.fdb?dialect=1&lc_ctype=WIN1252&noResultSetTracking
*/
String databaseURL = args.length == 0 ? URL_DEFAULT : args[0];
String user = args.length < 2 ? USER_DEFAULT : args[1];
String password = args.length < 3 ? PASSWORD_DEFAULT : args[2];
/* Here are the JDBC objects we're going to work with.
* We're defining them outside the scope of the try block because
* they need to be visible in a finally clause which will be used
* to close everything when we are done.
*/
java.sql.Driver driver = null;
java.sql.Connection con = null;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
try {
/* Before a JDBC driver can be used, it must have been
* registered with the DriverManager.
*
* Demonstrate the different methods to register the
* Firebird JCA-JDBC driver with the driver manager
*/
int registrationAlternative = REGISTER_CLASS_FOR_NAME;
switch (registrationAlternative) {
case REGISTER_CLASS_FOR_NAME:
/* For JDBC 3.0 and earlier, the standard method of registering
* the driver is by loading the class.
*
* Class.forName() instructs the java class loader to load
* and initialize a class. As part of the class initialization
* any static clauses associated with the class are executed.
*
* Every driver class is required by the jdbc specification to
* create an instance of itself and register that instance with
* the DriverManager when the driver class is loaded by the java
* classloader (this is done via a static clause associated with
* the driver class).
*
* Notice that the driver name could have been supplied
* dynamically, so that an application is not hardwired to any
* particular driver as would be the case if a driver constructor
* were used, eg. new org.firebirdsql.jdbc.FBDriver().
*/
try {
Class.forName("org.firebirdsql.jdbc.FBDriver");
} catch (java.lang.ClassNotFoundException e) {
// A call to Class.forName() forces us to consider this exception
System.out.println("Firebird JCA-JDBC driver not found in class path");
System.out.println(e.getMessage());
return;
}
break;
case REGISTER_PROPERTIES:
/* Add the Firebird JCA-JDBC driver name to your system's
* jdbc.drivers property list.
*
* The driver manager will load drivers from this system
* property list.
*/
java.util.Properties sysProps = System.getProperties();
StringBuffer drivers = new StringBuffer("org.firebirdsql.jdbc.FBDriver");
String oldDrivers = sysProps.getProperty("jdbc.drivers");
if (oldDrivers != null)
drivers.append(":" + oldDrivers);
sysProps.put("jdbc.drivers", drivers.toString());
System.setProperties(sysProps);
break;
case REGISTER_JDBC4:
/* From JDBC 4.0 (Java 6), drivers are required to have a file
* /META-INF/services/java.sql.Driver with the
* classname(s) of the drivers.
*
* The DriverManager will automatically load all drivers,
* so there is no need to explicitly load the driver.
*/
break;
}
/* At this point the driver should be registered with the DriverManager.
* Try to find the registered driver that recognizes Firebird URLs
*/
try {
// We pass the entire database URL, but we could just pass "jdbc:firebirdsql:"
driver = java.sql.DriverManager.getDriver(databaseURL);
System.out.println("Firebird JCA-JDBC driver version " + driver.getMajorVersion() + "."
+ driver.getMinorVersion() + " registered with driver manager.");
} catch (java.sql.SQLException e) {
System.out.println("Unable to find Firebird JCA-JDBC driver among the registered drivers.");
showSQLException(e);
return;
}
/* Now that Firebird
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Jaybird 2.2.11 for Java 8 / JDBC 4.2, zip kit (230个子文件)
stylesheet.css 13KB
jaybird22_x64.dll 710KB
jaybird22.dll 543KB
jaybird22_x64.dll 149KB
jaybird22.dll 123KB
index-all.html 1.64MB
ISCConstants.html 818KB
constant-values.html 622KB
AbstractDatabaseMetaData.html 308KB
AbstractResultSet.html 293KB
AbstractCallableStatement.html 247KB
AbstractPreparedStatement.html 152KB
FBWrappingDataSource.html 145KB
release_notes.html 143KB
AbstractFBConnectionPoolDataSource.html 139KB
GDS.html 130KB
AbstractConnection.html 126KB
AbstractStatement.html 118KB
FBManagedConnectionFactory.html 110KB
FBSimpleDataSource.html 100KB
ServiceRequestBuffer.html 99KB
FBAbstractCommonDataSource.html 95KB
FBConnectionProperties.html 93KB
FBManagedConnection.html 66KB
FBCallableStatement.html 64KB
DatabaseParameterBuffer.html 62KB
BasicAbstractConnectionPool.html 60KB
FBMaintenanceManager.html 59KB
AbstractDriverConnectionPoolDataSource.html 58KB
XSQLVAR.html 57KB
overview-tree.html 56KB
AbstractPingablePooledConnection.html 54KB
MaintenanceManager.html 53KB
FBResultSet.html 52KB
FirebirdConnectionProperties.html 49KB
FBBlob.html 49KB
FBEscapedFunctionHelper.html 45KB
FBBackupManager.html 45KB
FBDatabaseMetaData.html 42KB
FBPreparedStatement.html 41KB
FBResultSetMetaData.html 41KB
FBEscapedParser.html 39KB
FBServiceManager.html 38KB
deprecated-list.html 38KB
FirebirdPool.html 37KB
BackupManager.html 37KB
FBConnectionRequestInfo.html 36KB
FBManager.html 36KB
FBConnectionPoolDataSource.html 36KB
FirebirdConnection.html 35KB
FBUpdatableCursorFetcher.html 34KB
FBEventManager.html 34KB
FBArray.html 33KB
FBTpbMapper.html 33KB
FBConnection.html 30KB
FBNBackupManager.html 30KB
FBParameterMetaData.html 30KB
FBSQLException.html 30KB
AbstractDriver.html 30KB
FBClob.html 30KB
package-tree.html 30KB
FBStatement.html 29KB
FBProcedureCall.html 29KB
TransactionParameterBuffer.html 28KB
FBUserManager.html 28KB
AbstractConnectionPool.html 28KB
allclasses-frame.html 28KB
FBBlobInputStream.html 27KB
FirebirdParameterMetaData.html 27KB
InternalTransactionCoordinator.html 27KB
DriverConnectionPoolDataSource.html 27KB
FBCachedBlob.html 27KB
package-summary.html 27KB
FBUser.html 26KB
FBTraceManager.html 26KB
GDSException.html 26KB
FBStatisticsManager.html 26KB
FBConnectionPoolDataSource.html 25KB
ServiceParameterBuffer.html 25KB
FirebirdPreparedStatement.html 25KB
FBXADataSource.html 25KB
serialized-form.html 25KB
FBLocalTransaction.html 24KB
allclasses-noframe.html 24KB
faq.html 23KB
FirebirdBlob.BlobInputStream.html 22KB
FBManagerMBean.html 22KB
ConnectionPoolConfiguration.html 22KB
EventManager.html 22KB
FirebirdDatabaseMetaData.html 21KB
FBConnectionHelper.html 21KB
IscStmtHandle.html 21KB
XAConnectionHandler.html 21KB
FBDataSource.html 21KB
AbstractPooledConnection.html 20KB
FBResourceException.html 20KB
FBRowUpdater.html 20KB
PingablePooledConnection.html 19KB
IscDbHandle.html 19KB
FBXAException.html 19KB
共 230 条
- 1
- 2
- 3
资源评论
Winsky
- 粉丝: 15
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功