Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class appli{
  5. public static void main(String args[]){
  6. try{
  7. //step1 load the driver class
  8. Class.forName("oracle.jdbc.OracleDriver");
  9.  
  10. //step2 create the connection object
  11. Connection con=DriverManager.getConnection(
  12. "jdbc:oracle:thin:@localhost:1521:XE","ETUDIANT","ETUDIANT");
  13.  
  14. //step3 create the statement object
  15. Statement stmt=con.createStatement();
  16.  
  17. //step4 execute query
  18. //question 1
  19. ResultSet rs1=stmt.executeQuery("select * from avion");
  20. while(rs1.next())
  21. System.out.println(rs1.getInt(1)+" "+rs1.getString(2)+" "+rs1.getString(3));
  22.  
  23.  
  24. //question 2
  25. String s;
  26. Scanner sc1 = new Scanner(System.in);
  27. System.out.println("Entrez nom de la ville : ");
  28. s = sc1.nextLine();
  29. String request2;
  30. request2 =" select * from avion where Entrepot = '" + s + "'";
  31.  
  32. ResultSet rs2=stmt.executeQuery(request2);
  33. while(rs2.next())
  34. System.out.println(rs2.getInt(1)+" ");
  35.  
  36.  
  37. //question 3
  38. PreparedStatement rs3=con.prepareStatement("select * from avion where Entrepot = ?");
  39. Scanner sc2 = new Scanner(System.in);
  40. System.out.println("Entrez nom de la ville : ");
  41. s = sc2.nextLine();
  42. rs3.setString(1,s);//1 specifies the first parameter in the query .
  43.  
  44. rs3.executeUpdate();
  45.  
  46.  
  47. //step5 close the connection object
  48. con.close();
  49.  
  50.  
  51. }catch(Exception e){ System.out.println(e);}
  52.  
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement