Results 1 to 8 of 8
  1. #1
    hitesh is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Exclamation multiple java files with single connection code

    Hi I m developing a database application using java ,netbeans and mysql

    Now I write the following code in every file or class to connect to database

    public void Connect()
    {
    try
    {
    // Load the JDBC driver
    String driverName = "com.mysql.jdbc.Driver";

    Class.forName(driverName);

    // Create a connection to the database
    String url = "jdbc:mysql://***.***.***.***:3306/abc";
    String username = "xyz";
    String pass = "xyz";
    con1=DriverManager.getConnection(url, username,pass);
    }
    catch (ClassNotFoundException e)
    {

    System.out.println("Connection Failed");
    System.out.println(e);
    }
    catch(SQLException e)
    {
    System.out.println("Connection Failed");
    }

    }

    But now the application become very lazy or slow because every file or class connects to the database separately and it takes time

    I want some solution that the application connects to the database only once and every class or file will use the object of that connection

    thanks

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Have a class separatley with singleton method for managing a connection and then use it.

    Once all the operations are over you can close the connection.
    Ramya:cool:

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hitesh View Post
    But now the application become very lazy or slow because every file or class connects to the database separately and it takes time

    I want some solution that the application connects to the database only once and every class or file will use the object of that connection
    You already answered your own question: keep the connection open until you're done with it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    For better performance you can look into Connection Pooling:
    Chapter 8 Continued: Connection Pooling

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    You can use following line by putting it in a method:

    connection = DriverManager.getConnection(databaseUrl, userName, password);

    make a call to this method whenever you need connection. But, do keep in mind synchronization issues (as multi-threaded access to this method would create problem). You can use connection-pool for this.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Sounds to me like you have db code scattered throughout your program, rather than keepig it contained in its own layer. Also, is this a desktop app or a webapp? Is the db on the same machine? Have you profiled the app to see where the slowdown is occurring? I ask that because with modern drivers the time taken to make a connection is pretty small.

  7. #7
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default make separate code for database connectivity

    make separte file in which u write code for database connectivity code in that return static variable of type Connection inside the static method and use that variable wherever u want database connectivity

  8. #8
    hitesh is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    Hi
    Thank u everybody
    my problem is solved
    I create a singleton class to manage database connection and call it where I need database connection through out the application.

Similar Threads

  1. Replies: 1
    Last Post: 10-25-2010, 11:21 PM
  2. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  3. Multiple Java in a single BOX
    By luke_devon in forum New To Java
    Replies: 2
    Last Post: 07-05-2010, 01:59 PM
  4. Single Tomcat Multiple Developers
    By Quentinb in forum Eclipse
    Replies: 3
    Last Post: 06-14-2010, 01:40 PM
  5. multiple users with single connection
    By nishi.kishore in forum Java Servlet
    Replies: 3
    Last Post: 09-06-2009, 03:15 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
  •