Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Conexao {
  4.  
  5.     Connection conexao = null;
  6.     Statement stmt = null;
  7.     String host, usr, pwd, db, con;
  8.  
  9.     public Conexao(String h, String u, String d) {
  10.         host = h;
  11.         usr = u;
  12.         db = d;
  13.         pwd = null;
  14.         con = "jdbc:mysql://" + host + "/" + db;
  15.     }
  16.  
  17.     public void conectar(String p) {
  18.         try {
  19.             String driver = "com.mysql.jdbc.Driver";
  20.             Class.forName(driver);
  21.             pwd = p;
  22.             conexao = DriverManager.getConnection(con, usr, pwd);
  23.             stmt = conexao.createStatement();
  24.             System.out.println("Conexao estabilizada: " + conexao);
  25.         } catch (ClassNotFoundException e) {
  26.             System.out.println("Class not found");
  27.         } catch (SQLException e) {
  28.             System.out.println("SQL Exception");
  29.         }
  30.     }
  31.    
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement