Advertisement
buzzonit

crud

Jul 25th, 2016
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.22 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package crud;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.swing.JOptionPane;
  15.  
  16. /**
  17. *
  18. * @author gbastos-inf152
  19. */
  20. public class NewJFrame extends javax.swing.JFrame {
  21.  
  22. /**
  23. * Creates new form NewJFrame
  24. */
  25. public NewJFrame() {
  26. initComponents();
  27. }
  28.  
  29. /**
  30. * This method is called from within the constructor to initialize the form.
  31. * WARNING: Do NOT modify this code. The content of this method is always
  32. * regenerated by the Form Editor.
  33. */
  34. @SuppressWarnings("unchecked")
  35. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  36. private void initComponents() {
  37.  
  38. jPanel1 = new javax.swing.JPanel();
  39. jLabel1 = new javax.swing.JLabel();
  40. jTextField1 = new javax.swing.JTextField();
  41. jLabel3 = new javax.swing.JLabel();
  42. jLabel4 = new javax.swing.JLabel();
  43. jLabel5 = new javax.swing.JLabel();
  44. jLabel6 = new javax.swing.JLabel();
  45. jLabel7 = new javax.swing.JLabel();
  46. jLabel8 = new javax.swing.JLabel();
  47. jLabel9 = new javax.swing.JLabel();
  48. jLabel10 = new javax.swing.JLabel();
  49. jLabel11 = new javax.swing.JLabel();
  50. jLabel12 = new javax.swing.JLabel();
  51. jLabel13 = new javax.swing.JLabel();
  52. jLabel14 = new javax.swing.JLabel();
  53. jPasswordField1 = new javax.swing.JPasswordField();
  54. jTextField2 = new javax.swing.JTextField();
  55. jTextField3 = new javax.swing.JTextField();
  56. jTextField4 = new javax.swing.JTextField();
  57. jTextField5 = new javax.swing.JTextField();
  58. jTextField6 = new javax.swing.JTextField();
  59. jComboBox1 = new javax.swing.JComboBox();
  60. jTextField7 = new javax.swing.JTextField();
  61. jTextField8 = new javax.swing.JTextField();
  62. jTextField10 = new javax.swing.JTextField();
  63. jTextField11 = new javax.swing.JTextField();
  64. jLabel15 = new javax.swing.JLabel();
  65. jTextField12 = new javax.swing.JTextField();
  66. jComboBox2 = new javax.swing.JComboBox();
  67. jButton1 = new javax.swing.JButton();
  68. jButton2 = new javax.swing.JButton();
  69. jButton3 = new javax.swing.JButton();
  70. jButton4 = new javax.swing.JButton();
  71. jButton5 = new javax.swing.JButton();
  72. jButton6 = new javax.swing.JButton();
  73.  
  74. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  75. getContentPane().setLayout(null);
  76.  
  77. jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  78.  
  79. jLabel1.setText("Codigo");
  80. jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 28, -1, -1));
  81.  
  82. jTextField1.addActionListener(new java.awt.event.ActionListener() {
  83. public void actionPerformed(java.awt.event.ActionEvent evt) {
  84. jTextField1ActionPerformed(evt);
  85. }
  86. });
  87. jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, 95, -1));
  88.  
  89. jLabel3.setText("Nome");
  90. jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 63, -1, -1));
  91.  
  92. jLabel4.setText("Endereco");
  93. jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 95, -1, -1));
  94.  
  95. jLabel5.setText("Cidade");
  96. jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 145, -1, -1));
  97.  
  98. jLabel6.setText("Cep");
  99. jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, -1, -1));
  100.  
  101. jLabel7.setText("Estado Civil");
  102. jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 260, -1, -1));
  103.  
  104. jLabel8.setText("Email");
  105. jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 293, -1, -1));
  106.  
  107. jLabel9.setText("Bairro");
  108. jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(221, 154, -1, -1));
  109.  
  110. jLabel10.setText("Fone:");
  111. jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(221, 186, -1, -1));
  112.  
  113. jLabel11.setText("UF");
  114. jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(346, 154, -1, -1));
  115.  
  116. jLabel12.setText("Cel:");
  117. jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(346, 186, -1, -1));
  118.  
  119. jLabel13.setText("Login");
  120. jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 450, -1, -1));
  121.  
  122. jLabel14.setText("Senha");
  123. jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 450, -1, -1));
  124.  
  125. jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
  126. public void actionPerformed(java.awt.event.ActionEvent evt) {
  127. jPasswordField1ActionPerformed(evt);
  128. }
  129. });
  130. jPanel1.add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 450, 120, -1));
  131.  
  132. jTextField2.addActionListener(new java.awt.event.ActionListener() {
  133. public void actionPerformed(java.awt.event.ActionEvent evt) {
  134. jTextField2ActionPerformed(evt);
  135. }
  136. });
  137. jPanel1.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, 360, 20));
  138. jPanel1.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100, 360, 20));
  139. jPanel1.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 140, 140, 20));
  140. jPanel1.add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 190, 140, 20));
  141. jPanel1.add(jTextField6, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 150, 80, 20));
  142.  
  143. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
  144. jPanel1.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 150, -1, -1));
  145. jPanel1.add(jTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 180, 80, 20));
  146. jPanel1.add(jTextField8, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 180, 60, 20));
  147. jPanel1.add(jTextField10, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 290, 170, 20));
  148. jPanel1.add(jTextField11, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 450, 120, 20));
  149.  
  150. jLabel15.setText("Obs:");
  151. jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 350, -1, -1));
  152.  
  153. jTextField12.addActionListener(new java.awt.event.ActionListener() {
  154. public void actionPerformed(java.awt.event.ActionEvent evt) {
  155. jTextField12ActionPerformed(evt);
  156. }
  157. });
  158. jPanel1.add(jTextField12, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 330, 340, 110));
  159.  
  160. jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Casado", "Solteiro", "Separado" }));
  161. jComboBox2.addActionListener(new java.awt.event.ActionListener() {
  162. public void actionPerformed(java.awt.event.ActionEvent evt) {
  163. jComboBox2ActionPerformed(evt);
  164. }
  165. });
  166. jPanel1.add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 260, 170, 20));
  167.  
  168. jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/1469163587_search.png"))); // NOI18N
  169. jButton1.addActionListener(new java.awt.event.ActionListener() {
  170. public void actionPerformed(java.awt.event.ActionEvent evt) {
  171. jButton1ActionPerformed(evt);
  172. }
  173. });
  174. jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 30, 70, 60));
  175.  
  176. jButton2.setText("adicionar button 1");
  177. jButton2.addActionListener(new java.awt.event.ActionListener() {
  178. public void actionPerformed(java.awt.event.ActionEvent evt) {
  179. jButton2ActionPerformed(evt);
  180. }
  181. });
  182. jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 100, 70, 60));
  183.  
  184. jButton3.setText("alterar");
  185. jButton3.addActionListener(new java.awt.event.ActionListener() {
  186. public void actionPerformed(java.awt.event.ActionEvent evt) {
  187. jButton3ActionPerformed(evt);
  188. }
  189. });
  190. jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 170, 70, 50));
  191.  
  192. jButton4.setText("nbutton3");
  193. jButton4.addActionListener(new java.awt.event.ActionListener() {
  194. public void actionPerformed(java.awt.event.ActionEvent evt) {
  195. jButton4ActionPerformed(evt);
  196. }
  197. });
  198. jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 230, 70, 60));
  199.  
  200. jButton5.setText("limpa");
  201. jButton5.addActionListener(new java.awt.event.ActionListener() {
  202. public void actionPerformed(java.awt.event.ActionEvent evt) {
  203. jButton5ActionPerformed(evt);
  204. }
  205. });
  206. jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 300, 70, 60));
  207.  
  208. jButton6.setText("jButton6");
  209. jButton6.addActionListener(new java.awt.event.ActionListener() {
  210. public void actionPerformed(java.awt.event.ActionEvent evt) {
  211. jButton6ActionPerformed(evt);
  212. }
  213. });
  214. jPanel1.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 370, 70, 60));
  215.  
  216. getContentPane().add(jPanel1);
  217. jPanel1.setBounds(0, 0, 670, 540);
  218.  
  219. pack();
  220. }// </editor-fold>
  221.  
  222. private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
  223. // TODO add your handling code here:
  224. }
  225.  
  226. private void jTextField12ActionPerformed(java.awt.event.ActionEvent evt) {
  227. // TODO add your handling code here:
  228. }
  229.  
  230. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  231.  
  232. }
  233.  
  234. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  235. try{
  236.  
  237. Connection con;
  238. con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/alunos_crud_java","alunos","alunos");
  239. String sql;
  240. sql = "INSERT INTO tb_alunos set (nome,endereco,bairro,cep,cidade,estado,fone,cel,estadocivil,"
  241. + "email,obs,login,senha)VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
  242. PreparedStatement ps;
  243. ps = con.prepareStatement (sql);
  244. ps.setString(1, jTextField1.getText());
  245. ps.setString(2, jTextField2.getText());
  246. ps.setString(3, jTextField3.getText());
  247. ps.setString(4, jTextField4.getText());
  248. ps.setString(5, jTextField5.getText());
  249. ps.setString(6, jComboBox1.getSelectedItem().toString());
  250. ps.setString(7, jTextField7.getText());
  251. ps.setString(8, jTextField8.getText());
  252. ps.setString(9, jComboBox2.getSelectedItem().toString());
  253. ps.setString(10, jTextField10.getText());
  254. ps.setString(12, jTextField12.getText());
  255. ps.setString(11, jTextField11.getText());
  256. ps.setString(13, jPasswordField1.getText());
  257.  
  258.  
  259. int res = ps.executeUpdate();
  260. if (res > 0) {
  261. JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso");}
  262. else {
  263. JOptionPane.showMessageDialog(null, "Falha no casdastro");}
  264. }
  265. catch (SQLException ex)
  266. {
  267. JOptionPane.showMessageDialog(null, "SQLException: " );JOptionPane.showMessageDialog(null, "SQLException: "
  268.  
  269. + ex.getMessage () + "\nSQLState: " + ex.getSQLState() + "\nVendorError: " + ex.getErrorCode());
  270.  
  271. }
  272. } // TODO add your handling code here:
  273.  
  274. private void PreparedStatement(String sql) {
  275. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  276.  
  277. }
  278.  
  279. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  280.  
  281. // TODO add your handling code here:
  282. }
  283.  
  284. private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
  285. // TODO add your handling code here:
  286. }
  287.  
  288. private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
  289. try{
  290. Class.forName("com.mysql.jbdc.Driver");
  291. Connection con;
  292. con = DriverManager.getConnection("jbdc:mysql://localhost:3306/projeto_crud_java","alunos","alunos");
  293. String sql;
  294. sql = "UPDATE cad_alunos set Nome = ?,Endereco=?,Bairro=?,CEP=?,Cidade=?,Estado=?,Fone=?,CEL=?,estadocivil=?,"
  295. + "Email=?,Obs=?,login = ? where ID_alunos = ?";
  296. PreparedStatement ps;
  297. ps = con.prepareStatement (sql);
  298. ps.setString(1, jTextField1.getText());
  299. ps.setString(2, jTextField2.getText());
  300. ps.setString(3, jTextField3.getText());
  301. ps.setString(4, jTextField4.getText());
  302. ps.setString(5, jTextField5.getText());
  303. ps.setString(6, jComboBox1.getSelectedItem().toString());
  304. ps.setString(7, jTextField7.getText());
  305. ps.setString(8, jTextField8.getText());
  306. ps.setString(9, jComboBox2.getSelectedItem().toString());
  307. ps.setString(10, jTextField10.getText());
  308. ps.setString(12, jTextField12.getText());
  309. ps.setString(11, jTextField11.getText());
  310.  
  311. ps.setString(1, jTextField1.getText());
  312. int res = ps.executeUpdate();
  313. if (res > 0) {
  314. JOptionPane.showMessageDialog(null, "Alterado com sucesso");}
  315. else {
  316. JOptionPane.showMessageDialog(null, "Nenhum registro alterado");}
  317. } catch (SQLException ex)
  318. {
  319. JOptionPane.showMessageDialog(null, "SQLException: " + ex.getMessage () + "\nSQLState: " + ex.getSQLState() + "\nVendorError: " + ex.getErrorCode());
  320. } catch (ClassNotFoundException ex) {
  321. Logger.getLogger(JFRCurso.class.getName()).log(Level.SEVERE,null, ex);
  322. } // TODO add your handling code here:
  323. }
  324.  
  325. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  326. try{
  327.  
  328. Connection con;
  329. con = DriverManager.getConnection("jbdc:mysql://127.0.01/Nome_Banco","alunos","alunos");
  330. String sql;//string para receber a script
  331. sql = "INSERT INTO cad_alunos(campo1,campo2,campo3)VALUES (?,?,?)";
  332. /*
  333. */
  334. PreparedStatement ps;
  335. ps = con.prepareStatement(sql);
  336. ps.setString(1, jTextField2.getText());
  337. ps.setString(2, jTextField4.getText());
  338. ps.setString(3, jTextField6.getText());
  339.  
  340.  
  341. int res = ps.executeUpdate();
  342. if (res>0){
  343. JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso");}
  344. else{
  345. JOptionPane.showMessageDialog(null, "falha no cadastro");}
  346. }
  347. catch (SQLException ex) //Se ocorrer algum erro uma mesangem e exibida.
  348. {
  349. JOptionPane.showMessageDialog(null, "SQLException: ");
  350. } // TODO add your handling code here:
  351. }
  352.  
  353. private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
  354. // TODO add your handling code here:
  355. }
  356.  
  357. private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
  358. // TODO add your handling code here:
  359. }
  360.  
  361. private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
  362. // TODO add your handling code here:
  363. }
  364.  
  365. /**
  366. * @param args the command line arguments
  367. */
  368. public static void main(String args[]) {
  369. /* Set the Nimbus look and feel */
  370. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  371. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  372. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  373. */
  374. try {
  375. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  376. if ("Nimbus".equals(info.getName())) {
  377. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  378. break;
  379. }
  380. }
  381. } catch (ClassNotFoundException ex) {
  382. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  383. } catch (InstantiationException ex) {
  384. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  385. } catch (IllegalAccessException ex) {
  386. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  387. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  388. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  389. }
  390. //</editor-fold>
  391.  
  392. /* Create and display the form */
  393. java.awt.EventQueue.invokeLater(new Runnable() {
  394. public void run() {
  395. new NewJFrame().setVisible(true);
  396. }
  397. });
  398. }
  399.  
  400. // Variables declaration - do not modify
  401. private javax.swing.JButton jButton1;
  402. private javax.swing.JButton jButton2;
  403. private javax.swing.JButton jButton3;
  404. private javax.swing.JButton jButton4;
  405. private javax.swing.JButton jButton5;
  406. private javax.swing.JButton jButton6;
  407. private javax.swing.JComboBox jComboBox1;
  408. private javax.swing.JComboBox jComboBox2;
  409. private javax.swing.JLabel jLabel1;
  410. private javax.swing.JLabel jLabel10;
  411. private javax.swing.JLabel jLabel11;
  412. private javax.swing.JLabel jLabel12;
  413. private javax.swing.JLabel jLabel13;
  414. private javax.swing.JLabel jLabel14;
  415. private javax.swing.JLabel jLabel15;
  416. private javax.swing.JLabel jLabel3;
  417. private javax.swing.JLabel jLabel4;
  418. private javax.swing.JLabel jLabel5;
  419. private javax.swing.JLabel jLabel6;
  420. private javax.swing.JLabel jLabel7;
  421. private javax.swing.JLabel jLabel8;
  422. private javax.swing.JLabel jLabel9;
  423. private javax.swing.JPanel jPanel1;
  424. private javax.swing.JPasswordField jPasswordField1;
  425. private javax.swing.JTextField jTextField1;
  426. private javax.swing.JTextField jTextField10;
  427. private javax.swing.JTextField jTextField11;
  428. private javax.swing.JTextField jTextField12;
  429. private javax.swing.JTextField jTextField2;
  430. private javax.swing.JTextField jTextField3;
  431. private javax.swing.JTextField jTextField4;
  432. private javax.swing.JTextField jTextField5;
  433. private javax.swing.JTextField jTextField6;
  434. private javax.swing.JTextField jTextField7;
  435. private javax.swing.JTextField jTextField8;
  436. // End of variables declaration
  437. }
  438. ////////////////////////////////////
  439. /*
  440. * To change this license header, choose License Headers in Project Properties.
  441. * To change this template file, choose Tools | Templates
  442. * and open the template in the editor.
  443. */
  444. package crud;
  445.  
  446. import java.sql.Connection;
  447. import java.sql.DriverManager;
  448. import java.sql.PreparedStatement;
  449. import java.sql.SQLException;
  450. import java.util.logging.Level;
  451. import java.util.logging.Logger;
  452. import javax.swing.JOptionPane;
  453.  
  454. /**
  455. *
  456. * @author gbastos-inf152
  457. */
  458. public class NewJFrame extends javax.swing.JFrame {
  459.  
  460. /**
  461. * Creates new form NewJFrame
  462. */
  463. public NewJFrame() {
  464. initComponents();
  465. }
  466.  
  467. /**
  468. * This method is called from within the constructor to initialize the form.
  469. * WARNING: Do NOT modify this code. The content of this method is always
  470. * regenerated by the Form Editor.
  471. */
  472. @SuppressWarnings("unchecked")
  473. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  474. private void initComponents() {
  475.  
  476. jPanel1 = new javax.swing.JPanel();
  477. jLabel1 = new javax.swing.JLabel();
  478. jTextField1 = new javax.swing.JTextField();
  479. jLabel3 = new javax.swing.JLabel();
  480. jLabel4 = new javax.swing.JLabel();
  481. jLabel5 = new javax.swing.JLabel();
  482. jLabel6 = new javax.swing.JLabel();
  483. jLabel7 = new javax.swing.JLabel();
  484. jLabel8 = new javax.swing.JLabel();
  485. jLabel9 = new javax.swing.JLabel();
  486. jLabel10 = new javax.swing.JLabel();
  487. jLabel11 = new javax.swing.JLabel();
  488. jLabel12 = new javax.swing.JLabel();
  489. jLabel13 = new javax.swing.JLabel();
  490. jLabel14 = new javax.swing.JLabel();
  491. jPasswordField1 = new javax.swing.JPasswordField();
  492. jTextField2 = new javax.swing.JTextField();
  493. jTextField3 = new javax.swing.JTextField();
  494. jTextField4 = new javax.swing.JTextField();
  495. jTextField5 = new javax.swing.JTextField();
  496. jTextField6 = new javax.swing.JTextField();
  497. jComboBox1 = new javax.swing.JComboBox();
  498. jTextField7 = new javax.swing.JTextField();
  499. jTextField8 = new javax.swing.JTextField();
  500. jTextField10 = new javax.swing.JTextField();
  501. jTextField11 = new javax.swing.JTextField();
  502. jLabel15 = new javax.swing.JLabel();
  503. jTextField12 = new javax.swing.JTextField();
  504. jComboBox2 = new javax.swing.JComboBox();
  505. jButton1 = new javax.swing.JButton();
  506. jButton2 = new javax.swing.JButton();
  507. jButton3 = new javax.swing.JButton();
  508. jButton4 = new javax.swing.JButton();
  509. jButton5 = new javax.swing.JButton();
  510. jButton6 = new javax.swing.JButton();
  511.  
  512. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  513. getContentPane().setLayout(null);
  514.  
  515. jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  516.  
  517. jLabel1.setText("Codigo");
  518. jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 28, -1, -1));
  519.  
  520. jTextField1.addActionListener(new java.awt.event.ActionListener() {
  521. public void actionPerformed(java.awt.event.ActionEvent evt) {
  522. jTextField1ActionPerformed(evt);
  523. }
  524. });
  525. jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, 95, -1));
  526.  
  527. jLabel3.setText("Nome");
  528. jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 63, -1, -1));
  529.  
  530. jLabel4.setText("Endereco");
  531. jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 95, -1, -1));
  532.  
  533. jLabel5.setText("Cidade");
  534. jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 145, -1, -1));
  535.  
  536. jLabel6.setText("Cep");
  537. jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, -1, -1));
  538.  
  539. jLabel7.setText("Estado Civil");
  540. jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 260, -1, -1));
  541.  
  542. jLabel8.setText("Email");
  543. jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 293, -1, -1));
  544.  
  545. jLabel9.setText("Bairro");
  546. jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(221, 154, -1, -1));
  547.  
  548. jLabel10.setText("Fone:");
  549. jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(221, 186, -1, -1));
  550.  
  551. jLabel11.setText("UF");
  552. jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(346, 154, -1, -1));
  553.  
  554. jLabel12.setText("Cel:");
  555. jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(346, 186, -1, -1));
  556.  
  557. jLabel13.setText("Login");
  558. jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 450, -1, -1));
  559.  
  560. jLabel14.setText("Senha");
  561. jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 450, -1, -1));
  562.  
  563. jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
  564. public void actionPerformed(java.awt.event.ActionEvent evt) {
  565. jPasswordField1ActionPerformed(evt);
  566. }
  567. });
  568. jPanel1.add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 450, 120, -1));
  569.  
  570. jTextField2.addActionListener(new java.awt.event.ActionListener() {
  571. public void actionPerformed(java.awt.event.ActionEvent evt) {
  572. jTextField2ActionPerformed(evt);
  573. }
  574. });
  575. jPanel1.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, 360, 20));
  576. jPanel1.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100, 360, 20));
  577. jPanel1.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 140, 140, 20));
  578. jPanel1.add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 190, 140, 20));
  579. jPanel1.add(jTextField6, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 150, 80, 20));
  580.  
  581. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
  582. jPanel1.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 150, -1, -1));
  583. jPanel1.add(jTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 180, 80, 20));
  584. jPanel1.add(jTextField8, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 180, 60, 20));
  585. jPanel1.add(jTextField10, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 290, 170, 20));
  586. jPanel1.add(jTextField11, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 450, 120, 20));
  587.  
  588. jLabel15.setText("Obs:");
  589. jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 350, -1, -1));
  590.  
  591. jTextField12.addActionListener(new java.awt.event.ActionListener() {
  592. public void actionPerformed(java.awt.event.ActionEvent evt) {
  593. jTextField12ActionPerformed(evt);
  594. }
  595. });
  596. jPanel1.add(jTextField12, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 330, 340, 110));
  597.  
  598. jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Casado", "Solteiro", "Separado" }));
  599. jComboBox2.addActionListener(new java.awt.event.ActionListener() {
  600. public void actionPerformed(java.awt.event.ActionEvent evt) {
  601. jComboBox2ActionPerformed(evt);
  602. }
  603. });
  604. jPanel1.add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 260, 170, 20));
  605.  
  606. jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/1469163587_search.png"))); // NOI18N
  607. jButton1.addActionListener(new java.awt.event.ActionListener() {
  608. public void actionPerformed(java.awt.event.ActionEvent evt) {
  609. jButton1ActionPerformed(evt);
  610. }
  611. });
  612. jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 30, 70, 60));
  613.  
  614. jButton2.setText("adicionar button 1");
  615. jButton2.addActionListener(new java.awt.event.ActionListener() {
  616. public void actionPerformed(java.awt.event.ActionEvent evt) {
  617. jButton2ActionPerformed(evt);
  618. }
  619. });
  620. jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 100, 70, 60));
  621.  
  622. jButton3.setText("alterar");
  623. jButton3.addActionListener(new java.awt.event.ActionListener() {
  624. public void actionPerformed(java.awt.event.ActionEvent evt) {
  625. jButton3ActionPerformed(evt);
  626. }
  627. });
  628. jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 170, 70, 50));
  629.  
  630. jButton4.setText("nbutton3");
  631. jButton4.addActionListener(new java.awt.event.ActionListener() {
  632. public void actionPerformed(java.awt.event.ActionEvent evt) {
  633. jButton4ActionPerformed(evt);
  634. }
  635. });
  636. jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 230, 70, 60));
  637.  
  638. jButton5.setText("limpa");
  639. jButton5.addActionListener(new java.awt.event.ActionListener() {
  640. public void actionPerformed(java.awt.event.ActionEvent evt) {
  641. jButton5ActionPerformed(evt);
  642. }
  643. });
  644. jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 300, 70, 60));
  645.  
  646. jButton6.setText("jButton6");
  647. jButton6.addActionListener(new java.awt.event.ActionListener() {
  648. public void actionPerformed(java.awt.event.ActionEvent evt) {
  649. jButton6ActionPerformed(evt);
  650. }
  651. });
  652. jPanel1.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 370, 70, 60));
  653.  
  654. getContentPane().add(jPanel1);
  655. jPanel1.setBounds(0, 0, 670, 540);
  656.  
  657. pack();
  658. }// </editor-fold>
  659.  
  660. private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
  661. // TODO add your handling code here:
  662. }
  663.  
  664. private void jTextField12ActionPerformed(java.awt.event.ActionEvent evt) {
  665. // TODO add your handling code here:
  666. }
  667.  
  668. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  669.  
  670. }
  671.  
  672. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  673. try{
  674.  
  675. Connection con;
  676. con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/alunos_crud_java","alunos","alunos");
  677. String sql;
  678. sql = "INSERT INTO tb_alunos set (nome,endereco,bairro,cep,cidade,estado,fone,cel,estadocivil,"
  679. + "email,obs,login,senha)VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
  680. PreparedStatement ps;
  681. ps = con.prepareStatement (sql);
  682. ps.setString(1, jTextField1.getText());
  683. ps.setString(2, jTextField2.getText());
  684. ps.setString(3, jTextField3.getText());
  685. ps.setString(4, jTextField4.getText());
  686. ps.setString(5, jTextField5.getText());
  687. ps.setString(6, jComboBox1.getSelectedItem().toString());
  688. ps.setString(7, jTextField7.getText());
  689. ps.setString(8, jTextField8.getText());
  690. ps.setString(9, jComboBox2.getSelectedItem().toString());
  691. ps.setString(10, jTextField10.getText());
  692. ps.setString(12, jTextField12.getText());
  693. ps.setString(11, jTextField11.getText());
  694. ps.setString(13, jPasswordField1.getText());
  695.  
  696.  
  697. int res = ps.executeUpdate();
  698. if (res > 0) {
  699. JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso");}
  700. else {
  701. JOptionPane.showMessageDialog(null, "Falha no casdastro");}
  702. }
  703. catch (SQLException ex)
  704. {
  705. JOptionPane.showMessageDialog(null, "SQLException: " );JOptionPane.showMessageDialog(null, "SQLException: "
  706.  
  707. + ex.getMessage () + "\nSQLState: " + ex.getSQLState() + "\nVendorError: " + ex.getErrorCode());
  708.  
  709. }
  710. } // TODO add your handling code here:
  711.  
  712. private void PreparedStatement(String sql) {
  713. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  714.  
  715. }
  716.  
  717. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  718.  
  719. // TODO add your handling code here:
  720. }
  721.  
  722. private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
  723. // TODO add your handling code here:
  724. }
  725.  
  726. private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
  727. try{
  728. Class.forName("com.mysql.jbdc.Driver");
  729. Connection con;
  730. con = DriverManager.getConnection("jbdc:mysql://localhost:3306/projeto_crud_java","alunos","alunos");
  731. String sql;
  732. sql = "UPDATE td_alunos set nome = ?,Endereco=?,Bairro=?,CEP=?,Cidade=?,Estado=?,Fone=?,CEL=?,estadocivil=?,"
  733. + "Email=?,Obs=?,login = ? where ID_alunos = ?";
  734. PreparedStatement ps;
  735.  
  736. ps = con.prepareStatement (sql);
  737.  
  738. ps.setString(1, jTextField1.getText());
  739. ps.setString(2, jTextField2.getText());
  740. ps.setString(3, jTextField3.getText());
  741. ps.setString(4, jTextField4.getText());
  742. ps.setString(5, jTextField5.getText());
  743. ps.setString(6, jComboBox1.getSelectedItem().toString());
  744. ps.setString(7, jTextField7.getText());
  745. ps.setString(8, jTextField8.getText());
  746. ps.setString(9, jComboBox2.getSelectedItem().toString());
  747. ps.setString(10, jTextField10.getText());
  748. ps.setString(12, jTextField12.getText());
  749. ps.setString(11, jTextField11.getText());
  750.  
  751. ps.setString(1, jTextField1.getText());
  752.  
  753. int res = ps.executeUpdate();
  754. if (res > 0) {
  755. JOptionPane.showMessageDialog(null, "Alterado com sucesso");}
  756. else {
  757. JOptionPane.showMessageDialog(null, "Nenhum registro alterado");}
  758. } catch (SQLException ex)
  759. {
  760. JOptionPane.showMessageDialog(null, "SQLException: " + ex.getMessage () + "\nSQLState: " + ex.getSQLState() + "\nVendorError: " + ex.getErrorCode());
  761. } catch (ClassNotFoundException ex) {
  762. Logger.getLogger(JFRCurso.class.getName()).log(Level.SEVERE, null, ex);
  763. } // TODO add your handling code here:
  764. }
  765.  
  766. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  767. try{
  768. Class.forName("com.mysql.jbdc.Driver");
  769. Connection con;
  770. con = DriverManager.getConnection("jbdc:mysql://127.0.01/Nome_Banco","alunos","alunos");
  771. String sql = "Select * from cad_alunos Where ID_alunos =? ";
  772. PreparedStatement ps = con.prepareStatement(sql);
  773. ps.setString(1, JTextField1.getText());
  774. ResultSet rs;
  775. rs = ps.execuseQuery();
  776. if (rs.next ()) {
  777.  
  778.  
  779. jTextField2.setText(rs.getString("nome"));
  780. jTextField2.setText(rs.getString("endereco"));
  781. jTextField2.setText(rs.getString("cidade"));
  782. jTextField2.setText(rs.getString("bairro"));
  783. jComboBox1.setSelectedItem(rs.getString("estado"));
  784. jTextField6.setText(rs.getString("cep"));
  785. jTextField7.setText(rs.getString("fone"));
  786. jTextField8.setText(rs.getString("cel"));
  787. jTextField9.setText(rs.getString("email"));
  788. jTextField11.setText(rs.getString("obs"));
  789. }else{
  790.  
  791.  
  792. JOptionPane.showMessageDialog(null, "Registro nao encontrado", "Mensagem", JOptionPane.WARNING_MESSAGE);
  793. }
  794. } catch (SQLException ex)
  795. {
  796.  
  797. JOptionPane.showMessageDialog(null, "SQLException: " + ex.getMessage () + "\nSQLState: " + ex.getSQLState() + "\nVendorError: " + ex.getErrorCode());
  798.  
  799. } catch (ClassNotFoundException ex) {
  800. Logger.getLogger(JFRCurso.class.getName()).log(Level.SEVERE, null, ex);
  801. } // TODO add your handling code here:
  802. }
  803.  
  804.  
  805. }
  806. catch (SQLException ex) //Se ocorrer algum erro uma mesangem e exibida.
  807. {
  808. JOptionPane.showMessageDialog(null, "SQLException: ");
  809. } // TODO add your handling code here:
  810. }
  811.  
  812. private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
  813. // TODO add your handling code here:
  814. }
  815.  
  816. private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
  817. // TODO add your handling code here:
  818. }
  819.  
  820. private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
  821. // TODO add your handling code here:
  822. }
  823.  
  824. /**
  825. * @param args the command line arguments
  826. */
  827. public static void main(String args[]) {
  828. /* Set the Nimbus look and feel */
  829. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  830. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  831. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  832. */
  833. try {
  834. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  835. if ("Nimbus".equals(info.getName())) {
  836. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  837. break;
  838. }
  839. }
  840. } catch (ClassNotFoundException ex) {
  841. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  842. } catch (InstantiationException ex) {
  843. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  844. } catch (IllegalAccessException ex) {
  845. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  846. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  847. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  848. }
  849. //</editor-fold>
  850.  
  851. /* Create and display the form */
  852. java.awt.EventQueue.invokeLater(new Runnable() {
  853. public void run() {
  854. new NewJFrame().setVisible(true);
  855. }
  856. });
  857. }
  858.  
  859. // Variables declaration - do not modify
  860. private javax.swing.JButton jButton1;
  861. private javax.swing.JButton jButton2;
  862. private javax.swing.JButton jButton3;
  863. private javax.swing.JButton jButton4;
  864. private javax.swing.JButton jButton5;
  865. private javax.swing.JButton jButton6;
  866. private javax.swing.JComboBox jComboBox1;
  867. private javax.swing.JComboBox jComboBox2;
  868. private javax.swing.JLabel jLabel1;
  869. private javax.swing.JLabel jLabel10;
  870. private javax.swing.JLabel jLabel11;
  871. private javax.swing.JLabel jLabel12;
  872. private javax.swing.JLabel jLabel13;
  873. private javax.swing.JLabel jLabel14;
  874. private javax.swing.JLabel jLabel15;
  875. private javax.swing.JLabel jLabel3;
  876. private javax.swing.JLabel jLabel4;
  877. private javax.swing.JLabel jLabel5;
  878. private javax.swing.JLabel jLabel6;
  879. private javax.swing.JLabel jLabel7;
  880. private javax.swing.JLabel jLabel8;
  881. private javax.swing.JLabel jLabel9;
  882. private javax.swing.JPanel jPanel1;
  883. private javax.swing.JPasswordField jPasswordField1;
  884. private javax.swing.JTextField jTextField1;
  885. private javax.swing.JTextField jTextField10;
  886. private javax.swing.JTextField jTextField11;
  887. private javax.swing.JTextField jTextField12;
  888. private javax.swing.JTextField jTextField2;
  889. private javax.swing.JTextField jTextField3;
  890. private javax.swing.JTextField jTextField4;
  891. private javax.swing.JTextField jTextField5;
  892. private javax.swing.JTextField jTextField6;
  893. private javax.swing.JTextField jTextField7;
  894. private javax.swing.JTextField jTextField8;
  895. // End of variables declaration
  896. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement