Advertisement
prezman

Untitled

Apr 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.DriverManager;        
  6. import java.sql.SQLException;
  7. import java.sql.Statement;      
  8.  
  9. public class Conexion {
  10.     private Statement sen;
  11.     private ResultSet rs;
  12.     private Connection con;
  13.    
  14.     public Conexion(String server, String bd, String user, String pass) throws ClassNotFoundException, SQLException{
  15.         String protocolo = "jdbc:mysql://";
  16.         String lineaUser = "user="+user;
  17.         String lineaPass = "password="+pass;
  18.        
  19.         String url = protocolo +
  20.                 server + "/" +
  21.                 bd + "?" +
  22.                 lineaUser + "&" +
  23.                 lineaPass;
  24.        
  25.         System.out.println(url);
  26.        
  27.         Class.forName("com.mysql.jdbc.Driver"); // JAR?
  28.         con = DriverManager.getConnection(url);
  29.     }
  30.    
  31.     public void ejecutar(String query) throws SQLException{
  32.         System.out.println(query);
  33.        
  34.         sen = con.createStatement();
  35.         sen.executeUpdate(query);
  36.         close();
  37.     }
  38.    
  39.     public ResultSet ejecutarSelect(String query) throws SQLException{
  40.         System.out.println(query);
  41.        
  42.         sen = con.createStatement();
  43.         rs = sen.executeQuery(query);
  44.         return rs;
  45.     }
  46.    
  47.     public void close() throws SQLException{
  48.         sen.close();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement