Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. public class MainApp {
  12.  
  13. public static void main(String[] args) throws IOException, IllegalAccessException,InstantiationException,SQLException,ClassNotFoundException {
  14. // TODO Auto-generated method stub
  15. String url="jdbc:mysql://localhost:3306/test";
  16. Class.forName("com.mysql.jdbc.Driver").newInstance();
  17. Connection con=DriverManager.getConnection(url,"root","root");
  18.  
  19. BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
  20. int opt;
  21. do
  22. {
  23. System.out.println("1.Afisare in consola a datelor din MySql");
  24. System.out.println("2.Adaugare in tabela a unei destinatiii citite de la consola");
  25. System.out.println("3.Actualizare nr obiective turistice pentru o statiune citita");
  26. System.out.println("Dati optiune : ");
  27. opt=Integer.parseInt(reader.readLine());
  28. switch(opt)
  29. {
  30. case 1:
  31. Statement sql = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  32. ResultSet rs=sql.executeQuery("Select *from destinatii");
  33.  
  34. while(rs.next())
  35. {
  36. System.out.println(rs.getString("tara")+" "+rs.getString("statiune")+" "+rs.getInt("nr_obiective"));
  37. }
  38. System.out.println("Am afisat!!!!");
  39.  
  40. sql.close();
  41. rs.close();
  42. break;
  43. case 2:
  44. Statement sql1 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  45. ResultSet rs1=sql1.executeQuery("Select *from destinatii");
  46.  
  47. String tara1;
  48. String statiune1;
  49. int nr_obiective1;
  50. int index1;
  51. System.out.println("Dati tara de adaugat : ");
  52. tara1=reader.readLine();
  53. System.out.println("Dati statiune de adaugat : ");
  54. statiune1=reader.readLine();
  55. System.out.println("Dati nr de obiective : ");
  56. nr_obiective1=Integer.parseInt(reader.readLine());
  57. /*System.out.println("Dati indexul: ");
  58. index1=Integer.parseInt(reader.readLine());*/
  59. rs1.moveToInsertRow();
  60.  
  61. rs1.updateString("tara", tara1);
  62. rs1.updateString("statiune", statiune1);
  63. rs1.updateInt("nr_obiective",nr_obiective1);
  64. rs1.insertRow();
  65. sql1.close();
  66. rs1.close();
  67. break;
  68.  
  69. case 3:
  70. Statement sql2 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  71.  
  72. String statiune11;
  73. int nr_obiective11;
  74. System.out.println("Dati statiune pentru update : ");
  75. statiune11=reader.readLine();
  76. System.out.println("Dati nr de obiective : ");
  77. nr_obiective11=Integer.parseInt(reader.readLine());
  78. ResultSet rs2=sql2.executeQuery("Select *from destinatii where statiune=\""+statiune11 + "\"");
  79. rs2.first();
  80. rs2.updateInt("nr_obiective", nr_obiective11);
  81. rs2.updateRow();
  82.  
  83.  
  84.  
  85. sql2.close();
  86. rs2.close();
  87. break;
  88. }
  89. }while(opt<4);
  90. con.close();
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement