Advertisement
Guest User

Untitled

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