Created
September 20, 2016 00:48
-
-
Save antunesleo/ab482b5c39ce339c4513b66065b2d3a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package exemplobanco; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.sql.PreparedStatement; | |
/* | |
* 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. | |
*/ | |
/** | |
* | |
* @author ehkishimot | |
*/ | |
public class ContatoJFrame extends javax.swing.JFrame { | |
/** | |
* Creates new form ContatoJFrame | |
*/ | |
public ContatoJFrame() { | |
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() { | |
jTextField1 = new javax.swing.JTextField(); | |
jTextField2 = new javax.swing.JTextField(); | |
jButton1 = new javax.swing.JButton(); | |
jTextField3 = new javax.swing.JTextField(); | |
jLabel1 = new javax.swing.JLabel(); | |
jLabel2 = new javax.swing.JLabel(); | |
jLabel3 = new javax.swing.JLabel(); | |
jButton2 = new javax.swing.JButton(); | |
jButton3 = new javax.swing.JButton(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
jTextField2.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jTextField2ActionPerformed(evt); | |
} | |
}); | |
jButton1.setText("Inserir"); | |
jButton1.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton1ActionPerformed(evt); | |
} | |
}); | |
jTextField3.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jTextField3ActionPerformed(evt); | |
} | |
}); | |
jLabel1.setText("Id"); | |
jLabel2.setText("Nome"); | |
jLabel3.setText("Idade"); | |
jButton2.setText("Deletar"); | |
jButton2.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton2ActionPerformed(evt); | |
} | |
}); | |
jButton3.setText("Atualizar"); | |
jButton3.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton3ActionPerformed(evt); | |
} | |
}); | |
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() | |
.addGap(84, 84, 84) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(10, 10, 10) | |
.addComponent(jLabel2)) | |
.addComponent(jLabel3) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jButton1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE) | |
.addComponent(jButton2)))) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(92, 92, 92) | |
.addComponent(jLabel1))) | |
.addGap(87, 87, 87)) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addContainerGap(163, Short.MAX_VALUE) | |
.addComponent(jButton3) | |
.addGap(162, 162, 162))) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(38, 38, 38) | |
.addComponent(jLabel1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jLabel2) | |
.addGap(4, 4, 4) | |
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGap(2, 2, 2) | |
.addComponent(jLabel3) | |
.addGap(2, 2, 2) | |
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGap(45, 45, 45) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jButton1) | |
.addComponent(jButton2)) | |
.addContainerGap(72, Short.MAX_VALUE)) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addContainerGap(215, Short.MAX_VALUE) | |
.addComponent(jButton3) | |
.addGap(62, 62, 62))) | |
); | |
pack(); | |
}// </editor-fold> | |
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { | |
String nome = jTextField2.getText(); | |
int idade = Integer.parseInt(jTextField3.getText()); | |
try{ | |
Connection con = FabricaConexao.pegaConexao(); | |
String sql = "INSERT INTO CONTATO (nome, idade) VALUES (?,?)"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, nome); | |
ps.setInt(2, idade); | |
ps.execute(); | |
} catch(SQLException e) { | |
System.err.println("PROBLEMA " + e.getMessage()); | |
} | |
} | |
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) { | |
// TODO add your handling code here: | |
} | |
private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) { | |
// TODO add your handling code here: | |
} | |
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { | |
int id = Integer.parseInt(jTextField1.getText()); | |
String nome = jTextField2.getText(); | |
int idade = Integer.parseInt(jTextField3.getText()); | |
try{ | |
Connection con = FabricaConexao.pegaConexao(); | |
String sql = "DELETE FROM CONTATO WHERE id = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setInt(1, id); | |
ps.execute(); | |
} catch(SQLException e) { | |
System.err.println("PROBLEMA " + e.getMessage()); | |
} | |
} | |
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { | |
int id = Integer.parseInt(jTextField1.getText()); | |
String nome = jTextField2.getText(); | |
int idade = Integer.parseInt(jTextField3.getText()); | |
try{ | |
Connection con = FabricaConexao.pegaConexao(); | |
String sql = "UPDATE CONTATO SET nome = ?, idade = ? WHERE id = ?"; | |
PreparedStatement ps = con.prepareStatement(sql); | |
ps.setString(1, nome); | |
ps.setInt(2, idade); | |
ps.setInt(3, id); | |
ps.execute(); | |
} catch(SQLException e) { | |
System.err.println("PROBLEMA " + e.getMessage()); | |
} | |
} | |
/** | |
* @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(ContatoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(ContatoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(ContatoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(ContatoJFrame.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 ContatoJFrame().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JButton jButton1; | |
private javax.swing.JButton jButton2; | |
private javax.swing.JButton jButton3; | |
private javax.swing.JLabel jLabel1; | |
private javax.swing.JLabel jLabel2; | |
private javax.swing.JLabel jLabel3; | |
private javax.swing.JTextField jTextField1; | |
private javax.swing.JTextField jTextField2; | |
private javax.swing.JTextField jTextField3; | |
// End of variables declaration | |
} |
This file contains hidden or 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 exemplobanco; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
/** | |
* | |
* @author ehkishimot | |
*/ | |
public class FabricaConexao { | |
public static Connection con = null; | |
public static Connection pegaConexao() { | |
try { | |
if (con == null) { | |
con = DriverManager.getConnection("jdbc:mysql://localhost/teste","root","unimep"); | |
} | |
} catch (SQLException e) { | |
System.out.println(e); | |
} | |
return con; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment