
13/05/2010, 14:50
|
| | Fecha de Ingreso: mayo-2010
Mensajes: 20
Antigüedad: 14 años, 9 meses Puntos: 0 | |
Ayuda Jtable Hola compañeros tengo un problema con un Jtable tengo este codigo que es para insertar un ResultSet de MySQL a un Jtable y mostrarlo en pantalla y mandar la tabla resultante a imprimir : Esta Clase hace la conexión:
Código:
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class Conexion
{
public static Connection conectar()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://172.29.182.95:3306/recomendaciones","root","12345");//ruta de la BD
return con;
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
return null;
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
return null;
}
}
public static void desconectar(Connection con)
{
try
{
if(con!=null)
{
con.close();
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
Y este es el código principal
Código:
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.awt.print.*;
5 import java.text.*;
6 import java.util.*;
7 import java.io.*;
8 import javax.swing.table.*;
9 import javax.swing.JTable;
10 import javax.swing.table.DefaultTableModel;
11 import java.sql.Connection;
12 import java.sql.SQLException;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.Statement;
16
17
18 public class TablePrint extends JPanel {
19
20 Connection conexion;
21 PreparedStatement stmt;
22 ResultSet rs;
23 Statement s;
24 DefaultTableModel modelo;
25 JTable table;
26 JButton button;
27 JScrollPane scrollPane;
28
29 public TablePrint()
30 {
31 carga();
32 muestra();
33 configura();
34 }
35 public void carga()
36 {
37 try
38 {
39 DefaultTableModel modelo = new DefaultTableModel();
40 JTable table = new JTable(modelo);
41 modelo.addColumn("no_inf");
42 modelo.addColumn("part_a");
43 modelo.addColumn("part_b");
44 modelo.addColumn("part_c");
45 modelo.addColumn("part_d");
46 modelo.addColumn("part_e");
47 modelo.addColumn("fecha_com");
48 modelo.addColumn("lugar");
49 modelo.addColumn("obj_com");
50 modelo.addColumn("fecha_inf");
51 modelo.addColumn("resp_inf");
52 modelo.addColumn("recom_a");
53 modelo.addColumn("recom_b");
54 modelo.addColumn("recom_c");
55 modelo.addColumn("recom_d");
56 modelo.addColumn("recom_e");
57 modelo.addColumn("niv_risk");
58 modelo.addColumn("dir_loc");
59 modelo.addColumn("fecha_seg");
60 while (rs.next())
61 {
62 Object [] fila = new Object[19];
63 for (int i=0;i<19;i++)
64 fila[i] = rs.getObject(i+1);
65 modelo.addRow(fila);
66 }
67 table.setModel(modelo);
68 }
69 catch (SQLException e)
70 {
71
72 }
73 }
74 public void getDatos(Connection con)
75 {
76 try
77 {
78 conexion=con;
79 stmt=conexion.prepareStatement("(SELECT * FROM informe WHERE niv_risk='MEDIO'&&TO_DAYS(NOW())-TO_DAYS(fecha_inf)>=15&&fecha_seg='0000-00-00')UNION(SELECT * FROM informe WHERE niv_risk='ALTO'&&TO_DAYS(NOW())-TO_DAYS(fecha_inf)>=5&&fecha_seg='0000-00-00')UNION(SELECT * FROM informe WHERE niv_risk='BAJO'&&TO_DAYS(NOW())-TO_DAYS(fecha_inf)>=30&&fecha_seg='0000-00-00') ORDER BY niv_risk");
80 rs=stmt.executeQuery();
81 rs.next();
82 carga();
83 }
84 catch(SQLException e)
85 {
86
87 }
88 }
89
90 public void configura()
91 {
92 JButton button = new JButton("Print");
93 ActionListener printAction = new ActionListener() {
94 public void actionPerformed(ActionEvent e) {
95 try {
96 MessageFormat headerFormat = new MessageFormat("Page {0}\n CNA");
97 MessageFormat footerFormat = new MessageFormat("- {0} -");
98 table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
99 }
100 catch (PrinterException pe)
101 {
102 System.err.println("Error printing: " + pe.getMessage());
103 }
104 }
105 };
106 button.addActionListener(printAction);
107 }
108
109 public void muestra()
110 {
111 setLayout(new GridLayout(0,1));
112 add(table);
113 add(button);
114 }
115
116 public static void main(String args[]) {
117
118 TablePrint tb= new TablePrint();
119 JFrame frame = new JFrame("Table Printing");
120 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121 frame.getContentPane().add(tb);
122 frame.setSize(600, 300);
123 frame.setVisible(true);
124 }
125 }
Cuando compilo no me genera ni un error, pero a la hora que lo ejecuto sale esto
Código:
--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at TablePrint.carga(TablePrint.java:60)
at TablePrint.<init>(TablePrint.java:31)
at TablePrint.main(TablePrint.java:118)
Process completed.
Si me pudieran ayudar a checar porque no sale la tabla, se los agradecería mucho
Saludos...
Última edición por RiNo_sKa; 19/05/2010 a las 11:12 |