Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final int CONNECTION_TIMEOUT = 10000;
- private Connection connection;
- private Properties connectionProps;
- private DBSingleton() {}
- public static DBSingleton get()
- {
- return DB;
- }
- private boolean initialize()
- {
- connectionProps = new Properties();
- connectionProps.put("user", USERNAME);
- connectionProps.put("password", PASSWORD);
- try
- {
- refreshConnection();
- return connection.isValid(CONNECTION_TIMEOUT);
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return false;
- }
- private void refreshConnection()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://"+IP+":"+PORT+"/"+DB_NAME,connectionProps);
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- catch(ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- public Statement stmnt()
- {
- try
- {
- if(connection == null && !initialize()) //connection has not been established yet
- throw new RuntimeException();
- if(!connection.isValid(CONNECTION_TIMEOUT))
- refreshConnection();
- return connection.createStatement();
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement