Advertisement
Guest User

Untitled

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