Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package com.DylanPerez.www;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class CandyCartel {
  8.  
  9.     private Connection connection;
  10.    
  11.     private String host, database, username, password;
  12.     private int port;
  13.    
  14.     public static void main(String[] args) {
  15.         CandyCartel cc = new CandyCartel();
  16.         // host - localhost
  17.         cc.host = "localhost";
  18.         // port - 3306
  19.         cc.port = 3306;
  20.         // database name - candycarteldb
  21.         cc.database = "candycarteldb";
  22.         // username - root
  23.         cc.username = "root";
  24.         // password -
  25.         cc.password = "";
  26.        
  27.         try {
  28.             cc.openConnection();
  29.             if(!cc.connection.isClosed()) {
  30.                 System.out.println("[Candy Cartel] Database has been connected!");
  31.             }
  32.         } catch(SQLException e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.    
  37.     private void openConnection() throws SQLException {
  38.         if(connection != null && !connection.isClosed()) {
  39.             return;
  40.         }
  41.        
  42.         connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
  43.     }
  44.    
  45.    
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement