Accessing connection from different class in Java MySQL

0 votes
I've been working with MySQL for quite a while now, but I recently started with Java and its OOP concepts. Now, I need my DB connection to be open for the duration of time that my application runs for, and also a server socket. So, should I be implementing them in different classes? Please suggest how I can keep accessing the connection.
Nov 14, 2018 in Database by Bharani
• 4,660 points
1,059 views

1 answer to this question.

0 votes

You should just instantiate DoComms with every previous connection you get. Say, something like this:

DoComms conn_c = new DoComms(server, con);

That way DoComms will be able to hold a reference to that connection for use later.

But always make sure that after getting the connection, you also close it in the finally block before instantiating your DoComms objects. So, once you finish processing everything, get to closing your connection like this:

try {
  // get connection

  // do stuff in threads
}
catch {
  // handle
}
finally {
  con.close();
}

And, for long-lived applications, use connection-pooling (e.g. C3P0 or Apache DBCP) to open/close connections as and when required. Although, that entirely depends on if the requirements of your application really need something like that.

answered Nov 14, 2018 by nirvana
• 3,130 points

Related Questions In Database

0 votes
1 answer

Connection in Hbase

Following code is used to open a ...READ MORE

answered Jul 11, 2018 in Database by Data_Nerd
• 2,390 points
253 views
0 votes
1 answer

Types of tables in MySQL

MyISAM is the default table that is ...READ MORE

answered Aug 21, 2018 in Database by CodingByHeart77
• 3,720 points
1,140 views
0 votes
1 answer

TRIGGERS used in MySQL tables

The following TRIGGERS are allowed in MySQL:  BEFORE ...READ MORE

answered Aug 21, 2018 in Database by CodingByHeart77
• 3,720 points
253 views
0 votes
1 answer

Find values in a comma separated string in a MySQL query

You can add commas to the left and ...READ MORE

answered Sep 10, 2018 in Database by Sahiti
• 6,380 points
11,649 views
0 votes
1 answer

How to do a batch insert in MySQL

You can try out the following query: INSERT ...READ MORE

answered Sep 10, 2018 in Database by Sahiti
• 6,380 points
1,699 views
0 votes
1 answer

What are the different authentication modes in SQL Server? How can it be changed?

Windows mode and Mixed Mode – SQL ...READ MORE

answered Oct 29, 2018 in Database by Sahiti
• 6,380 points
651 views
0 votes
0 answers

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

This is the exception on getConnection(): java.sql.SQLException: Access denied ...READ MORE

May 16 in Java by Kichu
• 19,040 points
2,695 views
0 votes
0 answers

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname [duplicate]

I have this Java program: MySQLConnectExample.java import java.sql.*; import java.util.Properties; public ...READ MORE

Aug 21 in Database by Kithuzzz
• 12,240 points
105 views
0 votes
0 answers
0 votes
0 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP