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
898 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
214 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,087 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
209 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,360 points
11,231 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,360 points
1,632 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,360 points
575 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
• 16,850 points
79 views
0 votes
0 answers
0 votes
1 answer

How to retrieve column names from java.sql.ResultSet?

You may refer the below code: ResultSet ...READ MORE

answered Jul 4, 2018 in Java by sophia
• 1,400 points
1,675 views
0 votes
1 answer

Establish JDBC Connection in R through single sign-on

You can certainly connect to databases with ...READ MORE

answered Sep 12, 2018 in AWS by Priyaj
• 58,060 points
1,296 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP