Guest User

Untitled

a guest
Oct 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package DAT;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5.  
  6. public class DATConexion {
  7. Connection con = null;
  8. public Connection getConnection() throws ClassNotFoundException, SQLException{
  9. String driver = "com.mysql.jdbc.Driver";
  10. String url = "jdbc:mysql://localhost:3306/prueba";
  11. Class.forName(driver);
  12. return DriverManager.getConnection(url, "root", "password");
  13. }
  14. public Connection AbrirConexion() throws ClassNotFoundException, SQLException{
  15. con = getConnection();
  16. return con;
  17. }
  18. public void CerrarConexion() throws ClassNotFoundException, SQLException{
  19. con = null;
  20.  
  21. }
  22. }
  23.  
  24. package DAT;
  25.  
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.sql.Statement;
  29.  
  30.  
  31. public class DATPrueba {
  32.  
  33. DAT.DATConexion c = new DATConexion();
  34.  
  35. public ResultSet pruebaCarga() throws ClassNotFoundException, SQLException{
  36. Statement st = c.AbrirConexion().createStatement();
  37. String sentencia = "SELECT * FROM usuario";
  38. ResultSet re = st.executeQuery(sentencia);
  39. return re;
  40. }
  41. }
  42.  
  43. package BL;
  44.  
  45. import DAT.DATPrueba;
  46. import java.sql.ResultSet;
  47. import java.sql.ResultSetMetaData;
  48. import java.sql.SQLException;
  49. import java.util.ArrayList;
  50.  
  51.  
  52. public class BLPrueba {
  53. DATPrueba manejadorPrueba = new DATPrueba();
  54.  
  55. public ArrayList<Object[]> verPrueba() throws SQLException, ClassNotFoundException {
  56. ArrayList<Object[]> datos = new ArrayList<>();
  57. ResultSet rs = manejadorPrueba.pruebaCarga();
  58. ResultSetMetaData rm = rs.getMetaData();
  59. int col = rm.getColumnCount();
  60. while (rs.next()) {
  61. Object[] filas = new Object[col];
  62. for (int i = 0; i < filas.length; i++) {
  63. filas[i] = rs.getObject(i + 1);
  64. }
  65. datos.add(filas);
  66. }
  67. return datos;
  68. }
  69. }
  70.  
  71. public static void updateTabla() {
  72. try {
  73. BLPrueba dl = new BLPrueba();
  74. DefaultTableModel dtm = new DefaultTableModel();
  75. dtm.addColumn("Id");
  76. dtm.addColumn("Nombre");
  77. dtm.addColumn("Cedula");
  78. int aux = dl.verPrueba().size();
  79. System.out.println(aux);
  80. //for (int i = 0; i < dl.verPrueba().size(); i++) {
  81. for (int i =0 ; i < aux; i++) {
  82. dtm.addRow(dl.verPrueba().get(i));
  83. }
  84.  
  85. tblPrueba.setModel(dtm);
  86.  
  87. } catch (ClassNotFoundException | SQLException ex) {
  88. Logger.getLogger(pruebagui.class.getName()).log(Level.SEVERE, null, ex);
  89. }
  90.  
  91. }
Add Comment
Please, Sign In to add comment