Skip to content

Instantly share code, notes, and snippets.

@leostera
Created November 3, 2012 19:07
Show Gist options
  • Save leostera/4008305 to your computer and use it in GitHub Desktop.
Save leostera/4008305 to your computer and use it in GitHub Desktop.
Access DB
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Formularios;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author User
*/
public class personal extends javax.swing.JFrame {
DefaultTableModel model;
/* ... */
private void botonMostrarActionPerformed(java.awt.event.ActionEvent evt) {
try {
Boolean shouldClose = false;
if ( resultados == null ) {
String db="jdbc:odbc:Driver=Microsoft Access Driver (*.mdb);DBQ=c:/base.mdb";
String sql="SELECT * FROM personal";
Connection cn = DriverManager.getConnection(db,"","");
Statement st = cn.createStatement();
resultados = st.executeQuery(sql);
shouldClose = true;
}
while(resultados.next()){
nombreMostrar.setText(resultados.getString(2));
apellidoMostrar.setText(resultados.getString(3));
telefonoMostrar.setText(resultados.getString(4));
}
resultados.close(); /* tengo mis dudas respecto de cerrar el resultset, creeria que tenes que dejarlo abierto */
if (shouldClose) {
st.close();
cn.close();
}
} catch (SQLException ex) {
Logger.getLogger(personal.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* .. */
// End of variables declaration
ResultSet resultados=null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment