Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. package daoaluno;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class ConnectionFactory {
  8.    
  9.     String driverClassName = "com.mysql.jdbc.Driver";
  10.     String connectionUrl = "jdbc:mysql://localhost:3306/exemplo_jdbc";
  11.     String dbUser = "root";
  12.     String dbPwd = "vertrigo";
  13.  
  14.     private static ConnectionFactory connectionFactory = null;
  15.  
  16.     private ConnectionFactory() {
  17.         try {
  18.             Class.forName(driverClassName);
  19.         } catch (ClassNotFoundException e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.  
  24.     public Connection getConnection() throws SQLException {
  25.         Connection conn = null;
  26.         conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
  27.         return conn;
  28.     }
  29.  
  30.     public static ConnectionFactory getInstance() {
  31.         if (connectionFactory == null) {
  32.             connectionFactory = new ConnectionFactory();
  33.         }
  34.         return connectionFactory;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement