Guest User

db

a guest
Oct 20th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. /*
  4. <applet code="DbDemo.class" height="500" width="500">
  5. </applet>
  6. */
  7. public class DbDemo extends Applet implements ActionListner
  8. {
  9. Label lblname,lblroll;
  10. Button ins,update,del,search;
  11. TextField Txtnm,Txtroll;
  12. public void init()
  13. {
  14. lblname=new Label("enter name");
  15. lblroll=new Label("enter roll");
  16.  
  17.  
  18. ins=new Button("insert");
  19. update=new Button("update");
  20. del=new Button("delete");
  21. search=new Button("search");
  22. Txtnm=new TextField();
  23. txtroll=new TextField();
  24. add(lblname);
  25. add(Txtnm);
  26. add(lblroll);
  27. add(Txtroll);
  28. add(ins);
  29. add(update);
  30. add(del);
  31. add(search);
  32. ins.add ActionListner(this);
  33. update.add ActionListner(this);
  34. del.add ActionListner(this);
  35. search.add ActionListner(this);
  36. }
  37. public void actionPerformed(ActionEvent ae)
  38. {
  39. String lbl=ae.getActionCommand();
  40. String n=Txtnm.getText();
  41. String r=Txtroll.getText();
  42. if(lbl.equals("insert"))
  43. {
  44.  
  45. DbMain d=new Dbmain();
  46. d.insert(n,r);
  47.  
  48.  
  49. }
  50. if(lbl.equals("update"))
  51. {
  52. DbMain d=new Dbmain();
  53. d.update(n,r);
  54.  
  55.  
  56. }
  57. if(lbl.equals("delete"))
  58. {
  59. DbMain d=new Dbmain();
  60. d.delete(r);
  61.  
  62.  
  63. }
  64. if(lbl.equals("search"))
  65. {
  66. DbMain d=new Dbmain();
  67. d.search();
  68.  
  69.  
  70. }
  71. }
  72. }
  73. class DbMain
  74. {
  75. DbMain(){
  76. try
  77. {
  78.  
  79. Class.forName("com.mysql.jdbc.Driver");
  80. Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/stud?zeroDateTimeBehavior=convertToNull", "Stud",
  81.  
  82. "");
  83. Statement stmt=conn.createStatement();
  84. String query="select * from Stud";
  85. ResultSet rs=stmt.executeQuery(query);
  86.  
  87.  
  88. }
  89. catch(SQLException e1)
  90. {
  91. System.out.println("Error"+e1);
  92. }
  93. catch(ClassNotFoundException e1)
  94. {
  95. System.out.println("Error"+e1);
  96. }
  97.  
  98. }
  99. public void search()
  100. {
  101. String query="select * from Stud";
  102. try
  103. {
  104. rs=stmt.executeQuery(query);
  105. while(rs.next())
  106.  
  107. {
  108. int r=rs.getInt("Roll");
  109. String n=rs.getString("Name");
  110. System.out.println("Name="+n+"\t"+"Roll ="+r);
  111.  
  112. }
  113.  
  114. }
  115. catch(Exception e1)
  116. {
  117. System.out.println("Error"+e1);
  118. }
  119. }
  120. public void insert(int r,string n)
  121. {
  122. try
  123. {
  124. String q="insert into Stud values("+r+",'"+n+";)";
  125. int cnt=stmt.executeUpdate(q);
  126. if(cnt>0)
  127. { System.out.println("inserted");}
  128. else
  129. {System.out.println("not inserted");}
  130. }
  131. catch(Exception e)
  132. {System.out.println("error:"+e);}
  133. }
  134. public void delete(int r)
  135. {
  136. try
  137. {
  138. String q="delete from Stud where roll="+r+"";
  139. int cnt=stmt.executeUpdate(q);
  140. if(cnt>0)
  141. { System.out.println("deleted");}
  142. else
  143. {System.out.println("not not deleted");}
  144. }
  145. catch(Exception e)
  146. {System.out.println("error:"+e);}
  147. }
  148. public void update(int r,String n)
  149. {
  150. try
  151. {
  152. String q="update Stud where set name='"+n+"' where roll="+r+"";
  153. int cnt=stmt.executeQuery(q);
  154. if(cnt>0)
  155. { System.out.println("updated");}
  156. else
  157. {System.out.println("not updated");}
  158. }
  159. catch(Exception e)
  160. {System.out.println("error:"+e);}
  161. }
  162. }
Add Comment
Please, Sign In to add comment