Guest User

Untitled

a guest
Jan 14th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. retrieving values from database in java
  2. import java.sql.*;
  3.  
  4. public class GuestsInfo
  5. {
  6. private String firstName, lastName;
  7. private int age;
  8. private Connection con;
  9. private PreparedStatement statement;
  10.  
  11. public GuestsInfo()
  12. {
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. con = DriverManager.getConnection(
  16. "jdbc:mysql://localhost:3306/3moronsdb","root","");
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. public GuestsInfo(String firstName, String lastName, int age)
  22. {
  23. this();
  24. try
  25. {
  26. statement = con.prepareStatement("Insert into guest(firstName,lastName,age)values(?,?,?)");
  27.  
  28. statement.setString(1, firstName);
  29. statement.setString(2, lastName);
  30. statement.setInt(3, age);
  31.  
  32. statement.executeUpdate();
  33. con.close();
  34. }
  35. catch(Exception e)
  36. {
  37. e.printStackTrace();
  38. return;
  39. }
  40. }
  41.  
  42.  
  43. public void setFirstName(String firstName)
  44. {
  45.  
  46. try{
  47. statement= con.prepareStatement("SELECT * FROM guest WHERE firstName = ?");
  48. statement.setString(1, firstName);
  49. ResultSet rs = statement.executeQuery();
  50. while(rs.next()){
  51. firstName = rs.getString(1);
  52. }
  53. }catch(Exception e){
  54. System.out.print(e);
  55. }
  56. }
  57.  
  58. public String getFirstName(){
  59. return firstName;
  60. }
  61.  
  62. public void setLastName(String lastName)
  63. {
  64.  
  65. try{
  66. statement= con.prepareStatement("SELECT * FROM guest WHERE lastName = ?");
  67. statement.setString(2, lastName);
  68. ResultSet rs = statement.executeQuery();
  69. while(rs.next()){
  70. lastName = rs.getString(2);
  71. }
  72. }catch(Exception e){
  73. System.out.print(e);
  74. }
  75. }
  76.  
  77. public String getLastName(){
  78. return lastName;
  79. }
  80.  
  81. public void setAge(int age){
  82. try{
  83. statement = con.prepareStatement("SELECT * FROM guest WHERE age=?");
  84. statement.setInt(3, age);
  85. ResultSet rs = statement.executeQuery();
  86. while(rs.next()){
  87. age = rs.getInt(3);
  88. }
  89. }catch(Exception e){
  90. System.out.print(e);
  91. }
  92. }
  93.  
  94. public int getAge(){
  95. return age;
  96. }
  97.  
  98. public class TestMain {
  99. public static void main(String [] args){
  100.  
  101. GuestsInfo gi = new GuestsInfo();
  102. gi.setFirstName("Ericka");
  103. System.out.println(gi.getFirstName());
  104.  
  105. }
  106. }
  107.  
  108. public void setFirstName(String firstName)
  109. {
  110.  
  111. try{
  112. // Here you create a READing statement.
  113. statement= con.prepareStatement("SELECT * FROM guest WHERE firstName = ?");
  114. statement.setString(1, firstName);
  115. ResultSet rs = statement.executeQuery();
  116. while(rs.next()){
  117. // Here you set the >>method param<< to a non-existing String
  118. firstName = rs.getString(1);
  119. }
  120. }catch(Exception e){
  121. System.out.print(e);
  122. }
  123. }
  124. // After the method is done you have done nothing because you didn't even set your class member variable "firstName", which you would have set using "this.firstName".
  125.  
  126. GuestsInfo gi = new GuestsInfo("Ericka", "Something", 30);
  127.  
  128. GuestsInfo gi = new GuestsInfo();
  129.  
  130. GuestsInfo gi = new GuestsInfo("Ericka", "lastName", 23);
Add Comment
Please, Sign In to add comment