Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. package agenda;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. public class Agenda {
  16.  
  17.  
  18. public static void main(String[] args) {
  19. System.out.print("wtf");
  20.  
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver").newInstance();
  23. Connection c =
  24. DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseagenda", "root", "");
  25. Statement st = c.createStatement();
  26.  
  27.  
  28.  
  29.  
  30.  
  31. if(args.length >0){
  32. if(args[0].equals("-v")){
  33. if(args.length==2){
  34. //apro file e lo leggo
  35. ResultSet s= st.executeQuery("SELECT * "
  36. + "FROM agenda "
  37. + "WHERE giorno = '"+args[1]+ "' ");//metto più per fargli capire che devo trovare il valore dentro args
  38. while(s.next()){
  39. System.out.print(s.getString(1)+ " "+ s.getString(2)+ " "+ s.getString(3));
  40. }
  41.  
  42. } else{
  43. System.out.print("errore passaggio parametri");
  44. System.exit(-1);
  45. }
  46. } else if(args.equals("-s")){
  47. if(args.length==2){
  48. try {
  49. FileReader f= new FileReader("Agenda.txt");
  50. BufferedReader b= new BufferedReader(f);
  51. String line="";
  52. while((line = b.readLine())!=null){
  53. String [] splitted = line.split(" ");
  54. if(args[1].equals(splitted[2])){
  55. System.out.println(line);
  56.  
  57. }
  58.  
  59. }
  60.  
  61.  
  62. b.close();
  63. f.close();
  64. //if args[1] è contenuto nel file{
  65. // allora restituisci il contenuto di quel giorno}
  66.  
  67. } catch (FileNotFoundException e) {
  68.  
  69. e.printStackTrace();
  70. } catch (IOException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. }
  74.  
  75.  
  76. }else{
  77. System.out.print("errore passaggio parametri");
  78. System.exit(-1);
  79.  
  80. }
  81. }
  82. }
  83. else if(args.equals("-i")){
  84. if(args.length==4){
  85. ResultSet s= st.executeQuery("SELECT * "
  86. + "FROM agenda "
  87. + "WHERE giorno = '"+args[1]+"' AND ora = '"+args[2]+"'");
  88. s.getString(1);
  89. if(s.wasNull()){
  90. st.execute("INSERT INTO agenda(ora,giorno,descrizione) "
  91. + "VALUES ('"+args[2]+"','"+args[1]+"','"+args[3]+"')");
  92. }else{
  93. System.out.println("riga già inserita");
  94. }
  95.  
  96.  
  97.  
  98.  
  99. }else{
  100. System.out.print("errore passaggio parametri");
  101. System.exit(-1);
  102.  
  103. }
  104. }
  105. } catch (InstantiationException e) {
  106. System.out.print("wtf");
  107. e.printStackTrace();
  108. } catch (IllegalAccessException e) {
  109. System.out.print("wtf");
  110. e.printStackTrace();
  111. } catch (ClassNotFoundException e) {
  112. System.out.print("classe non trovata");
  113. e.printStackTrace();
  114. } catch (SQLException e) {
  115. System.out.print("wtf");
  116. e.printStackTrace();
  117. }
  118.  
  119.  
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement