Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package sample25;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- /**
- *
- * @author ONEYES
- */
- public class NewApplication extends javax.swing.JFrame {
- /**
- * Creates new form NewApplication
- */
- public NewApplication() {
- 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() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- txtname = new javax.swing.JTextField();
- txtclass = new javax.swing.JTextField();
- bsave = new javax.swing.JButton();
- bcancel = new javax.swing.JButton();
- update = new javax.swing.JButton();
- delete1 = new javax.swing.JButton();
- menuBar = new javax.swing.JMenuBar();
- fileMenu = new javax.swing.JMenu();
- openMenuItem = new javax.swing.JMenuItem();
- saveMenuItem = new javax.swing.JMenuItem();
- saveAsMenuItem = new javax.swing.JMenuItem();
- exitMenuItem = new javax.swing.JMenuItem();
- editMenu = new javax.swing.JMenu();
- cutMenuItem = new javax.swing.JMenuItem();
- copyMenuItem = new javax.swing.JMenuItem();
- pasteMenuItem = new javax.swing.JMenuItem();
- deleteMenuItem = new javax.swing.JMenuItem();
- helpMenu = new javax.swing.JMenu();
- contentsMenuItem = new javax.swing.JMenuItem();
- aboutMenuItem = new javax.swing.JMenuItem();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setText("Name");
- jLabel2.setText("Class");
- txtname.setText(" ");
- bsave.setText("Save");
- bsave.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bsaveActionPerformed(evt);
- }
- });
- bcancel.setText("Cancel");
- bcancel.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- bcancelActionPerformed(evt);
- }
- });
- update.setText("update");
- update.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- updateActionPerformed(evt);
- }
- });
- delete1.setText("delete");
- delete1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- delete1ActionPerformed(evt);
- }
- });
- fileMenu.setMnemonic('f');
- fileMenu.setText("File");
- openMenuItem.setMnemonic('o');
- openMenuItem.setText("Open");
- fileMenu.add(openMenuItem);
- saveMenuItem.setMnemonic('s');
- saveMenuItem.setText("Save");
- fileMenu.add(saveMenuItem);
- saveAsMenuItem.setMnemonic('a');
- saveAsMenuItem.setText("Save As ...");
- saveAsMenuItem.setDisplayedMnemonicIndex(5);
- fileMenu.add(saveAsMenuItem);
- exitMenuItem.setMnemonic('x');
- exitMenuItem.setText("Exit");
- exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- exitMenuItemActionPerformed(evt);
- }
- });
- fileMenu.add(exitMenuItem);
- menuBar.add(fileMenu);
- editMenu.setMnemonic('e');
- editMenu.setText("Edit");
- cutMenuItem.setMnemonic('t');
- cutMenuItem.setText("Cut");
- editMenu.add(cutMenuItem);
- copyMenuItem.setMnemonic('y');
- copyMenuItem.setText("Copy");
- editMenu.add(copyMenuItem);
- pasteMenuItem.setMnemonic('p');
- pasteMenuItem.setText("Paste");
- editMenu.add(pasteMenuItem);
- deleteMenuItem.setMnemonic('d');
- deleteMenuItem.setText("Delete");
- editMenu.add(deleteMenuItem);
- menuBar.add(editMenu);
- helpMenu.setMnemonic('h');
- helpMenu.setText("Help");
- contentsMenuItem.setMnemonic('c');
- contentsMenuItem.setText("Contents");
- helpMenu.add(contentsMenuItem);
- aboutMenuItem.setMnemonic('a');
- aboutMenuItem.setText("About");
- helpMenu.add(aboutMenuItem);
- menuBar.add(helpMenu);
- setJMenuBar(menuBar);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(74, 74, 74)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jLabel2)
- .addComponent(jLabel1))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(txtname, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
- .addComponent(txtclass))
- .addGap(109, 109, 109))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(delete1)
- .addGroup(layout.createSequentialGroup()
- .addComponent(bsave)
- .addGap(62, 62, 62)
- .addComponent(bcancel)))
- .addGap(30, 30, 30)
- .addComponent(update)
- .addContainerGap(45, Short.MAX_VALUE))))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(70, 70, 70)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(47, 47, 47)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(txtclass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(50, 50, 50)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bsave)
- .addComponent(bcancel)
- .addComponent(update))
- .addGap(18, 18, 18)
- .addComponent(delete1)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- private void bsaveActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- // TODO add your handling code here:
- String s=txtname.getText();
- String r= txtclass.getText();
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift","root","admin");
- String query ="insert into gift1 VALUES(?,?)";
- PreparedStatement pst=con.prepareStatement(query);
- pst.setString(1,s);
- pst.setString(2,r);
- int count=pst.executeUpdate();
- if(count>0)
- {
- dialog("saved successfully");
- }
- con.close();
- clear();
- } catch (SQLException ex) {
- Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void dialog(String m)
- {
- JOptionPane.showMessageDialog(rootPane, m);
- }
- void clear(){
- txtname.setText(" ");
- txtclass.setText(" ");
- }
- private void bcancelActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- clear();
- }
- private void updateActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- String id1= txtname.getText();
- String nam=txtclass.getText();
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift","root","admin");
- String query ="update gift1 set nam=? where id1=?";
- PreparedStatement pst=con.prepareStatement(query);
- pst.setString(1,nam);
- pst.setString(2,id1);
- int count=pst.executeUpdate();
- if(count>0)
- {
- dialog("saved successfully");
- }
- con.close();
- // TODO add your handling code here:
- } catch (SQLException ex) {
- Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void delete1ActionPerformed(java.awt.event.ActionEvent evt) {
- String id1= txtname.getText();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift","root","admin");
- String query ="delete from gift1 where id1=?";
- PreparedStatement pst=con.prepareStatement(query);
- pst.setString(1,id1);
- int count=pst.executeUpdate();
- if(count>0)
- {
- dialog("saved successfully");
- }
- con.close();
- // TODO add your handling code here:
- } catch (SQLException ex) {
- Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(NewApplication.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(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewApplication.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 NewApplication().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JMenuItem aboutMenuItem;
- private javax.swing.JButton bcancel;
- private javax.swing.JButton bsave;
- private javax.swing.JMenuItem contentsMenuItem;
- private javax.swing.JMenuItem copyMenuItem;
- private javax.swing.JMenuItem cutMenuItem;
- private javax.swing.JButton delete1;
- private javax.swing.JMenuItem deleteMenuItem;
- private javax.swing.JMenu editMenu;
- private javax.swing.JMenuItem exitMenuItem;
- private javax.swing.JMenu fileMenu;
- private javax.swing.JMenu helpMenu;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JMenuBar menuBar;
- private javax.swing.JMenuItem openMenuItem;
- private javax.swing.JMenuItem pasteMenuItem;
- private javax.swing.JMenuItem saveAsMenuItem;
- private javax.swing.JMenuItem saveMenuItem;
- private javax.swing.JTextField txtclass;
- private javax.swing.JTextField txtname;
- private javax.swing.JButton update;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment