Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Post: A connection is opened if the current connection is not valid
- private static boolean open() throws SQLException {
- // If connection is still valid, return without doing anything
- try {
- if (connection != null && connection.isValid(1)) {
- return true;
- }
- }
- catch (SQLException e1) {
- close();
- }
- // Start connecting
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://mysql.starredout.com/" + database;
- String user = "starredout";
- String password = "starredout";
- try {
- Class.forName(driver).newInstance();
- }
- catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- connection = DriverManager.getConnection(url,user,password);
- return true;
- }
- // Post: If a connection was open, it is closed
- private static void close() {
- try {
- if (connection == null || connection.isClosed())
- return;
- connection.close();
- }
- catch (Exception e) {
- e.printStackTrace();
- System.out.println(e.toString());
- }
- }
Add Comment
Please, Sign In to add comment