Fala brother.
Segue um exemplo de código que imprime consultas em uma Table por palavras chave.
Está integrado com PostGres.
Para que você possa editar as tabelas e atualizar o Banco é necessário que você implemente sua DefaultTableModel.
Dê uma pesquisada aqui no portal em MVC.
Abraço.
Charles
[code]/**
- This code was generated using CloudGarden’s Jigloo
- SWT/Swing GUI Builder, which is free for non-commercial
- use. If Jigloo is being used commercially (ie, by a corporation,
- company or business for any purpose whatever) then you
- should purchase a license for each developer using Jigloo.
- Please visit www.cloudgarden.com for details.
- Use of Jigloo implies acceptance of these licensing terms.
- A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED
- for this machine, so Jigloo or this code cannot be used legally
- for any corporate or commercial purpose.
*/
package Teste;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Insets;
import com.cloudgarden.layout.AnchorLayout;
import javax.swing.JLabel;
import com.cloudgarden.layout.AnchorConstraint;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
/**
- This code was generated using CloudGarden’s Jigloo
- SWT/Swing GUI Builder, which is free for non-commercial
- use. If Jigloo is being used commercially (ie, by a corporation,
- company or business for any purpose whatever) then you
- should purchase a license for each developer using Jigloo.
- Please visit www.cloudgarden.com for details.
- Use of Jigloo implies acceptance of these licensing terms.
- A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED
- for this machine, so Jigloo or this code cannot be used legally
- for any corporate or commercial purpose.
/
/*
-
@author Darimont
-
@version 1.0
- Date of creation: 10.03.2004
- File: JTableExample.java
- Modifier: Darimont
- Revision: Revision
- State: Germany
*/
public class ConsulPal extends JPanel {
private static final String dbDriverClass = “org.postgresql.Driver”;
static {
try {
Thread
.currentThread()
.getContextClassLoader()
.loadClass(dbDriverClass)
.newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private JButton btnConnect, btnRefresh;
private JTextField txtSQL;
private JTable table;
private JLabel jLabel1;
private JScrollPane scrollPane;
private DefaultTableModel dtm;
private javax.swing.JPanel btnPanel, contentPanel;
private Connection con;
private ResultSet rs;
public ConsulPal() {
super();
GridBagConstraints gridBagConstraints;
contentPanel = new JPanel();
btnPanel = new JPanel();
AnchorLayout btnPanelLayout = new AnchorLayout();
btnPanel.setLayout(btnPanelLayout);
btnConnect = new JButton();
AnchorLayout btnConnectLayout = new AnchorLayout();
btnConnect.setLayout(btnConnectLayout);
table = new JTable();
dtm = new DefaultTableModel();
setLayout(new FlowLayout());
contentPanel.setLayout(new GridBagLayout());
contentPanel.setMinimumSize(new Dimension(400, 300));
contentPanel.setPreferredSize(new java.awt.Dimension(400, 328));
{
scrollPane = new JScrollPane();
contentPanel.add(scrollPane, new GridBagConstraints(
-1,
-1,
GridBagConstraints.RELATIVE,
GridBagConstraints.RELATIVE,
0.0,
0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0,
0));
scrollPane.setMinimumSize(new Dimension(400, 200));
scrollPane.setPreferredSize(new Dimension(400, 200));
}
btnPanel.setMinimumSize(new Dimension(400, 100));
btnPanel.setPreferredSize(new Dimension(400, 100));
{
jLabel1 = new JLabel();
btnPanel.add(jLabel1, new AnchorConstraint(
435,
368,
735,
51,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL));
jLabel1.setText(“Digite o nome da obra:”);
jLabel1.setPreferredSize(new java.awt.Dimension(127, 30));
}
btnConnect.setText(“Conectar”);
btnPanel.add(btnConnect, new AnchorConstraint(
165,
498,
425,
266,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL));
btnConnect.setPreferredSize(new java.awt.Dimension(93, 26));
{
btnRefresh = new JButton();
btnPanel.add(btnRefresh, new AnchorConstraint(
175,
766,
435,
533,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL));
AnchorLayout btnRefreshLayout = new AnchorLayout();
btnRefresh.setLayout(btnRefreshLayout);
btnRefresh.setText(“Consultar”);
btnRefresh.setPreferredSize(new java.awt.Dimension(93, 26));
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
refresh();
}
});
}
{
txtSQL = new JTextField();
btnPanel.add(txtSQL, new AnchorConstraint(
495,
943,
665,
391,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL,
AnchorConstraint.ANCHOR_REL));
txtSQL.setColumns(25);
txtSQL.setPreferredSize(new java.awt.Dimension(221, 17));
}
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
contentPanel.add(btnPanel, gridBagConstraints);
btnPanel.setAutoscrolls(true);
btnPanel.setSize(400, 370);
table.setModel(dtm);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);
add(contentPanel);
this.setSize(441, 318);
btnConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
connect();
}
});
setVisible(true);
}
/**
*
*/
protected void refresh() {
// TODO Auto-generated method stub
try {
if (con == null || con.isClosed()) {
JOptionPane.showMessageDialog(
this,
“É necessário fazer a conexão!”,
“Mensagem”,
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Statement stmt = null;
try {
stmt = con.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (stmt == null) {
throw new RuntimeException(“stmt is null”);
}
String texto = txtSQL.getText();
String query=(“SELECT * from obra where nome like’%”+texto+"%’");
if (texto == null) {
JOptionPane.showMessageDialog(
this,
“”,
“”,
JOptionPane.ERROR_MESSAGE);
return;
}
try {
rs = stmt.executeQuery(query);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
putRSinTableModel(rs, dtm);
table.setModel(dtm);
table.updateUI();
}
/**
-
@param rs
-
@param dtm
*/
private void putRSinTableModel(ResultSet rs, DefaultTableModel dtm) {
// TODO Auto-generated method stub
ResultSetMetaData rsmd = null;
int clmCnt = -1;
try {
rsmd = rs.getMetaData();
clmCnt = rsmd.getColumnCount();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (rsmd == null || clmCnt == -1) {
throw new RuntimeException(“rsmd is null”);
}
try {
Object[] clmHeaders = new Object[clmCnt];
for (int i = 1; i <= clmCnt; i++) {
clmHeaders[i - 1] = rsmd.getColumnName(i);
}
Vector rows = new Vector();
Vector rowData = null;
int row = 0;
while (rs.next()) {
rowData = new Vector();
for (int i = 1; i <= clmCnt; i++) {
String fieldVal = rs.getString(i);
System.out.print(fieldVal + " ");
rowData.add(fieldVal);
}
rows.add(rowData);
System.out.println();
}
dtm.setDataVector(rows, new Vector(Arrays.asList(clmHeaders)));
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/**
*
*/
protected void connect() {
// TODO Auto-generated method stub
if (con != null)
return;
String host = “192.168.2.4”;
String porta = “5432”;
String base = “biblioteca”;
String dbURL = “jdbc:postgresql://” + host + “:” + porta + “/” + base;
String usuario = “AdminBi”;
String senha = “dbbi”;
try {
con = DriverManager.getConnection(dbURL, usuario, senha);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Auto-generated method for setting the popup menu for a component
*/
private void setComponentPopupMenu(final java.awt.Component parent, final javax.swing.JPopupMenu menu) {
parent.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
if(e.isPopupTrigger())
menu.show(parent, e.getX(), e.getY());
}
public void mouseReleased(java.awt.event.MouseEvent e) {
if(e.isPopupTrigger())
menu.show(parent, e.getX(), e.getY());
}
});
}
}[/code]