This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC " Watch "JDBC " New topic


Anubhav Singhal

Joined: Jan 30, 2013
Posts: 4
Hi, i got this code from my friend, making it more standardized. Previously i use simple code that is written in try catch block here, but after i got this code i didn't understand the use of if condition here. When the connection parameters are in try catch black then why this code
return con;

is used here................Please Help !! Thanks in advance ..

import java.sql.*;

public class ConnectionProvider
private static Connection con = null;

public static Connection getConnection() {
if (con != null)
return con;
else {
try {

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "password";
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println(e); }
return con;

Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 867
The variable "con" is used to store the reference to a Connection. Once it was set, the same value will be returned and not a connection will be built every time.

I agree. Here's the link:
subject: JDBC
Similar Threads
Error accessing a static method (JSP, Java)
Here's a fun one...
java jdbc
ThreadLocal and DB connection...
SQL Server 2008r2 Connection Failed