Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package destinatii_turistice;
  2.  
  3. public class Destinatie {
  4. private String tara;
  5. private String statiune;
  6. private int nr_obiective;
  7. public Destinatie(String tara, String statiune, int nr_obiective) {
  8.  
  9. this.tara = tara;
  10. this.statiune = statiune;
  11. this.nr_obiective = nr_obiective;
  12. }
  13. public String getTara() {
  14. return tara;
  15. }
  16. public void setTara(String tara) {
  17. this.tara = tara;
  18. }
  19. public String getStatiune() {
  20. return statiune;
  21. }
  22. public void setStatiune(String statiune) {
  23. this.statiune = statiune;
  24. }
  25. public int getNr_obiective() {
  26. return nr_obiective;
  27. }
  28. public void setNr_obiective(int nr_obiective) {
  29. this.nr_obiective = nr_obiective;
  30. }
  31. @Override
  32. public String toString() {
  33. return "tara=" + tara + ", statiune=" + statiune
  34. + ", nr_obiective=" + nr_obiective + "]";
  35. }
  36.  
  37.  
  38. }
  39. package destinatii_turistice;
  40.  
  41. import java.sql.Connection;
  42. import java.sql.DriverManager;
  43. import java.sql.PreparedStatement;
  44. import java.sql.ResultSet;
  45. import java.sql.SQLException;
  46. import java.sql.Statement;
  47. import java.io.BufferedReader;
  48. import java.io.IOException;
  49. import java.io.InputStreamReader;
  50. public class MainApp {
  51.  
  52. public static void main(String[] args) throws IOException, IllegalAccessException,InstantiationException,SQLException,ClassNotFoundException {
  53. // TODO Auto-generated method stub
  54. String url="jdbc:mysql://localhost:3306/test";
  55. Class.forName("com.mysql.jdbc.Driver").newInstance();
  56. Connection con=DriverManager.getConnection(url,"root","root");
  57.  
  58. BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
  59. int opt;
  60. do
  61. {
  62. System.out.println("1.Afisare in consola a datelor din MySql");
  63. System.out.println("2.Adaugare in tabela a unei destinatiii citite de la consola");
  64. System.out.println("3.Actualizare nr obiective turistice pentru o statiune citita");
  65. System.out.println("Dati optiune : ");
  66. opt=Integer.parseInt(reader.readLine());
  67. switch(opt)
  68. {
  69. case 1:
  70. Statement sql = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  71. ResultSet rs=sql.executeQuery("Select *from destinatii");
  72.  
  73. while(rs.next())
  74. {
  75. System.out.println(rs.getString("tara")+" "+rs.getString("statiune")+" "+rs.getInt("nr_obiective"));
  76. }
  77. System.out.println("Am afisat!!!!");
  78.  
  79. sql.close();
  80. rs.close();
  81. break;
  82. case 2:
  83. Statement sql1 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  84. ResultSet rs1=sql1.executeQuery("Select *from destinatii");
  85.  
  86. String tara1;
  87. String statiune1;
  88. int nr_obiective1;
  89. int index1;
  90. System.out.println("Dati tara de adaugat : ");
  91. tara1=reader.readLine();
  92. System.out.println("Dati statiune de adaugat : ");
  93. statiune1=reader.readLine();
  94. System.out.println("Dati nr de obiective : ");
  95. nr_obiective1=Integer.parseInt(reader.readLine());
  96. /*System.out.println("Dati indexul: ");
  97. index1=Integer.parseInt(reader.readLine());*/
  98. rs1.moveToInsertRow();
  99.  
  100. rs1.updateString("tara", tara1);
  101. rs1.updateString("statiune", statiune1);
  102. rs1.updateInt("nr_obiective",nr_obiective1);
  103. rs1.insertRow();
  104. sql1.close();
  105. rs1.close();
  106. break;
  107.  
  108. case 3:
  109. Statement sql2 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  110.  
  111. String statiune11;
  112. int nr_obiective11;
  113. System.out.println("Dati statiune pentru update : ");
  114. statiune11=reader.readLine();
  115. System.out.println("Dati nr de obiective : ");
  116. nr_obiective11=Integer.parseInt(reader.readLine());
  117. ResultSet rs2=sql2.executeQuery("Select *from destinatii where statiune=\""+statiune11 + "\"");
  118. rs2.first();
  119. rs2.updateInt("nr_obiective", nr_obiective11);
  120. rs2.updateRow();
  121.  
  122.  
  123.  
  124. sql2.close();
  125. rs2.close();
  126. break;
  127. //sterge
  128. case 4:
  129. Statement sql3 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  130.  
  131. String statiune3;
  132. int nr_obiective3;
  133. System.out.println("Dati statiune pentru sters : ");
  134. statiune3=reader.readLine();
  135.  
  136. ResultSet rs3=sql3.executeQuery("Select *from destinatii where statiune=\""+statiune3 + "\"");
  137. rs3.first();
  138. rs3.deleteRow();
  139. sql3.close();
  140. rs3.close();
  141. break;
  142. }
  143.  
  144.  
  145. }while(opt<5);
  146. con.close();
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement