Forums Register Login

JDBC

+Pie Number of slices to send: Send
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
if(con!=null)
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";
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println(e); }
return con;
}

}
}
1
+Pie Number of slices to send: Send
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.

Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 811 times.
Similar Threads
Here's a fun one...
ThreadLocal and DB connection...
Error accessing a static method (JSP, Java)
java jdbc
SQL Server 2008r2 Connection Failed
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:52:02.