Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ConexionJDBC {
- String db = "popbl4tienda";
- String usuario = "root";
- String contrasena = "";
- String url = "jdbc:mysql://localhost/"+db+"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- Connection conexion;
- Statement statement = null;
- ResultSet result;
- public ConexionJDBC() {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- conexion = DriverManager.getConnection(url, usuario, contrasena);
- if (conexion != null) {
- statement = conexion.createStatement();
- System.out.println(" Conexion a base de datos " + db + " correcta.");
- } else
- System.out.println("Conexion fallida.");
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public ResultSet select(String query){
- try{
- statement = conexion.createStatement();
- result = statement.executeQuery(query);
- return result;
- }catch(SQLException e){
- System.out.println("Error al ejecutar el comando select!");
- System.out.println(e.toString());
- return null;
- }
- }
- public int update(String query){
- try{
- statement = conexion.createStatement();
- return statement.executeUpdate(query);
- }catch(SQLException e){
- System.out.println("Error al ejecutar el comando query");
- System.out.println(e.toString());
- return -1;
- }
- }
- public int delete(String query){
- try{
- statement = conexion.createStatement();
- return statement.executeUpdate(query);
- }catch(SQLException e){
- System.out.println("Error al ejecutar el comando delete");
- System.out.println(e.toString());
- return -1;
- }
- }
- public void close(){
- try{
- statement.close();
- result.close();
- }catch(SQLException e){
- System.out.println("Error al cerrar!");
- System.out.println(e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement