/*
* ConnectionTest.java
* -------------------
* CoersionTest.java
*
* Demonstrates the use of subprotocal in creating and connecting to
* dtF/SQL databases.
*
*
* 6-Jul-1999 Will Gilbert, Gilbert@Informagen.com
*
*----------------------------------------------------------------------------------------
*
* DriverManager.getConnection(String jdbcURL);
* DriverManager.getConnection(String jdbcURL, Properties properties);
* DriverManager.getConnection(String jdbcURL, String username, String password);
*
*
* URL
* ---
* Format: "jdbc:dtF:<database path>;<attr=value>;...;<attr=value>"
*
* <database path/file> as determined by File.getCanonicalPath()
*
* or
*
* relative paths will use System.getProperty("user.dir") as the anchor.
*
*
* Attribute/Value pairs
* ---------------------
* Format: "jdbc:dtF:<database path>;attr=value;...;attr=value
*
* Attributes:
* UID | USR | user - login username
* PWD | password - login password
*
* Create - What to do if it can't find the database file:
* "always" - always create a new database, deleting any old ones.
* "never" - Don't create a new database even if one doesn't exist.
* "maybe" - create a new database only if one doesn't exist (the default).
* APPL - Macintosh application creator type to assign to the database & BLOB file,
* defaults to "dtFA", the dtFAdmin application,
* DType - Macintosh file type of database file, defaults to "DTFD" used by dtFAdmin.
* BType - Macintosh file type of BLOB file, defaults to "DTFB" used by dtFAdmin.
* BAPPL - Application creator for BLOB file. If left unspecified and BTYPE has
* been specified the value of APPL will be used for BAPPL. In other words
* "APPL=MyAp;DTYPE=data;BTYPE=blob" is equivalent to but simplier than
* "APPL=MyAp;DTYPE=data;BAPPL=MyAp;BTYPE=blob"
*
*
*
* Properties
* ----------
* By using a prefix of "dtF." in the properties file a single property file
* could be used with a multitude of JDBC connections. The "dtF." properties
* would be ignored by other JDBC implementations. The exceptions are "user" and
* "password" which are used by JDBC, as well as UID and PWD which are used by
* ODBC.
*
*
* Keys:
* UID | user | dtF.Username - logon username
* PWD | password | dtF.Password - logon password
*
* dtF.dbpath - <database path/file>
* dtF.Create - What to do if you can't find the database
* "always" - always create a new database, deleting the old one
* "never" - Don't create a new database if one exists
* "maybe" - create a new one only if one doesn't exist (the default)
* dtF.APPL - Macintosh creator type to assign to the database & BLOB file,
* defaults to "swBT", the dtFAdmin application
* dtF.DType - Macintosh file type of database file, defaults to "DTFD"
* dtF.BType - Macintosh file type of BLOB file, defaults to "DTFB"
*/
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionTest {
public static void main(String args[]) {
// - Can we find the Driver library and the native shared library
try {
Class.forName("com.informagen.jdbc.dtF.Driver");
} catch(ClassNotFoundException e) {
System.out.println(e.toString());
}
final String username = "dtfadm";
final String password = "dtfadm";
String jdbcURL = null;
Properties theProperties = new Properties();
theProperties.put("dtF.Username", username);
theProperties.put("dtF.Password", password);
File defaultDir = new File (System.getProperty("user.dir"));
File dbFile = new File(defaultDir, "Sample Database");
Connection theConnection = null;
try {
// Using a properties object with the full path specifed
theProperties.put("dtF.dbpath", dbFile.getAbsolutePath());
System.out.println("\nOpening a connection using full paths.");
System.out.println("Database: " + theProperties.getProperty("dtF.dbpath"));
theProperties.put("dtF.Create", "always");
theConnection = DriverManager.getConnection("jdbc:dtF", theProperties);
theProperties.remove("dtF.Create");
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
System.out.println("\nAttempting a second connection to newly created database.");
theConnection = DriverManager.getConnection("jdbc:dtF", theProperties);
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
try {
// Using a properties object with the relative path (/sub) specified
File subDir = new File(defaultDir, File.separator + "sub");
if ( subDir.exists() == false )
subDir.mkdir();
theProperties.put("dtF.dbpath", "sub" + File.separator + dbFile.getName());
System.out.println("\nOpening a connection using relative path.");
System.out.println("Database: " + theProperties.getProperty("dtF.dbpath"));
theConnection = DriverManager.getConnection("jdbc:dtF", theProperties);
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
try {
// Using a properties object with the database name only
theProperties.put("dtF.dbpath", dbFile.getName());
System.out.println("\nOpening a connection using filename only in properties.");
System.out.println("Database: " + theProperties.getProperty("dtF.dbpath"));
theConnection = DriverManager.getConnection("jdbc:dtF", theProperties);
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
// Using a URL with username and password
try {
jdbcURL = "jdbc:dtF:" + dbFile.getName();
System.out.println("\nOpening a connection using getConnection(\"" + jdbcURL + "\"," + username +
", " + password +");");
theConnection = DriverManager.getConnection(jdbcURL, username, password);
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
// Using a URL with username and password specified in the subprotocal
try {
jdbcURL = "jdbc:dtF:" + dbFile.getName() + ";UID=dtfadm;PWD=dtfadm";
System.out.println("\nOpening a connection using getConnection(\"" + jdbcURL + "\");");
theConnection = DriverManager.getConnection(jdbcURL);
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.\n");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
// Create a database with a custom icon.
try {
jdbcURL = "jdbc:dtF:E. coli;USR=dtfadm;PWD=dtfadm;Create=always;APPL=GIVE;DType=FILE";
System.out.println("\nOpening a connection using getConnection(\"" + jdbcURL + "\");");
theConnection = DriverManager.getConnection(jdbcURL);
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
// Opening a connection with a bad username/password.
try {
jdbcURL = "jdbc:dtF:" + dbFile.getName();
System.out.println("\nOpening a connection with a bad username/password. " + jdbcURL);
theConnection = DriverManager.getConnection(jdbcURL, "foo", "bar");
if (theConnection == null)
System.out.println("Driver failed - null Connection returned.");
else
System.out.println("-- Success");
theConnection.close();
} catch(SQLException sqle) {
System.out.println(sqle.toString());
}
System.out.println("\nConnection Test Done.");
}
}