Skip to content

Instantly share code, notes, and snippets.

@yupadhyay
Created October 20, 2014 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yupadhyay/ea18e4b2fd3ac11528a7 to your computer and use it in GitHub Desktop.
Save yupadhyay/ea18e4b2fd3ac11528a7 to your computer and use it in GitHub Desktop.
MySql Connection Helper in CQ
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