Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package cl.pokemonProject.bd;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Conexion {
  6.  
  7.     private Connection con;// Establecer la conexi�n
  8.     private Statement sen; // Ejecutar consultas
  9.     private ResultSet rs;  // Recorrer los resultados (Tabla)
  10.  
  11.     public Conexion(String server, String bd, String user, String pass) throws SQLException, ClassNotFoundException {
  12.         String protocolo = "jdbc:mysql://";
  13.         String lineaUser = "user=" + user;
  14.         String lineaPass = "password=" + pass;
  15.  
  16.         String url = protocolo
  17.                 + server + "/"
  18.                 + bd + "?"
  19.                 + lineaUser + "&"
  20.                 + lineaPass;
  21.  
  22.         System.out.println(url);
  23.        
  24.         Class.forName("com.mysql.jdbc.Driver");
  25.  
  26.         con = DriverManager.getConnection(url);
  27.        
  28.        
  29.        
  30.         /*
  31.         MysqlDataSource mysql = new MysqlDataSource();
  32.        
  33.         mysql.setServerName(server);
  34.         mysql.setDatabaseName(bd);
  35.         mysql.setUser(user);
  36.         mysql.setPassword(pass);
  37.         con = mysql.getConnection();
  38.         */
  39.     }
  40.  
  41.     /*
  42.     Consultas actualizan los datos --> delete,insert,update
  43.     ver datos--> select;
  44.      */
  45.  /*Metodo sirve para insert,update,delete*/
  46.     public void ejecutar(String query) throws SQLException {
  47.         sen = con.createStatement();
  48.         sen.executeUpdate(query);
  49.         sen.close();
  50.     }
  51.  
  52.     public ResultSet ejecutarSelect(String query) throws SQLException {
  53.         sen = con.createStatement();
  54.         rs = sen.executeQuery(query);
  55.         return rs;
  56.  
  57.     }
  58.  
  59.     public void desconectar() throws SQLException {
  60.         sen.close();
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement