Advertisement
Guest User

DB Connect

a guest
Nov 9th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. //import com.mysql.*;
  9.  
  10. public class DataBase {
  11.     protected Connection connection;
  12.     protected Statement statment;
  13.     protected ResultSet rs;
  14.     public static DataBase d = DataBase.GetInsatance();
  15.  
  16.     private DataBase() {
  17.         try {
  18.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  19.             try {
  20.                 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lab", "root", "mysql_root_snj");
  21.                 statment = connection.createStatement();
  22.             } catch (SQLException e) {
  23.                 System.out.println("Not connected!");
  24.                 e.printStackTrace();
  25.             }
  26.         } catch (ClassNotFoundException e) {
  27.             // TODO Auto-generated catch block
  28.             System.out.println("Class not Found! shitt!!");
  29.             e.printStackTrace();
  30.         } catch (InstantiationException e1) {
  31.             // TODO Auto-generated catch block
  32.             e1.printStackTrace();
  33.         } catch (IllegalAccessException e1) {
  34.             // TODO Auto-generated catch block
  35.             e1.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     public static DataBase GetInsatance() {
  40.         if (d == null)
  41.             return d = new DataBase();
  42.         try {
  43.             if (d.connection.isClosed())
  44.                 d = new DataBase();
  45.         } catch (SQLException e) {
  46.             // TODO Auto-generated catch block
  47.             e.printStackTrace();
  48.             d = new DataBase();
  49.         }
  50.         return d;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement