Guest User

Untitled

a guest
Sep 2nd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. how to use prepared statement
  2. try
  3. {
  4. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  5. System.out.println("n Driver loaded");
  6. Connection con=DriverManager.getConnection("jdbc:odbc:wanisamajDB");
  7. Statement stmt=con.createStatement();
  8. System.out.println("statement is created");
  9. // System.out.println(Integer.parseInt(cbregn.getSelectedItem().toString()));
  10. String qry= " UPDATE Registration1 set RegistrationNo = '"+cbregn.getSelectedItem()+"',SeniorPerson = '"+cbnm.getSelectedItem()+"', NativePlace = '"+tfplace.getText()+"',Kul = '"+tfkul.getText()+"', Gotra = '"+tfgotra.getText()+"' ,KulSwami = '"+tfswami.getText()+"', ResidensialAddress = '"+taraddr.getText()+"' , PinCode = '"+tfpcd.getText()+"', STDcode = '"+tfstdcode.getText()+"',TelephoneNo = '"+tftele.getText()+"', MobileNo = '"+tfmno.getText()+"', Email = '"+tfemail.getText()+"',Website ='"+tfweb.getText()+"',Education ='"+tfedu.getText()+"',Branch ='"+tfbrch.getText()+"',BloodGroup ='"+cbbldgrp.getSelectedItem()+"' where SeniorPerson='" +cbnm.getSelectedItem().toString()+"'" ;
  11. // String qry= " UPDATE Registration1 set SeniorPerson = '"+cbnm.getSelectedItem().toString()+"',NativePlace = '"+tfplace.getText()+"', Kul = '"+tfkul.getText()+"',Gotra = '"+tfgotra.getText()+"' , KulSwami = '"+tfswami.getText()+"', ResidensialAddress = '"+taraddr.getText()+"' , PinCode = '"+Integer.parseInt(tfpcd.getText())+"', STDcode = '"+Integer.parseInt(tfstdcode.getText())+"',TelephoneNo = '"+Integer.parseInt(tftele.getText())+"',MobileNo = '"+Integer.parseInt(tfmno.getText())+"',Email = '"+tfemail.getText()+"',Website ='"+tfweb.getText()+"',Education ='"+tfedu.getText()+"',Branch ='"+tfbrch.getText()+"',BloodGroup ='"+cbbldgrp.getSelectedItem().toString()+"' where RegistrationNo='" +Integer.parseInt(cbregn.getSelectedItem().toString())+"'" ;
  12. stmt.executeUpdate(qry);
  13. JOptionPane.showMessageDialog(null,"RECORD IS UPDATED SUCCESSFULLY ");
  14. System.out.println("QURY");
  15.  
  16. // cbregn.setEditable(false);
  17. cbnm.setEditable(false);
  18. tfplace.setEditable(false);
  19. tfkul.setEditable(false);
  20. tfgotra.setEditable(false);
  21. tfswami.setEditable(false);
  22. taraddr.setEditable(false);
  23. tfpcd.setEditable(false);
  24. tfstdcode.setEditable(false);
  25. tftele.setEditable(false);
  26. tfmno.setEditable(false);
  27. tfemail.setEditable(false);
  28. tfweb.setEditable(false);
  29. tfedu.setEditable(false);
  30. tfbrch.setEditable(false);
  31. cbbldgrp.setEditable(false);
  32. con.close();
  33. stmt.close();
  34. }
  35. // catch(SQLException eM)
  36. // {
  37. // JOptionPane.showMessageDialog(null,"RECORD IS NOT FOUND ");
  38. // }
  39. catch(Exception et)
  40. {
  41. et.printStackTrace();
  42. // System.out.println("error:"+et.getMessage());
  43. }
  44.  
  45. String qry= " UPDATE Registration1 set RegistrationNo = '"+cbregn.getSelectedItem()+"',SeniorPerson = '"+cbnm.getSelectedItem()+"', NativePlace = '"+tfplace.getText()+"',Kul = '"+tfkul.getText()+"', Gotra = '"+tfgotra.getText()+"' ,KulSwami = '"+tfswami.getText()+"', ResidensialAddress = '"+taraddr.getText()+"' , PinCode = '"+tfpcd.getText()+"', STDcode = '"+tfstdcode.getText()+"',TelephoneNo = '"+tftele.getText()+"', MobileNo = '"+tfmno.getText()+"', Email = '"+tfemail.getText()+"',Website ='"+tfweb.getText()+"',Education ='"+tfedu.getText()+"',Branch ='"+tfbrch.getText()+"',BloodGroup ='"+cbbldgrp.getSelectedItem()+"' where SeniorPerson='" +cbnm.getSelectedItem().toString()+"'" ;
  46. stmt.executeUpdate(qry);
  47.  
  48. String qry= " UPDATE Registration1 set RegistrationNo = ?,SeniorPerson = ?, NativePlace = ?,Kul = ?, Gotra = ?,KulSwami = ?, ResidensialAddress = ?, PinCode = ?, STDcode = ?,TelephoneNo = ?, MobileNo = ?, Email = ?,Website =?,Education =?,Branch =?,BloodGroup =? where SeniorPerson=?" ;
  49.  
  50. PreparedStatement updateQry = con.prepareStatement(qry);
  51. updateQry.setString(1,cbregn.getSelectedItem());
  52. updateQry.setString(2,cbnm.getSelectedItem());
  53. updateQry.setString(3,tfplace.getText());
  54. updateQry.setString(4,tfkul.getText());
  55. updateQry.setString(5,tfgotra.getText());
  56. updateQry.setString(6,tfswami.getText());
  57. updateQry.setString(7,taraddr.getText());
  58. updateQry.setString(8,tfpcd.getText());
  59. updateQry.setString(9,tfstdcode.getText());
  60. updateQry.setString(10,tftele.getText());
  61. updateQry.setString(11,tfmno.getText());
  62. updateQry.setString(12,tfemail.getText());
  63. updateQry.setString(13,tfweb.getText());
  64. updateQry.setString(14,tfedu.getText());
  65. updateQry.setString(15,tfbrch.getText());
  66. updateQry.setString(16,cbbldgrp.getSelectedItem());
  67. updateQry.setString(17,cbnm.getSelectedItem().toString());
  68. updateQry.executeUpdate():
  69.  
  70. public class UpdatesRecords{
  71. public static void main(String[] args) {
  72. System.out.println("Updates Records Example through Prepared Statement!");
  73. Connection con = null;
  74. try{
  75. Class.forName("com.mysql.jdbc.Driver");
  76. con = DriverManager.getConnection(
  77. "jdbc:mysql://localhost:3306/jdbctutorial","root","root");
  78. try{
  79. String sql = "UPDATE movies SET title = ? WHERE year_made = ?";
  80. PreparedStatement prest = con.prepareStatement(sql);
  81. prest.setString(1,"Sanam We wafafa");
  82. prest.setInt(2,2005);
  83. prest.executeUpdate();
  84. System.out.println("Updating Successfully!");
  85. con.close();
  86. }
  87. catch (SQLException s){
  88. System.out.println("SQL statement is not executed!");
  89. }
  90. }
  91. catch (Exception e){
  92. e.printStackTrace();
  93. }
  94. }
  95. }
Add Comment
Please, Sign In to add comment