Guest User

Untitled

a guest
Oct 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. public class Conexion {
  2.  
  3. private static String servidor="jdbc:mysql://localhost:3306/skywater?zeroDateTimeBehavior=convertToNull";
  4. private static String user="root";
  5. private static String pass="pass";
  6. private static String driver="com.mysql.jdbc.Driver";
  7. private static Connection conexion;
  8.  
  9. public Conexion() {
  10. try {
  11. Class.forName(driver);
  12. conexion= DriverManager.getConnection(servidor,user,pass);
  13.  
  14. }catch(ClassNotFoundException | SQLException e) {
  15. JOptionPane.showMessageDialog(null,"Error al establecer conexion con base de datos n Contacte Servicio: "+e.getMessage());
  16. }
  17. }
  18. public Connection getConnection() {
  19. return conexion;
  20. }
  21. public Connection getClosedConnection() throws SQLException {
  22. conexion.close();
  23. System.out.println("Conexion cerrada");
  24. return conexion;
  25. }
  26.  
  27. public class OperacionesDB {
  28. Conexion conector = new Conexion();
  29.  
  30. Connection con=conector.getConnection();
  31. Statement st;
  32.  
  33. public DefaultTableModel consultaGeneral(String tabla,String columna, String valor) {
  34.  
  35.  
  36. System.out.println(tabla);
  37. DefaultTableModel modelo=new DefaultTableModel();
  38.  
  39. String seleccion="SELECT * FROM "+ tabla+" WHERE "+columna+" LIKE '%"+valor+"%';";
  40. try {
  41.  
  42. Statement ejecutor = con.createStatement();
  43. ResultSet rs=ejecutor.executeQuery(seleccion);
  44. String nvo[]= new String[rs.getMetaData().getColumnCount()];
  45. for(int j=1;j<=rs.getMetaData().getColumnCount();j++){
  46.  
  47. modelo.addColumn(rs.getMetaData().getColumnName(j));
  48.  
  49. }
  50.  
  51. while(rs.next())
  52.  
  53. {
  54.  
  55. for(int i=1;i<=modelo.getColumnCount();i++)
  56.  
  57. nvo[i-1]=rs.getString(i);
  58.  
  59. modelo.addRow(nvo);
  60. }
  61.  
  62. ejecutor.close();
  63. //con.close();
  64. rs.close();
  65.  
  66. return modelo;
  67. }
  68.  
  69.  
  70. catch(SQLException e) {
  71. System.out.println("Error en consulta general "+e.getMessage());
  72. return modelo;
  73. }
  74.  
  75. public DefaultTableModel consultaFechas(String tabla,String columna, String valor, String valor2, String columna2, String valor3) {
  76. DefaultTableModel modelo=new DefaultTableModel();
  77.  
  78. String seleccion="SELECT * FROM "+ tabla+" WHERE "+columna2+"='"+valor3+"' AND "+columna+">='"+valor+"' AND "+columna+ "<='"+valor2+"';";
  79. try {
  80. Statement ejecutor = con.createStatement();
  81. ResultSet rs=ejecutor.executeQuery(seleccion);
  82. String nvo[]= new String[rs.getMetaData().getColumnCount()];
  83. for(int j=1;j<=rs.getMetaData().getColumnCount();j++){
  84.  
  85. modelo.addColumn(rs.getMetaData().getColumnName(j));
  86.  
  87. }
  88.  
  89. while(rs.next())
  90.  
  91. {
  92.  
  93. for(int i=1;i<=modelo.getColumnCount();i++)
  94.  
  95. nvo[i-1]=rs.getString(i);
  96.  
  97. modelo.addRow(nvo);
  98. }
  99.  
  100.  
  101. ejecutor.close();
  102. con.close() //<- Cerrar la conexión
  103. return modelo;
  104. }
  105.  
  106.  
  107. catch(SQLException e) {
  108. return modelo;
  109. }
  110. }
Add Comment
Please, Sign In to add comment