Created
March 17, 2016 21:16
-
-
Save elcioabrahao/f05dad1fb65e448ee69a to your computer and use it in GitHub Desktop.
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 com.mysql.jdbc.Connection; | |
import java.sql.*; | |
import java.sql.DriverManager; | |
/** | |
* @desc A singleton database access class for MySQL | |
* @author Ramindu | |
*/ | |
public final class MysqlConnect { | |
public Connection conn; | |
private Statement statement; | |
public static MysqlConnect db; | |
private MysqlConnect() { | |
String url= "jdbc:mysql://localhost:3306/"; | |
String dbName = "database_name"; | |
String driver = "com.mysql.jdbc.Driver"; | |
String userName = "username"; | |
String password = "password"; | |
try { | |
Class.forName(driver).newInstance(); | |
this.conn = (Connection)DriverManager.getConnection(url+dbName,userName,password); | |
} | |
catch (Exception sqle) { | |
sqle.printStackTrace(); | |
} | |
} | |
/** | |
* | |
* @return MysqlConnect Database connection object | |
*/ | |
public static synchronized MysqlConnect getDbCon() { | |
if ( db == null ) { | |
db = new MysqlConnect(); | |
} | |
return db; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment