Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Through JDBC

 
Harry Singh
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 2
  • Suppose 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic