• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

JDBC connectivity without mentioning DSN

hi Freinds,,

   i am in a big trouble now. My java programme needs the connectivity to oracle,informix,ms access databases. but i don't know the correct dsn's for that. i heard that this can be possible by using pure JDBC driver without using the jdbc:odbc bridge.but i am confused with that..can u guys help me with that..

thanks in advance,

Jim
0
jimaricat072100
Asked:
jimaricat072100
  • 4
  • 3
  • 2
  • +1
1 Solution
 
tonyjohnCommented:
I dont think you have a pure JDBC driver for MS Access. For this case you will have to use JDBC-ODBC. I am sure  Oracle has a JDBC driver but have no idea about Informix. With the Oracle JDBC drivers all you need to specify is the database name, connect string, userid and password and JDBC will use SQL*Net to connect to the Oracle database without needing to go through ODBC.
0
 
dorothy2Commented:
Write a DataAccess class which relies on a properties file to configure the JDBC driver name and the database URL. Put the connection logic inside the DataAccess class. You can deploy the same code and use a different properties file for each configuration. I did this in the project we are now working on - I prototyped it in Access and did later development in Informix. The Informix JDBC driver does not handle the database URL the same way that Oracle and Access do, so you are better off putting the entire URL in your properties file instead of separate properties for user, password, port, etc.

Good luck

Dorothy
0
 
kvkameshCommented:
HI
use oracle thin jdbc type 4 driver. In this there is no need to use DSN name. simple use the fallowing parameters jdbc:oracle:thin:usename/scott@hostname:portnumber:sid, this is for oracle.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kvkameshCommented:
HI
use oracle thin jdbc type 4 driver. In this there is no need to use DSN name. simple use the fallowing parameters jdbc:oracle:thin:usename/password@hostname:portnumber:sid, this is for oracle.
0
 
jimaricat072100Author Commented:
hi tony..

  i desperately need the help for Access,Oracle nd Informix. i accept u'r answer..but i am still waiting for a good one whic can suit these three databases.

thank you for u'r support..

Jim
0
 
dorothy2Commented:
You need to write a data access class to handle the connection. You need to download JDBC drivers for every database you intend to support. I don't think the Oracle thin driver will work for other databases, because I believe it only works on Oracle. You may be stuck with the JDBC/ODBC bridge for MS Access, but this is the slowest driver so you don't want to use it for your database access unless you have no alternative.

Your data access class needs to be able to set the database URL and database driver class file name dynamically. A properties file is probably the easiest way to go. Then you can compile the data access source code and use the same java class files for every database installation, only changing the properties file.

Here is a bit of code to get you started -

      // we have written a Property Manager class; the "pm"
      // reference is to an instance of this property manager.
      // We also have a boolean flag DEBUG_ON which we
      // turn on during development and off when deploying.

      // The reference is to Informix, because that is what we
      // are using, however this bit of code should work with
      // Oracle and Access as well, provided their JDBC drivers
      // allow you to specify the database URL string, rather
      // than setting individual properties like user name, password,
      // etc.

      // Your database driver jar files have to be in your class
      // path, or you will get a Class Not Found error.

       String URLString = (String) pm.getProperty("DatabaseURL");
       String ifxDriver = (String) pm.getProperty("DriverName");

       setURLString(URLString);
       if(DEBUG_ON)
         System.out.println("Connection from: " + URLString);

         // Register the INFORMIX-JDBC driver
       Driver IfmxDrv = (Driver) Class.forName(ifxDriver).newInstance();
       if(IfmxDrv == null)
         System.out.println("Driver null.");

      // Get a connection to the database server
       _conn = DriverManager.getConnection(_URLString);

       if(_conn == null)
           throw new Exception("Attempted database connnection failed.");

Dorothy

0
 
kvkameshCommented:
HI
This is 100% answer for your question. To connect to the Oracle database try this example. you have to download the orcale type 4 driver "classes111.zip" file from www.otnxchange. and set the path in your system like this. say you have jdk in C drive. c:\jdk1.2.2\lib\classes111.zip.

output of the below program will give you number of records in emp table.

--------------------------------------------------------
import java.sql.*;
class ConnectToOracle {

static final String driver_class = "oracle.jdbc.driver.OracleDriver";
static final String driver_type = "jdbc:oracle:thin";

static final String user_name      = "scott";
static final String pass_word      = "tiger";
static final String host_name      = "200.100.50.137";
static final String port_number = "1521";
static final String data_base      = "orcl";
static final String table_name      = "emp";

static final String connect_string = driver_type +":"+ user_name +"/"+ pass_word +"@"+ host_name +":"+ port_number +":"+ data_base;


public static void main(String argv[])
 {
 try
{

 Class.forName(driver_class);
 
Connection conn = DriverManager.getConnection(connect_string);


Statement stmt = conn.createStatement();


String sqlCommand = new String("SELECT count(*) from " + table_name);


ResultSet rs = stmt.executeQuery(sqlCommand);


 while (rs.next())
{


 int rcount = rs.getInt(1);
 
 System.out.println(rcount + " records in " + table_name);
 }
 
 rs.close();
 stmt.close();
 conn.close();
 
 }
 
 catch( Exception e )
{
 System.out.println(e.getMessage());
 e.printStackTrace();
}
return;
}
 }
----------------------------------------------------
YOu have to use different drivers to connect different databases.
Informix you have to use weblogic Jdriver
------------------------------------------------------
Connecting to an Informix DBMS

The following steps tell you how to set up your application to connect to Informix using WebLogic jDriver for Informix. In general, connecting happens in three steps:


Load the JDBC driver by calling Class.forName().newInstance() with the full class name of the WebLogic jDriver for Informix JDBC driver class, and casting it to a java.sql.Driver object. This procedure properly loads and registers the JDBC driver. For example:

Driver myDriver = (Driver) Class.forName("weblogic.jdbc.informix4.Driver").newInstance();


Create a java.util.Properties object describing the connection. This object contains name-value pairs containing information such as user name, password, database name, server name, and port number. For example:

Properties props = new Properties();props.put("user",         "scott");props.put("password",     "secret");props.put("db",           "myDB");props.put("server",       "myHost");props.put("port",         "8659");


Create a JDBC Connection object, which becomes an integral piece in your JDBC operations, by calling the Driver.connect() method, which takes as its parameters the URL of the driver and the java.util.Properties object you created in step 2. For example:

Connection conn =    myDriver.connect("jdbc:weblogic:informix4", props);

In steps 1 and 3, you are describing the JDBC driver: in the first step, you use the full package name of the driver. Note that it is dot-delimited. In the third step, you identify the driver with its URL, which is colon-delimited. The URL must include at least weblogic:jdbc:informix4, and may include other information, including the server host name and the database name.
-----------------------------------------------------
For MS access you can'not connect to with type 4 driver.

i think you have clear understanding now.
You have to accept it. this is 100% answer for you Question.
0
 
jimaricat072100Author Commented:
Thanks KVKAMESH
0
 
kvkameshCommented:
hi
Jimaricat
thanks for accepting my answer and solving your probelm. i have 4 years experience in IT field you can ask me any Q?, i will give you exact answer not comment.thank you kamesh
0
 
jimaricat072100Author Commented:
Hi kamesh,..

Thanks for u'r invitation. I will try to contact u once i could get u'r mail id.

Regarding the answer,..one more doubt,..please..

above said answer is only for oracle right..?
Is this the same for anyother databases.Should we have to download thier own thin drivers,...?

Expecting from u soon..

Jimmy
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now