Results 1 to 3 of 3
  1. #1
    peiceonly is offline Member
    Join Date
    Mar 2007
    Rep Power

    Default Connecting to a database

    hi all
    How can I connect to a database ?
    Please reply in detail with jars and all

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Rep Power


    The traditional way to establish a connection with a database is to call the method DriverManager.getConnection. This method takes a string containing a URL. The DriverManager class, referred to as the JDBC management layer, attempts to locate a driver that can connect to the database represented by that URL. The DriverManager class maintains a list of registered Driver classes, and when the method getConnection is called, it checks with each driver in the list until it finds one that can connect to the database specified in the URL. The Driver method connect uses this URL to actually establish the connection.

    A user can bypass the JDBC management layer and call Driver methods directly. This could be useful in the rare case that two drivers can connect to a database and the user wants to explicitly select a particular driver. Normally, however, it is much easier to just let the DriverManager class handle opening a connection.

    The following code exemplifies opening a connection to a database located at the URL jdbc:odbc:wombat with a user ID of oboy and 12Java as the password:

    String url = "jdbc:odbc:wombat";
    Connection con = DriverManager.getConnection(url, "oboy", "12Java");

    The JDBC 2.0 Standard Extension API provides the DataSource interface as an alternative to the DriverManager for establishing a connection. When a DataSource class has been implemented appropriately, a DataSource object can be used to produce Connection objects that participate in connection pooling and/or Connection objects that can participate in distributed transactions. See the chapter "DataSource" for more information and to see example code for creating a connection using a DataSource object.

    An application uses a Connection object produced by a DataSource object in essentially the same way it uses a Connection object produced by the DriverManager. There are some differences, however. If the Connection object is a pooled connection, an application should include a finally block to assure that the connection is closed even if an exception is thrown. That way a valid connection will always be put back into the pool of available connections.

    If a Connection object is part of a distributed transaction, an application should not call the methods Connection.commit or Connection.rollback, nor should it turn on the connection's auto-commit mode. These would interfere with the transaction manager's handling of the distributed transaction.

  3. #3
    mageshwari is offline Member
    Join Date
    Apr 2008
    Rep Power

    Smile new to java

    hai friends,i m new to java.2day i start my payroll live project.if i have any doubt i will post ,friends pls help me

Similar Threads

  1. connecting to a database
    By mxb7642 in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 08:39 AM
  2. problem in connecting to mysql database
    By nancyv in forum Java Servlet
    Replies: 6
    Last Post: 04-02-2008, 11:33 AM
  3. Replies: 0
    Last Post: 02-13-2008, 11:26 AM
  4. Error while connecting to a database
    By Fukushuusha in forum JDBC
    Replies: 2
    Last Post: 12-16-2007, 09:40 AM
  5. connecting to mysql database
    By javagal in forum NetBeans
    Replies: 2
    Last Post: 08-04-2007, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts