aquaballoon

Java - JDBC

May 5th, 2014
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.52 KB | None | 0 0
  1. // DbConn.java
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. package mysql;
  10.  
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.Statement;
  16. import javax.swing.JOptionPane;
  17.  
  18. /**
  19.  *
  20.  * @author jlee
  21.  */
  22. public class DbConn {
  23.    
  24.     String driver = "com.mysql.jdbc.Driver";
  25.     String url = "jdbc:mysql://localhost/ITDB";
  26.     String user = "root";
  27.     String pass = "abc123";
  28.  
  29.     Connection conn;
  30.     //Statement stm;
  31.     PreparedStatement stm;
  32.     ResultSet result;
  33.    
  34.     DbConn(String sql){
  35.         try {
  36.             Class.forName(driver);
  37.             conn = DriverManager.getConnection(url, user, pass);
  38.             //stm = conn.createStatement();
  39.             stm = conn.prepareStatement(sql);
  40.  
  41.         } catch (Exception e) {
  42.             JOptionPane.showMessageDialog(null, "Warning: " + e.getMessage());
  43.         }
  44.     }
  45. }
  46.  
  47. // Init.java
  48.  
  49. /*
  50.  * To change this license header, choose License Headers in Project Properties.
  51.  * To change this template file, choose Tools | Templates
  52.  * and open the template in the editor.
  53.  */
  54.  
  55. package mysql;
  56.  
  57. /**
  58.  *
  59.  * @author jlee
  60.  */
  61. public class Init {
  62.     public static void main(String[] args){
  63.         new MainWin().setVisible(true);
  64.     }
  65.    
  66. }
  67.  
  68. // Register.java
  69.  
  70. /*
  71.  * To change this license header, choose License Headers in Project Properties.
  72.  * To change this template file, choose Tools | Templates
  73.  * and open the template in the editor.
  74.  */
  75.  
  76. package mysql;
  77.  
  78. import javax.swing.JOptionPane;
  79.  
  80. /**
  81.  *
  82.  * @author jlee
  83.  */
  84. public class Reg extends javax.swing.JFrame {
  85.  
  86.     /**
  87.      * Creates new form Reg
  88.      */
  89.    
  90. //    String driver = "com.mysql.jdbc.Driver";
  91. //    String url = "jdbc:mysql://localhost/ITDB";
  92. //    String user = "root";
  93. //    String pass = "abc123";
  94. //
  95. //    Connection conn;
  96. //    Statement stm;
  97. //    ResultSet result;
  98.    
  99.     DbConn dbConn;
  100.    
  101.     public Reg() {
  102.         initComponents();
  103.     }
  104.  
  105.     /**
  106.      * This method is called from within the constructor to initialize the form.
  107.      * WARNING: Do NOT modify this code. The content of this method is always
  108.      * regenerated by the Form Editor.
  109.      */
  110.     @SuppressWarnings("unchecked")
  111.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  112.     private void initComponents() {
  113.  
  114.         btSave = new javax.swing.JButton();
  115.         outName = new javax.swing.JLabel();
  116.         jLabel2 = new javax.swing.JLabel();
  117.         inName = new javax.swing.JTextField();
  118.         inEmail = new javax.swing.JTextField();
  119.         btCancel = new javax.swing.JButton();
  120.  
  121.         btSave.setText("Save");
  122.         btSave.addActionListener(new java.awt.event.ActionListener() {
  123.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  124.                 btSaveActionPerformed(evt);
  125.             }
  126.         });
  127.  
  128.         outName.setText("Name");
  129.  
  130.         jLabel2.setText("Email");
  131.  
  132.         btCancel.setText("Cancel");
  133.  
  134.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  135.         getContentPane().setLayout(layout);
  136.         layout.setHorizontalGroup(
  137.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138.             .addGroup(layout.createSequentialGroup()
  139.                 .addGap(24, 24, 24)
  140.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  141.                     .addGroup(layout.createSequentialGroup()
  142.                         .addComponent(btSave)
  143.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
  144.                         .addComponent(btCancel))
  145.                     .addGroup(layout.createSequentialGroup()
  146.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.                             .addComponent(outName)
  148.                             .addComponent(jLabel2))
  149.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150.                             .addGroup(layout.createSequentialGroup()
  151.                                 .addGap(18, 18, 18)
  152.                                 .addComponent(inName))
  153.                             .addGroup(layout.createSequentialGroup()
  154.                                 .addGap(20, 20, 20)
  155.                                 .addComponent(inEmail)))))
  156.                 .addGap(25, 25, 25))
  157.         );
  158.         layout.setVerticalGroup(
  159.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  160.             .addGroup(layout.createSequentialGroup()
  161.                 .addGap(25, 25, 25)
  162.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  163.                     .addComponent(outName)
  164.                     .addComponent(inName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  165.                 .addGap(19, 19, 19)
  166.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  167.                     .addComponent(jLabel2)
  168.                     .addComponent(inEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  169.                 .addGap(18, 18, 18)
  170.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  171.                     .addComponent(btSave)
  172.                     .addComponent(btCancel))
  173.                 .addContainerGap(29, Short.MAX_VALUE))
  174.         );
  175.  
  176.         pack();
  177.     }// </editor-fold>                        
  178.  
  179.     private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {                                      
  180.         // TODO add your handling code here:
  181.        
  182.         dbConn = new DbConn("");
  183.        
  184.         try {
  185. //            Class.forName(driver);
  186. //            conn = DriverManager.getConnection(url, user, pass);
  187. //            stm = conn.createStatement();
  188.            
  189.             String name = inName.getText();
  190.             String email = inEmail.getText();
  191.            
  192.             String sql = "insert into ITTB(name, email) values('"+name+"', '"+email+"')";
  193.             dbConn.stm.executeUpdate(sql);
  194.  
  195.         } catch (Exception e) {
  196.             JOptionPane.showMessageDialog(null, "Warning: " + e.getMessage());
  197.         }
  198.        
  199.        
  200.        
  201.         dispose();
  202.     }                                      
  203.  
  204.     /**
  205.      * @param args the command line arguments
  206.      */
  207. //    public static void main(String args[]) {
  208. //        /* Set the Nimbus look and feel */
  209. //        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  210. //        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  211. //         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  212. //         */
  213. //        try {
  214. //            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  215. //                if ("Nimbus".equals(info.getName())) {
  216. //                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
  217. //                    break;
  218. //                }
  219. //            }
  220. //        } catch (ClassNotFoundException ex) {
  221. //            java.util.logging.Logger.getLogger(Reg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  222. //        } catch (InstantiationException ex) {
  223. //            java.util.logging.Logger.getLogger(Reg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  224. //        } catch (IllegalAccessException ex) {
  225. //            java.util.logging.Logger.getLogger(Reg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  226. //        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  227. //            java.util.logging.Logger.getLogger(Reg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  228. //        }
  229. //        //</editor-fold>
  230. //
  231. //        /* Create and display the form */
  232. //        java.awt.EventQueue.invokeLater(new Runnable() {
  233. //            public void run() {
  234. //                new Reg().setVisible(true);
  235. //            }
  236. //        });
  237. //    }
  238.  
  239.     // Variables declaration - do not modify                    
  240.     private javax.swing.JButton btCancel;
  241.     private javax.swing.JButton btSave;
  242.     private javax.swing.JTextField inEmail;
  243.     private javax.swing.JTextField inName;
  244.     private javax.swing.JLabel jLabel2;
  245.     private javax.swing.JLabel outName;
  246.     // End of variables declaration                  
  247. }
  248.  
  249.  
  250. // Upgrade.java
  251. /*
  252.  * To change this license header, choose License Headers in Project Properties.
  253.  * To change this template file, choose Tools | Templates
  254.  * and open the template in the editor.
  255.  */
  256. package mysql;
  257.  
  258. import java.sql.Connection;
  259. import java.sql.DriverManager;
  260. import java.sql.ResultSet;
  261. import java.sql.SQLException;
  262. import java.sql.Statement;
  263. import javax.swing.JOptionPane;
  264.  
  265. /**
  266.  *
  267.  * @author jlee
  268.  */
  269. public class Upgrade extends javax.swing.JFrame {
  270.  
  271.     /**
  272.      * Creates new form Upgrade
  273.      */
  274. //    String driver = "com.mysql.jdbc.Driver";
  275. //    String url = "jdbc:mysql://localhost/ITDB";
  276. //    String user = "root";
  277. //    String pass = "abc123";
  278. //
  279. //    Connection conn;
  280. //    Statement stm;
  281. //    ResultSet result;
  282.    
  283.     DbConn dbConn;
  284.    
  285.     String search;
  286.  
  287.     public Upgrade(String search) {
  288.         this.search = search;
  289.         //dbConn();
  290.         initComponents();
  291.         //outName.setText(search);
  292.  
  293.         try {
  294.  
  295.             String sql = "SELECT * FROM ITTB where name = '" + search + "'";
  296.             //PreparedStatement stm = conn.prepareStatement(sSQL);
  297.            
  298.             //stm = conn.createStatement();
  299.             //ResultSet rs = stm.executeQuery(sql);
  300.            
  301.             dbConn = new DbConn(sql);
  302.             dbConn.result = dbConn.stm.executeQuery();
  303.  
  304.             while (dbConn.result.next()) {
  305.                 String name = dbConn.result.getString("name");
  306.                 String email = dbConn.result.getString("email");
  307.  
  308.                 outName.setText(name);
  309.                 outEmail.setText(email);
  310.             }
  311.  
  312.         } catch (SQLException ex) {
  313.             JOptionPane.showMessageDialog(null, ex);
  314.         }
  315.         //System.out.println(search);
  316.  
  317.     }
  318.  
  319. //    void dbConn() {
  320. //        try {
  321. //            Class.forName(driver);
  322. //            conn = DriverManager.getConnection(url, user, pass);
  323. //            stm = conn.createStatement();
  324. //
  325. //        } catch (Exception e) {
  326. //            JOptionPane.showMessageDialog(null, "Warning: " + e.getMessage());
  327. //        }
  328. //    }
  329.  
  330.     /**
  331.      * This method is called from within the constructor to initialize the form.
  332.      * WARNING: Do NOT modify this code. The content of this method is always
  333.      * regenerated by the Form Editor.
  334.      */
  335.     @SuppressWarnings("unchecked")
  336.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  337.     private void initComponents() {
  338.  
  339.         jLabel1 = new javax.swing.JLabel();
  340.         jLabel2 = new javax.swing.JLabel();
  341.         outName = new javax.swing.JTextField();
  342.         outEmail = new javax.swing.JTextField();
  343.         btUpgrade = new javax.swing.JButton();
  344.         btDelete = new javax.swing.JButton();
  345.  
  346.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  347.  
  348.         jLabel1.setText("Name");
  349.  
  350.         jLabel2.setText("Email");
  351.  
  352.         btUpgrade.setText("Upgrade");
  353.         btUpgrade.addActionListener(new java.awt.event.ActionListener() {
  354.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  355.                 btUpgradeActionPerformed(evt);
  356.             }
  357.         });
  358.  
  359.         btDelete.setText("Delete");
  360.         btDelete.addActionListener(new java.awt.event.ActionListener() {
  361.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  362.                 btDeleteActionPerformed(evt);
  363.             }
  364.         });
  365.  
  366.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  367.         getContentPane().setLayout(layout);
  368.         layout.setHorizontalGroup(
  369.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  370.             .addGroup(layout.createSequentialGroup()
  371.                 .addGap(49, 49, 49)
  372.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  373.                     .addGroup(layout.createSequentialGroup()
  374.                         .addComponent(btUpgrade)
  375.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  376.                         .addComponent(btDelete))
  377.                     .addGroup(layout.createSequentialGroup()
  378.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  379.                             .addComponent(jLabel1)
  380.                             .addComponent(jLabel2))
  381.                         .addGap(35, 35, 35)
  382.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  383.                             .addComponent(outName)
  384.                             .addComponent(outEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))))
  385.                 .addContainerGap(58, Short.MAX_VALUE))
  386.         );
  387.         layout.setVerticalGroup(
  388.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  389.             .addGroup(layout.createSequentialGroup()
  390.                 .addGap(22, 22, 22)
  391.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  392.                     .addComponent(jLabel1)
  393.                     .addComponent(outName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  394.                 .addGap(18, 18, 18)
  395.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  396.                     .addComponent(jLabel2)
  397.                     .addComponent(outEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  398.                 .addGap(18, 18, 18)
  399.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  400.                     .addComponent(btUpgrade)
  401.                     .addComponent(btDelete))
  402.                 .addContainerGap(27, Short.MAX_VALUE))
  403.         );
  404.  
  405.         pack();
  406.     }// </editor-fold>                        
  407.  
  408.     private void btDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                        
  409.         // TODO add your handling code here:
  410.         try {
  411. //            Class.forName(driver);
  412. //            conn = DriverManager.getConnection(url, user, pass);
  413. //            stm = conn.createStatement();
  414.            
  415.             String sql = "DELETE FROM ITTB WHERE name = '"+search+"'";
  416.            
  417.             dbConn = new DbConn(sql);
  418.             dbConn.stm.executeUpdate();
  419.  
  420.         } catch (Exception e) {
  421.             JOptionPane.showMessageDialog(null, "Warning: " + e.getMessage());
  422.         }
  423.         dispose();
  424.     }                                        
  425.  
  426.     private void btUpgradeActionPerformed(java.awt.event.ActionEvent evt) {                                          
  427.         // TODO add your handling code here:
  428.        
  429.         try {
  430. //            Class.forName(driver);
  431. //            conn = DriverManager.getConnection(url, user, pass);
  432. //            stm = conn.createStatement();
  433.            
  434.             String name = outName.getText();
  435.             String email = outEmail.getText();
  436.            
  437.             String sql = "UPDATE ITTB SET name = '"+name+"', email= '"+email+"' "
  438.                      + "WHERE name = '"+search+"'";
  439.            
  440.              dbConn = new DbConn(sql);
  441.              
  442.             dbConn.stm.executeUpdate();
  443.            
  444.  
  445.         } catch (Exception e) {
  446.             JOptionPane.showMessageDialog(null, "Warning: " + e.getMessage());
  447.         }
  448.         dispose();
  449.     }                                        
  450.  
  451.     /**
  452.      * @param args the command line arguments
  453.      */
  454. //    public static void main(String args[]) {
  455. //        /* Set the Nimbus look and feel */
  456. //        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  457. //        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  458. //         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  459. //         */
  460. //        try {
  461. //            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  462. //                if ("Nimbus".equals(info.getName())) {
  463. //                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
  464. //                    break;
  465. //                }
  466. //            }
  467. //        } catch (ClassNotFoundException ex) {
  468. //            java.util.logging.Logger.getLogger(Upgrade.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  469. //        } catch (InstantiationException ex) {
  470. //            java.util.logging.Logger.getLogger(Upgrade.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  471. //        } catch (IllegalAccessException ex) {
  472. //            java.util.logging.Logger.getLogger(Upgrade.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  473. //        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  474. //            java.util.logging.Logger.getLogger(Upgrade.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  475. //        }
  476. //        //</editor-fold>
  477. //
  478. //        /* Create and display the form */
  479. //        java.awt.EventQueue.invokeLater(new Runnable() {
  480. //            public void run() {
  481. //                new Upgrade().setVisible(true);
  482. //            }
  483. //        });
  484. //    }
  485.  
  486.     // Variables declaration - do not modify                    
  487.     private javax.swing.JButton btDelete;
  488.     private javax.swing.JButton btUpgrade;
  489.     private javax.swing.JLabel jLabel1;
  490.     private javax.swing.JLabel jLabel2;
  491.     private javax.swing.JTextField outEmail;
  492.     private javax.swing.JTextField outName;
  493.     // End of variables declaration                  
  494. }
Advertisement
Add Comment
Please, Sign In to add comment