Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: user8fb30
Added: May 10, 2018 9:46 AM
Views: 23
Tags: dbconnection
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package db;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15.  *
  16.  * @author Albin
  17.  */
  18. public class DBConnection {
  19.  
  20.     private static final String DRIVER = "com.mysql.jdbc.Driver";
  21.     private static final String URL = "jdbc:mysql://localhost:3306/test";
  22.     private static final String USER = "root";
  23.     private static final String PASSWORD = "root";
  24.  
  25.     private static Connection con;
  26.  
  27.     public static Connection getCon() {
  28.  
  29.         try {
  30.             Class.forName(DRIVER);// LOAD DRIVER AND REGISTER
  31.             con = DriverManager.getConnection(URL, USER, PASSWORD);//ESTABLISH CONNECTION
  32.  
  33.         } catch (ClassNotFoundException ex) {
  34.             Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  35.         } catch (SQLException ex) {
  36.             Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  37.         }
  38.             return con;
  39.     }
  40. }
  41.