Foro de elhacker.net

Programación => Java => Mensaje iniciado por: SebastianJava en 15 Noviembre 2013, 03:44 am



Título: AYUDA SOBRE CODIGO
Publicado por: SebastianJava en 15 Noviembre 2013, 03:44 am
 :-(

Estimados primero que todo muchas gracias, ahora bien, tengo un problema cuando trato de cargar un JTABLE CON LOS DATOS de una Base de datos  en MYSQL, se supone que con el boton consultar deberia mostrar los resultados, pero mi tabla aparece en blanco.


les pido su ayuda...

ESTA ES LA CLASE ing_clie


Código
  1. package formularios;
  2.  
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.table.DefaultTableModel;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JTextField;
  10. import javax.swing.JButton;
  11.  
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import java.sql.*;
  15. import javax.swing.JTable;
  16.  
  17. public class ing_cli extends JFrame {
  18.  
  19. private JPanel contentPane;
  20. private JTextField t_nom;
  21. private JTextField t_ape;
  22. private JTextField t_ciu;
  23. private JTextField t_tel;
  24.  
  25. private JTable t_datos;
  26.  
  27.  
  28. // DECLARAMOS Y DAMOS ACCIONES
  29.  
  30. public ing_cli() {
  31.  
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. setBounds(100, 100, 563, 533);
  34. contentPane = new JPanel();
  35. setContentPane(contentPane);
  36. contentPane.setLayout(null);
  37.  
  38. JLabel lblNombre = new JLabel("Nombre");
  39. lblNombre.setBounds(42, 46, 66, 14);
  40. contentPane.add(lblNombre);
  41.  
  42. JLabel lblIngresoClientes = new JLabel("Ingreso Clientes");
  43. lblIngresoClientes.setBounds(227, 11, 116, 14);
  44. contentPane.add(lblIngresoClientes);
  45.  
  46. JLabel lblApellido = new JLabel("Apellido");
  47. lblApellido.setBounds(42, 89, 66, 14);
  48. contentPane.add(lblApellido);
  49.  
  50. JLabel lblCiudad = new JLabel("Ciudad");
  51. lblCiudad.setBounds(42, 129, 66, 14);
  52. contentPane.add(lblCiudad);
  53.  
  54. JLabel lblTelefono = new JLabel("Telefono");
  55. lblTelefono.setBounds(42, 169, 66, 14);
  56. contentPane.add(lblTelefono);
  57.  
  58. t_nom = new JTextField();
  59. t_nom.setBounds(157, 43, 294, 20);
  60. contentPane.add(t_nom);
  61. t_nom.setColumns(10);
  62.  
  63. t_ape = new JTextField();
  64. t_ape.setBounds(157, 86, 294, 20);
  65. contentPane.add(t_ape);
  66. t_ape.setColumns(10);
  67.  
  68. t_ciu = new JTextField();
  69. t_ciu.setBounds(157, 126, 294, 20);
  70. contentPane.add(t_ciu);
  71. t_ciu.setColumns(10);
  72.  
  73. t_tel = new JTextField();
  74. t_tel.setBounds(157, 166, 294, 20);
  75. contentPane.add(t_tel);
  76. t_tel.setColumns(10);
  77.  
  78. // BOTON NUEVO
  79. JButton btnNuevo = new JButton("Nuevo");
  80. btnNuevo.addActionListener(new ActionListener() {
  81. public void actionPerformed(ActionEvent arg0) {
  82. limpiar();
  83. }
  84. });
  85. btnNuevo.setBounds(29, 211, 89, 23);
  86. contentPane.add(btnNuevo);
  87. // TERMINO BOTON NUEVO
  88.  
  89. // BOTON GRABAR
  90. JButton btnGrabar = new JButton("Grabar");
  91. btnGrabar.addActionListener(new ActionListener() {
  92. public void actionPerformed(ActionEvent e) {
  93. try{ conectar cc = new conectar ();
  94. Connection cn = cc.conexion();
  95. String nom, ape, ciu, tel;
  96. String sql="";
  97. nom = t_nom.getText();
  98. ape = t_ape.getText();
  99. ciu = t_ciu.getText();
  100. tel = t_tel.getText();
  101. sql="INSERT INTO clientes (nom_cli, ape_cli, ciu_cli, tel_cli) VALUES (?,?,?,?)";
  102.  
  103. PreparedStatement pst = cn.prepareStatement (sql);
  104. pst.setString(1,nom);
  105. pst.setString(2,ape);
  106. pst.setString(3,ciu);
  107. pst.setString(4,tel);
  108. int n = pst.executeUpdate();
  109. if (n>0){
  110. JOptionPane.showMessageDialog(null, "Registrado Grabado con exito");
  111. limpiar();
  112. }
  113.  
  114. } catch (SQLException e1) {
  115. JOptionPane.showMessageDialog(null, "ERROR");
  116. }
  117. }
  118. });
  119. btnGrabar.setBounds(128, 211, 89, 23);
  120. contentPane.add(btnGrabar);
  121. /// TERMINO BOTON GRABAR
  122.  
  123. // INICIO BOTON CONSULTAR
  124. JButton btnConsultar = new JButton("Consultar");
  125. btnConsultar.addActionListener(new ActionListener() {
  126. public void actionPerformed(ActionEvent e) {
  127. cargar();
  128. }
  129. });
  130. btnConsultar.setBounds(227, 211, 89, 23);
  131. contentPane.add(btnConsultar);
  132. // TERMINO DE BOTON CONSULTAR
  133.  
  134. // INICIO BOTON MODIFICAR
  135.  
  136. JButton btnModificar = new JButton("Modificar");
  137. btnModificar.setBounds(326, 211, 89, 23);
  138. contentPane.add(btnModificar);
  139. // TERMINO BOTON MODIFICAR
  140.  
  141. // INICIO BOTON SALIR
  142. JButton btnSalir = new JButton("Salir");
  143. btnSalir.setBounds(425, 211, 89, 23);
  144. contentPane.add(btnSalir);
  145. // TERMINO BOTON SALIR
  146.  
  147. // CREACION DE TABLA
  148. t_datos = new JTable();
  149. t_datos.setBounds(42, 247, 459, 224);
  150. contentPane.add(t_datos);
  151. // TERMINO TABLA
  152.  
  153. }
  154.  
  155. // INICIO METODO LIMPIAR
  156. void limpiar(){
  157. t_nom.setText("");
  158. t_ape.setText("");
  159. t_ciu.setText("");
  160. t_tel.setText("");
  161. }
  162.  
  163. // FIN METODO LIMPIAR
  164.  
  165. // INICIO METODO CARGAR DATOS BD A TABLA
  166. void cargar(){
  167. try{
  168. String [] titulos={"Codigo", "Nombre", "Apellido", "Ciudad", "Telefono"};
  169. String [] registros=new String[5];
  170.  
  171. String sql= "SELECT * FROM clientes";
  172.  
  173. model = new DefaultTableModel(null, titulos);
  174.  
  175. conectar cc=new conectar();
  176. Connection cn =cc.conexion();
  177. Statement st = cn.createStatement ();
  178. ResultSet rs = st.executeQuery(sql);
  179.  
  180. while(rs.next()){
  181. registros[0]=rs.getString("cod_cli");
  182. registros[1]=rs.getString("nom_cli");
  183. registros[2]=rs.getString("ape_cli");
  184. registros[3]=rs.getString("ciu_cli");
  185. registros[4]=rs.getString("tel_cli");
  186. model.addRow(registros);
  187.  
  188.  
  189. }
  190. } catch (SQLException e1) {
  191. JOptionPane.showMessageDialog(null, "ERROR");
  192. }
  193. }
  194.  
  195. // FIN METODO CARGAR
  196.  
  197. // DEJAMOS VISIBLE EL FRAME
  198. public static void main(String[] args) {
  199. ing_cli frame = new ing_cli();
  200. frame.setVisible(true);
  201. }
  202.  
  203. }[ / code]



ESTA ES LA CLASE conectar:

Código
  1. package formularios;
  2.  
  3. import java.sql.*;
  4. import javax.swing.*;
  5.  
  6. public class conectar {
  7. Connection conect = null;
  8.   public Connection conexion()
  9.    {
  10.      try {
  11.  
  12.           //Cargamos el Driver MySQL
  13.           Class.forName("org.gjt.mm.mysql.Driver");
  14.           conect = DriverManager.getConnection("jdbc:mysql://localhost:3306/bd","root","");
  15.           JOptionPane.showMessageDialog(null, "estás conectado");
  16.  
  17.        } catch (Exception e) {
  18.            JOptionPane.showMessageDialog(null,"Error "+e);
  19.        }
  20.        return conect;
  21.  
  22. }}[code==java]
  23.  
  24. NECESITAN EL SQL??
  25. muchas gracias y disculpen las molestias.
  26.  
  27.  
[/code]


Título: Re: AYUDA SOBRE CODIGO
Publicado por: Mitsu en 15 Noviembre 2013, 04:24 am
Imprime el arreglo registros para ver si la consulta ha devuelto los valores esperados. Acostúmbrate a hacer esto, a hacer pequeñas pruebas hasta dar con el origen del problema.


Título: Re: AYUDA SOBRE CODIGO
Publicado por: SebastianJava en 16 Noviembre 2013, 16:07 pm
GRACIAS  :-\ :-\