Guest User

Untitled

a guest
Mar 14th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 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 siddhesh;
  7. import java.sql.*;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12.  
  13.  
  14.  
  15. /**
  16. *
  17. * @author mca
  18. */
  19. public class rich2 extends javax.swing.JFrame {
  20.  
  21. Connection c=null;
  22. PreparedStatement ps=null;
  23. private Component frame;
  24.  
  25. /**
  26. * Creates new form rich2
  27. */
  28. public rich2() {
  29. initComponents();
  30. }
  31.  
  32. /**
  33. * This method is called from within the constructor to initialize the form.
  34. * WARNING: Do NOT modify this code. The content of this method is always
  35. * regenerated by the Form Editor.
  36. */
  37. @SuppressWarnings("unchecked")
  38. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  39. private void initComponents() {
  40.  
  41. l1 = new javax.swing.JLabel();
  42. t1 = new javax.swing.JTextField();
  43. b1 = new javax.swing.JButton();
  44. b2 = new javax.swing.JButton();
  45. jLabel1 = new javax.swing.JLabel();
  46. t2 = new javax.swing.JTextField();
  47.  
  48. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  49.  
  50. l1.setText("Product Name");
  51.  
  52. b1.setText("Add");
  53. b1.addActionListener(new java.awt.event.ActionListener() {
  54. public void actionPerformed(java.awt.event.ActionEvent evt) {
  55. b1ActionPerformed(evt);
  56. }
  57. });
  58.  
  59. b2.setText("Delete");
  60. b2.addActionListener(new java.awt.event.ActionListener() {
  61. public void actionPerformed(java.awt.event.ActionEvent evt) {
  62. b2ActionPerformed(evt);
  63. }
  64. });
  65.  
  66. jLabel1.setText("Price");
  67.  
  68. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  69. getContentPane().setLayout(layout);
  70. layout.setHorizontalGroup(
  71. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  72. .addGroup(layout.createSequentialGroup()
  73. .addGap(46, 46, 46)
  74. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  75. .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  76. .addComponent(b1)
  77. .addComponent(l1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  78. .addGap(95, 95, 95)
  79. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  80. .addComponent(b2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  81. .addComponent(t1)
  82. .addComponent(t2))
  83. .addContainerGap(129, Short.MAX_VALUE))
  84. );
  85. layout.setVerticalGroup(
  86. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  87. .addGroup(layout.createSequentialGroup()
  88. .addGap(41, 41, 41)
  89. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  90. .addComponent(l1)
  91. .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  92. .addGap(18, 18, 18)
  93. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  94. .addComponent(jLabel1)
  95. .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  96. .addGap(33, 33, 33)
  97. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  98. .addComponent(b1)
  99. .addComponent(b2))
  100. .addContainerGap(145, Short.MAX_VALUE))
  101. );
  102.  
  103. pack();
  104. }// </editor-fold>
  105.  
  106. private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
  107. // TODO add your handling code here:
  108.  
  109.  
  110. String s1=t1.getText();
  111. String s3=t2.getText();
  112. try{
  113. Class.forName("com.mysql.jdbc.Driver");
  114. c=DriverManager.getConnection("jdbc:mysql://localhost:3306/fattu","root","");
  115. String q="insert into akshay values (?,?)";
  116.  
  117. ps=c.prepareStatement(q);
  118. ps.setString(1, s1);
  119. ps.setString(2, s3);
  120. ps.execute();
  121. c.close();
  122. JOptionPane.showMessageDialog(frame, "Data inserted");
  123.  
  124. }
  125. catch(Exception e)
  126. {
  127. JOptionPane.showMessageDialog(null,e);
  128. }
  129.  
  130.  
  131. }
  132.  
  133. private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
  134. // TODO add your handling code here:
  135. String s2=t1.getText();
  136. try{
  137. Class.forName("com.mysql.jdbc.Driver");
  138. c=DriverManager.getConnection("jdbc:mysql://localhost:3306/fattu","root","");
  139. String q="delete from akshay where pname = ?";
  140.  
  141. ps=c.prepareStatement(q);
  142. ps.setString(1, s2);
  143. ps.execute();
  144. c.close();
  145. JOptionPane.showMessageDialog(frame, "Data deleted");
  146.  
  147. }
  148. catch(Exception e)
  149. {
  150. JOptionPane.showMessageDialog(null,e);
  151. }
  152. }
  153.  
  154. /**
  155. * @param args the command line arguments
  156. */
  157. public static void main(String args[]) {
  158. /* Set the Nimbus look and feel */
  159. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  160. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  161. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  162. */
  163. try {
  164. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  165. if ("Nimbus".equals(info.getName())) {
  166. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  167. break;
  168. }
  169. }
  170. } catch (ClassNotFoundException ex) {
  171. java.util.logging.Logger.getLogger(rich2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  172. } catch (InstantiationException ex) {
  173. java.util.logging.Logger.getLogger(rich2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  174. } catch (IllegalAccessException ex) {
  175. java.util.logging.Logger.getLogger(rich2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  176. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  177. java.util.logging.Logger.getLogger(rich2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  178. }
  179. //</editor-fold>
  180.  
  181. /* Create and display the form */
  182. java.awt.EventQueue.invokeLater(new Runnable() {
  183. public void run() {
  184. new rich2().setVisible(true);
  185. }
  186. });
  187. }
  188.  
  189. // Variables declaration - do not modify
  190. private javax.swing.JButton b1;
  191. private javax.swing.JButton b2;
  192. private javax.swing.JLabel jLabel1;
  193. private javax.swing.JLabel l1;
  194. private javax.swing.JTextField t1;
  195. private javax.swing.JTextField t2;
  196. // End of variables declaration
  197. }
Add Comment
Please, Sign In to add comment