Enviado em 13/01/2017 - 00:01h
Gostaria e quando clicar no botão Atualizar abrisse a JTAble populada do My SQL
create database projetojava;
use projetojava;
CREATE TABLE usuario (
id BIGINT(10) AUTO_INCREMENT,
nome VARCHAR(255),
cpf VARCHAR(255),
email VARCHAR(255),
telefone VARCHAR(255),
PRIMARY KEY (id) );
package dao;
import factory.ConnectionFactory;
import gui.JTableUsuario;
import modelo.Usuario;
import java.sql.*;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* @author brainiac
*/
public class UsuarioDAO {
private Connection connection;
Long id; String nome;
String cpf;
String email;
String telefone;
private Object tbl=null;
private Object rs;
public UsuarioDAO(){
this.connection = new ConnectionFactory().getConnection();
}
public void adiciona(Usuario usuario){
String sql = "INSERT INTO usuario(nome,cpf,email,telefone) VALUES(?,?,?,?)";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, usuario.getNome());
stmt.setString(2, usuario.getCpf());
stmt.setString(3, usuario.getEmail());
stmt.setString(4, usuario.getTelefone());
stmt.execute();
stmt.close();
} catch (SQLException u) {
throw new RuntimeException(u);
}
}
public void consultar(JTable tb){
DefaultTableModel dtm2 = (javax.swing.table.DefaultTableModel)tbl.getModel();
dtm2.setNumRows(0);
//ISO TIRA AS LINHAS DA TABELA
int x=0;
while(rs.next()){
dtm2.addRow(new Object[]{" "," "," "});// cada " " para cada coluna
tbl.setValueAt(rs.getString(1),x,0);
tbl.setValueAt(rs.getString(2),x,1);
tbl.setValueAt(rs.getString(3),x,2);
tbl.setValueAt(rs.getString(4),x,3);
tbl.setValueAt(rs.getString(5),x,4);
}
}
e popular este jTAble ao clicar num botão dela chamado atualizar:
package gui;
/**
*
* @author brainiac
*/
public class UsuarioConsulta extends javax.swing.JFrame {
private Object p;
/**
* Creates new form UsuarioConsulta
*/
public UsuarioConsulta() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
JTableUsuario = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JTableUsuario.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(JTableUsuario);
jButton1.setText("Consulta");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(26, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(156, 156, 156)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(60, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
p.consultar(JTableUsuario);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UsuarioConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UsuarioConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UsuarioConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UsuarioConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UsuarioConsulta().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTable JTableUsuario;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
}
Enviar mensagem ao usuário trabalhando com as opções do php.ini
Meu Fork do Plugin de Integração do CVS para o KDevelop
Compartilhando a tela do Computador no Celular via Deskreen
Como Configurar um Túnel SSH Reverso para Acessar Sua Máquina Local a Partir de uma Máquina Remota
Configuração para desligamento automatizado de Computadores em um Ambiente Comercial
Compartilhamento de Rede com samba em modo Público/Anônimo de forma simples, rápido e fácil
Cups: Mapear/listar todas as impressoras de outro Servidor CUPS de forma rápida e fácil
Criando uma VPC na AWS via CLI
É possível baixar o dicionário pt-br para /usr/share/dict no Linux Min... (1)
Arch Linux - Guia para Iniciantes (0)