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,550 points
49 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,060 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,360 points
59 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,690 points
147 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,690 points
31 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 darklord
• 6,170 points
2,799 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 darklord
• 6,170 points
398 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 darklord
• 6,170 points
77 views
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
220 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
• 56,940 points
374 views
0 votes
1 answer

Find Oracle JDBC driver in Maven repository

Download the jar and place it in ...READ MORE

answered Sep 26, 2018 in Java by Daisy
• 8,050 points
1,149 views
0 votes
1 answer

Error while connecting to Hive using Java JDBC

Use ​org.apache.hive.jdbc.HiveDriver as your driver. It ...READ MORE

answered Nov 21, 2018 in Big Data Hadoop by Omkar
• 67,660 points
146 views