| Author |
Connection Through JDBC
|
Harry Singh
Ranch Hand
Joined: May 02, 2001
Posts: 124
|
|
Hi Java Gurus, IS there any way by which i can make DB connection through java except calling DriverManager.getConnection(); Suppose i wanna give the name of driver at runtime.. then how can i do that? Can i give the JDBC driver as system property? if yes, then how wil i handle multiple drivers Thanks in advance Regards, Harjinder
|
 |
Laurent Leonard
Ranch Hand
Joined: May 15, 2001
Posts: 35
|
|
Here is a class exemple : JDBCConnectorFactory.java package com.lle.dbutils; import org.apache.log4j.Category; import org.apache.log4j.Logger; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; public class JDBCConnectorFactory { static Category cat = Logger.getInstance(JDBCConnectorFactory.class.getName()); public static Connection getConnection(String databaseName, boolean autocommit) { ResourceBundle properties = null; Connection connection = null; String driverName = ""; String url = ""; String username = ""; String password = ""; try { properties = PropertyResourceBundle.getBundle("com.lle.databases"); driverName = properties.getString(databaseName + ".drivername"); url = properties.getString(databaseName + ".url"); username = properties.getString(databaseName + ".username"); password = properties.getString(databaseName + ".password"); // Load the JDBC driver Class.forName(driverName); // Create a connection to the database cat.debug("Trying to connect with " + url); connection = DriverManager.getConnection(url, username, password); connection.setAutoCommit(autocommit); cat.debug("Connection done."); } catch (ClassNotFoundException e) { // Could not find the database driver cat.error("Class not found : " + driverName); } catch (SQLException e) { // Could not connect to the database cat.error("SQL Error during the connection : " + e.getMessage()); } catch (MissingResourceException mre) { // Missing property in the resource file if (mre.getKey().equals("")) { cat.error("Missing resource file : " + mre.getMessage()); } else { cat.error("Missing property (" + mre.getKey() + ") in the resource file : " + mre.getMessage()); } } return connection; } public static Connection getConnection(String databaseName) { return getConnection(databaseName, false); } } Here is the property files #configuration for Development oracle.thin.drivername = oracle.jdbc.driver.OracleDriver oracle.thin.url = jdbc racle:thin:@localhost:1521 ev oracle.thin.username = scott oracle.thin.password = tiger #oracle.oci.drivername = oracle.jdbc.driver.OracleDriver #oracle.oci.url = jdbc racle ci:scott/tiger@MYSID #oracle.oci.username = scott #oracle.oci.password = tiger sqlserver.thin.drivername = com.microsoft.jdbc.sqlserver.SQLServerDriver sqlserver.thin.url = jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;DatabaseName=myDB sqlserver.thin.username = user sqlserver.thin.password = password [ June 17, 2003: Message edited by: Laurent Leonard ]
|
Laurent LEONARD
|
 |
Avi Abrami
Ranch Hand
Joined: Oct 11, 2000
Posts: 1112
|
|
Hi Harjinder, The answers to your queries can be found in Getting Started with the JDBC API. This is part of the J2SE SDK documentation, so if you have downloaded the documentation, you can view this document offline. IS there any way by which i can make DB connection through java except calling "DriverManager.getConnection();" You should find the answer in chapter 2Suppose i wanna give the name of driver at runtime.. then how can i do that? You load the driver via "Class.forName()". This method takes a "String" parameter, so all you need to do is obtain that "String" at runtime. Perhaps as a command-line argument, or reading it from the console, or from a "JTextField" in a GUI.Can i give the JDBC driver as system property? See chapter 3 Hope this has helped you. Good Luck, Avi.
|
 |
 |
|
|
subject: Connection Through JDBC
|
|
|