Created
May 23, 2018 23:17
-
-
Save erick-jeronimo/d48f05545b6bc4961d1a7f754ed74283 to your computer and use it in GitHub Desktop.
Tela Correspondente ao cadastro de usuários
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package javaapplication1; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.JOptionPane; | |
import net.proteanit.sql.DbUtils; | |
/** | |
* | |
* @author aluno | |
*/ | |
public class Tela extends javax.swing.JFrame { | |
/** | |
* Creates new form Tela | |
*/ | |
public Tela() { | |
initComponents(); | |
this.setLocationRelativeTo(null); | |
getRootPane().setDefaultButton(jButton3); | |
atualizarTabela(); | |
} | |
/** | |
* 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(); | |
jTable1 = new javax.swing.JTable(); | |
jButton3 = new javax.swing.JButton(); | |
jLabel1 = new javax.swing.JLabel(); | |
jTextField1 = new javax.swing.JTextField(); | |
jLabel2 = new javax.swing.JLabel(); | |
jPasswordField1 = new javax.swing.JPasswordField(); | |
jTextField2 = new javax.swing.JTextField(); | |
jLabel3 = new javax.swing.JLabel(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
jTable1.setModel(new javax.swing.table.DefaultTableModel( | |
new Object [][] { | |
}, | |
new String [] { | |
"Nome", "Senha" | |
} | |
)); | |
jScrollPane1.setViewportView(jTable1); | |
jButton3.setText("Cadastrar"); | |
jButton3.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton3ActionPerformed(evt); | |
} | |
}); | |
jLabel1.setText("Nome"); | |
jLabel2.setText("Login"); | |
jLabel3.setText("Senha"); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE) | |
.addGroup(layout.createSequentialGroup() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jLabel1)) | |
.addGap(18, 18, 18) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jLabel2) | |
.addGap(0, 0, Short.MAX_VALUE)) | |
.addComponent(jTextField2))))) | |
.addGroup(layout.createSequentialGroup() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(187, 187, 187) | |
.addComponent(jButton3)) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(94, 94, 94) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jLabel3)))) | |
.addGap(0, 0, Short.MAX_VALUE))) | |
.addContainerGap()) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jLabel1) | |
.addComponent(jLabel2)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jLabel3) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE) | |
.addComponent(jButton3) | |
.addGap(6, 6, 6)) | |
); | |
pack(); | |
}// </editor-fold> | |
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { | |
try { | |
String nome = jTextField1.getText(); | |
String login = jTextField2.getText(); | |
String senha = new String(jPasswordField1.getPassword()); | |
String query = "INSERT INTO usuarios (nome, login, senha) VALUES (?, ?, ?)"; | |
Connection conn = Conexao.getConnection(); | |
PreparedStatement psmt = conn.prepareStatement(query); | |
psmt.setString(1, nome); | |
psmt.setString(2, login); | |
psmt.setString(3, senha); | |
psmt.execute(); | |
JOptionPane.showMessageDialog(null, "Cadastro Realizado com Sucesso!", "SGU", JOptionPane.INFORMATION_MESSAGE); | |
jTextField1.setText(""); | |
jTextField2.setText(""); | |
jPasswordField1.setText(""); | |
atualizarTabela(); | |
} catch (SQLException ex) { | |
JOptionPane.showMessageDialog(null, "Erro ao Executar a Operação!", "SGU", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
public void atualizarTabela() { | |
try { | |
String query = "SELECT * FROM usuarios"; | |
Connection conn = Conexao.getConnection(); | |
Statement smt = conn.createStatement(); | |
ResultSet rs = smt.executeQuery(query); | |
jTable1.setModel(DbUtils.resultSetToTableModel(rs)); | |
} catch (SQLException ex) { | |
Logger.getLogger(Tela.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* @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(Tela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(Tela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(Tela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(Tela.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 Tela().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JButton jButton3; | |
private javax.swing.JLabel jLabel1; | |
private javax.swing.JLabel jLabel2; | |
private javax.swing.JLabel jLabel3; | |
private javax.swing.JPasswordField jPasswordField1; | |
private javax.swing.JScrollPane jScrollPane1; | |
private javax.swing.JTable jTable1; | |
private javax.swing.JTextField jTextField1; | |
private javax.swing.JTextField jTextField2; | |
// End of variables declaration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment