Advertisement
Nasser_Ahmed

Java DB Connector

Apr 12th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package application;
  2. import java.sql.*;
  3. public class JDBCMySQLConnection {
  4.     private static JDBCMySQLConnection instance = new JDBCMySQLConnection();
  5.     //URL to the databse
  6.     /*where jdbc is essential
  7.      * mysql is the dbms
  8.      * localhost: the host of the server
  9.      * jdbcdb: the name of the databse
  10.      */
  11.     public static final String URL="jdbc:mysql://localhost/jdbcdb";
  12.     //User name to the database
  13.     public static final String USER_NAME="";
  14.     //Password
  15.     public static final String PASSWORD="";
  16.     //Driver : it comes with the jar file
  17.     public static final String DRIVER_CLASS="com.mysql.jdbc.Driver";
  18.     private JDBCMySQLConnection(){
  19.         try{
  20.             Class.forName(DRIVER_CLASS);
  21.            
  22.         }catch(ClassNotFoundException e){
  23.             e.printStackTrace();
  24.         }
  25.     }
  26.     private Connection createConnection(){
  27.         Connection connection = null;
  28.         try{
  29.             connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
  30.     }catch(SQLException e){
  31.         System.out.println("Error Unable to connect to DB");
  32.     }
  33.     return connection;
  34.     }
  35.     public static Connection getConnection(){
  36.         return instance.createConnection();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement