Eu tenho uma class TABELAREGISTRO q consulta o banco e joga pra uma Jtable todas as informaçoes vo te mostrar como fiz
so q eu quero chamar essa classe TABELAREGISTRO atraves de um botao em outra class chamada REGISTRO. e esta faltando alguma coisa
1. package DAO;
2.
3. import java.sql.*;
4. import java.awt.*;
5.
6. import javax.swing.*;
7.
8. import Visual.Registro;
9.
10. import java.util.*;
11. import java.awt.Dimension;
12.
13.
14.
15. public class TabelaRegistro extends JPanel {
16. private Connection connection;
17. private JTable table;
18. public JFrame jFrame;
19.
20. public TabelaRegistro() {
21. super();
22.
23.
24.
25.
26. String dbURL ="jdbc:mysql://localhost:3307/sistemacomercial";
27. String usuario = "root";
28. String senha = "123456";
29.
30.
31.
32.
33. try {
34. Class.forName("com.mysql.jdbc.Driver");
35. connection = DriverManager.getConnection(dbURL, usuario, senha);
36. }
37. catch (ClassNotFoundException cnfex) {
38. System.err.println(
39. "");
40. cnfex.printStackTrace();
41. System.exit(1);
42. }
43. catch (SQLException sqlex) {
44. System.err.println("");
45. sqlex.printStackTrace();
46. }
47.
48.
49.
50. }
51.
52. public void getTable()
53. {
54. Statement statement;
55. ResultSet resultset;
56.
57.
58.
59. try {
60. String query = "Select codigo,fornecedor,descricao,quantidade,data,valor,observacao from cadastroproduto order by data";
61. statement = connection.createStatement();
62. resultset = statement.executeQuery(query);
63. displayResultSet(resultset);
64. statement.close();
65. }
66. catch ( SQLException sqlex ) {
67. sqlex.printStackTrace();
68. }
69. }
70.
71. private void displayResultSet(ResultSet rs )
72. throws SQLException
73. {
74. boolean moreRecords = rs.next();
75.
76. if (! moreRecords) {
77. JOptionPane.showMessageDialog(this, "Nao existem registros na tabela!!");
78. //setTitle();
79. return;
80. }
81.
82.
83.
84.
85. Vector columnHeads = new Vector();
86. Vector rows = new Vector();
87.
88. try {
89. ResultSetMetaData rsmd = rs.getMetaData();
90. for (int i = 1; i <= rsmd.getColumnCount(); ++i)
91. columnHeads.addElement(rsmd.getColumnName(i));
92.
93. do {
94. rows.addElement(getNextRow(rs, rsmd));
95. } while (rs.next());
96.
97. table = new JTable(rows, columnHeads);
98.
99.
100. JScrollPane scroller = new JScrollPane(table);
101. add(scroller,BorderLayout.CENTER);
102. scroller.setPreferredSize(new java.awt.Dimension(800, 400));
103. scroller.setSize(new Dimension(900, 294));
104. validate();
105. }
106. catch (SQLException sqlex) {
107. sqlex.printStackTrace();
108. }
109. }
110.
111. public JFrame getJFrame() {
112.
113. return jFrame;
114. }
115.
116. private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd)
117. throws SQLException
118. {
119. Vector currentRow = new Vector();
120. for (int i = 1; i <= rsmd.getColumnCount(); ++i)
121. switch(rsmd.getColumnType(i)) {
122. case Types.VARCHAR: currentRow.addElement(rs.getString(i));
123. break;
124. case Types.INTEGER:currentRow.addElement(new Long(rs.getLong(i)));
125. break;
126. /*case Types.LONGCHAR:currentRow.addElement(rs.getString(i));
127. break;*/
128. default: System.out.println("Tipo dos Dados: " + rsmd.getColumnTypeName(i));
129. }
130. return currentRow;
131. }
132.
133. public void shutDown()
134. {
135. try {
136. connection.close();
137. }
138. catch (SQLException sqlex) {
139. System.err.println("N? foi poss?el desconectar.");
140. sqlex.printStackTrace();
141. }
142. }
143.
144. }
E NA OUTRA CLASS Q EU PRETENDO CHAMAR ESTA TABELAREGISTRO PELO BOTAO ESTA ASSIM:
CLASS REGISTRO{
1. private JButton getJButtonconsultar() {
2. if (jButtonconsultar == null) {
3. jButtonconsultar = new JButton();
4. jButtonconsultar.setBounds(new Rectangle(612, 124, 63, 58));
5. jButtonconsultar.setBackground(SystemColor.activeCaptionText);
6. jButtonconsultar.setToolTipText("Informe a data para consulta");
7.
8.
9. jButtonconsultar.addActionListener(new java.awt.event.ActionListener() {
10. public void actionPerformed(java.awt.event.ActionEvent e) {
11.
12.
13. TabelaRegistro form = new TabelaRegistro();
14. form.setVisible(true);
15. new TabelaRegistro().setVisible(true);
16.
17.
18.
19. }});
20.
21.
22.
23. }
24. return jButtonconsultar;
25. }