Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. package mariadb;
  2. import java.sql.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class MariaDB {
  8.  
  9. private Connection conexion;
  10.  
  11. public Connection getConexion() {
  12. return conexion;
  13. }
  14.  
  15. public void setConexion(Connection conexion) {
  16. this.conexion = conexion;
  17. }
  18.  
  19. public MariaDB conectar() {
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver");
  22. String BaseDeDatos = "jdbc:mysql://localhost:3311/unidad1?user=root&password=123456siete";
  23.  
  24. setConexion(DriverManager.getConnection(BaseDeDatos));
  25. if (conexion != null) {
  26. System.out.println("Conexion exitosa!");
  27. } else {
  28. System.out.println("Conexion fallida!");
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. return this;
  35. }
  36.  
  37. public ResultSet consultar(String sql) {
  38. ResultSet resultado;
  39. try {
  40. Statement sentencia = getConexion().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  41. resultado = sentencia.executeQuery(sql);
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. return null;
  45. }
  46. return resultado;
  47. }
  48.  
  49. public boolean ejecutar(String sql) {
  50. try {
  51. Statement sentencia = getConexion().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  52. sentencia.execute(sql);
  53. sentencia.close();
  54. } catch (SQLException e) {
  55. e.printStackTrace();
  56. return false;
  57. }
  58. return true;
  59. }
  60.  
  61. public static void main(String[] args) {
  62. MariaDB baseDatos = new MariaDB().conectar();
  63.  
  64. ------""""""""""""""EN ESTA PARTE ES DONDE SE REALIZAN LAS CONSULTAS""""""""""""""------
  65.  
  66. if (baseDatos.ejecutar("INSERT INTO UNIDAD1.productos(Pro_ID,Pro_descripcion,Pro_nombre,Pro_precio) VALUES(666, 'Fruta Seca', 'Mango', 333)")) {
  67. System.out.println("Ejecución correcta!");
  68. } else {
  69. System.out.println("Ocurrió un problema al ejecutar!");
  70. }
  71. ResultSet resultados = baseDatos.consultar("SELECT * FROM productos");
  72. if (resultados != null) {
  73. try {
  74. System.out.println("Producto ID Desc. Producto Nombre Producto Precio Producto");
  75. System.out.println("---------------------------------------------------------------------------------");
  76. while (resultados.next()) {
  77. System.out.println(" "+resultados.getBigDecimal("Pro_ID")+" "+resultados.getString("Pro_descripcion")+" "
  78. +resultados.getString("Pro_nombre")+" "+resultados.getBigDecimal("Pro_precio"));
  79. }
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement