Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DAT;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DATConexion {
- Connection con = null;
- public Connection getConnection() throws ClassNotFoundException, SQLException{
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/prueba";
- Class.forName(driver);
- return DriverManager.getConnection(url, "root", "password");
- }
- public Connection AbrirConexion() throws ClassNotFoundException, SQLException{
- con = getConnection();
- return con;
- }
- public void CerrarConexion() throws ClassNotFoundException, SQLException{
- con = null;
- }
- }
- package DAT;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DATPrueba {
- DAT.DATConexion c = new DATConexion();
- public ResultSet pruebaCarga() throws ClassNotFoundException, SQLException{
- Statement st = c.AbrirConexion().createStatement();
- String sentencia = "SELECT * FROM usuario";
- ResultSet re = st.executeQuery(sentencia);
- return re;
- }
- }
- package BL;
- import DAT.DATPrueba;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.util.ArrayList;
- public class BLPrueba {
- DATPrueba manejadorPrueba = new DATPrueba();
- public ArrayList<Object[]> verPrueba() throws SQLException, ClassNotFoundException {
- ArrayList<Object[]> datos = new ArrayList<>();
- ResultSet rs = manejadorPrueba.pruebaCarga();
- ResultSetMetaData rm = rs.getMetaData();
- int col = rm.getColumnCount();
- while (rs.next()) {
- Object[] filas = new Object[col];
- for (int i = 0; i < filas.length; i++) {
- filas[i] = rs.getObject(i + 1);
- }
- datos.add(filas);
- }
- return datos;
- }
- }
- public static void updateTabla() {
- try {
- BLPrueba dl = new BLPrueba();
- DefaultTableModel dtm = new DefaultTableModel();
- dtm.addColumn("Id");
- dtm.addColumn("Nombre");
- dtm.addColumn("Cedula");
- int aux = dl.verPrueba().size();
- System.out.println(aux);
- //for (int i = 0; i < dl.verPrueba().size(); i++) {
- for (int i =0 ; i < aux; i++) {
- dtm.addRow(dl.verPrueba().get(i));
- }
- tblPrueba.setModel(dtm);
- } catch (ClassNotFoundException | SQLException ex) {
- Logger.getLogger(pruebagui.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
Add Comment
Please, Sign In to add comment