Skip to content

Instantly share code, notes, and snippets.

@LuisFcoOrtiz
Last active June 6, 2017 09:01
Show Gist options
  • Save LuisFcoOrtiz/9ca949f8c1f876c7c8215b0130327d42 to your computer and use it in GitHub Desktop.
Save LuisFcoOrtiz/9ca949f8c1f876c7c8215b0130327d42 to your computer and use it in GitHub Desktop.
java class to connect with database (SQLITE) | Clase java para conexión con base de datos (SQLITE)
import java.sql.*;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author manrique
*/
public class ConexionBD {
private Connection connection;
private Statement query;
private ResultSet queryResults;
String sentenciaSQL; //Sentencia a ejecutar
public ConexionBD() {
connectDataBase();
}
/*METODOS PARA BASE DE DATOS*/
public void connectDataBase() {
String url="dataBase.sqlite";
try {
connection = DriverManager.getConnection("jdbc:sqlite:"+url);
query = connection.createStatement(); //Statement para las sentencias SQL
} catch (SQLException ex){
System.out.println("Error al obtener datos "+ ex.getMessage());
System.exit(0);
}
}
public ResultSet sendQuery(String queryToSend) {
sentenciaSQL = queryToSend;
try {
queryResults = query.executeQuery(sentenciaSQL);
return queryResults;
} catch(SQLException ex) {
System.out.println("No se pudo ejecutar la sentencia "+ex.getMessage());
return null;
}
}//Funcion para SELECT
public boolean insertElement (String sentence) {
sentenciaSQL = sentence ;
try {
query.executeUpdate(sentenciaSQL);
return true; //true si todo correcto
} catch(SQLException ex) {
System.out.println("No se pudo insertar el contacto " + ex.getMessage());
return false; //False si hay error
}
}//Funcion para INSERT, UPDATE, DELETE
/*FIN METODOS PARA BASE DE DATOS*/
/*Ejemplo de uso*/
public ResultSet example() {
return sendQuery("example");
}//Example metod
}//Fin de clase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment