Advertisement
Guest User

DB

a guest
Nov 26th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package megaventa;
  2. import java.sql.*;
  3. import java.sql.DriverManager;
  4. /**
  5.  *
  6.  * @author Angel Nuñez
  7.  */
  8. public class conexion {
  9.    
  10.     private static String username = "root";
  11.     private static String password = "";
  12.     private static String hostname = "localhost";
  13.     private static String puerto = "3306";
  14.     private static String database = "megaventa";
  15.     private static String classname = "com.mysql.jdbc.Driver";
  16.     private static String url = "jdbc:mysql://"+hostname+":"+puerto+"/"+database;
  17.     private static Connection conn;
  18.    
  19.     public static Connection obtener() throws ClassNotFoundException, SQLException
  20.     {
  21.         if(conn==null)
  22.         {
  23.             Class.forName(classname);
  24.             conn = DriverManager.getConnection(url, username, password);
  25.         }
  26.         return conn;
  27.     }
  28.    
  29.    
  30.     public conexion()
  31.     {
  32.         try {
  33.             Class.forName(classname);
  34.             conn = DriverManager.getConnection(url, username, password);
  35.         } catch (Exception e) {
  36.             System.err.println(e.getMessage());
  37.         }
  38.     }
  39.    
  40.     public Connection getConnection()
  41.     {
  42.         return this.conn;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement