Created
October 20, 2014 17:08
-
-
Save yupadhyay/ea18e4b2fd3ac11528a7 to your computer and use it in GitHub Desktop.
MySql Connection Helper in CQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
public class MySQLConnectionHelper { | |
private static volatile MySQLConnectionHelper mySQLConnectionHelper; | |
private MySQLConnectionHelper() { | |
try { | |
Class.forName("com.mysql.jdbc.Driver").newInstance(); | |
//For Teradata Class.forName("com.teradata.jdbc.TeraDriver").newInstance(); | |
} catch (InstantiationException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (ClassNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static synchronized Connection getConnection(final String url, | |
final String uid, final String pwd) throws SQLException { | |
if (mySQLConnectionHelper == null) { | |
mySQLConnectionHelper = new MySQLConnectionHelper(); | |
} | |
try { | |
return DriverManager.getConnection(url, uid, pwd); | |
} catch (SQLException e) { | |
throw e; | |
} | |
} | |
public static void close(Connection connection) { | |
try { | |
if (connection != null) { | |
connection.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment