Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.lilixips.transgenic.model;
- import java.sql.Connection; // para establecer conxion con la base de datos.
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- *
- * @author gzuñiga
- */
- public class Conexion {
- private Connection con;
- private Statement sentencia;
- private ResultSet tablaVirtual;
- //Constructor
- //Datos Necesarios.
- /*
- Servidor->localhost
- user->root
- pass->123456
- bdName->bd_alimentos
- */
- public Conexion(String server, String user, String pass, String bdName) throws ClassNotFoundException, SQLException {
- String prototolo = "jdbc:mysql://";
- String lineaUsuario = "user=" + user;
- String lineaPass = "password=" + pass;
- String url = prototolo + server + "/" + bdName + "?" + lineaUsuario + "&" + lineaPass;
- System.out.println(url);
- //Cragar la clase driver en tiempo de ejecución.
- Class.forName("com.mysql.jdbc.Driver");
- //Rescatar el objeto Conexion desde el Driver
- con = DriverManager.getConnection(url);
- }
- public void ejecutar(String query) throws SQLException {
- //1.- Crear la sentencia a través de la conexión.
- // Abrir la conexión.
- sentencia = con.createStatement();
- //2.- Ejecutar un QUERY
- sentencia.execute(query);
- //3.- Cerrar la conexión->desconectar();
- desconectar();
- System.out.println(query);
- }
- public ResultSet ejecutarSelect(String select) throws SQLException {
- sentencia = con.createStatement();
- tablaVirtual = sentencia.executeQuery(select);
- return tablaVirtual;
- }
- public void desconectar() throws SQLException {
- sentencia.close();
- }
- // public static void main(String[] args) throws ClassNotFoundException, SQLException {
- // Conexion c = new Conexion("localhost", "root", "123456", "bd_alimentos");
- //
- // String insert = "INSERT INTO alimento VALUES(NULL,'Loly','Sabory',true);";
- // c.ejecutar(insert);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement