Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. package funclubs;
  2. import java.sql.*;
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. class Funclubs{
  7.  
  8. static {
  9. try {
  10. // register the driver with DriverManager
  11. Class.forName("org.postgresql.Driver").newInstance();
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }
  16.  
  17. public static void main(String argv[]) {
  18. Connection con = null;
  19. try{
  20.  
  21.  
  22. // Connect to DB. URL is jdbc:postgresql:DB
  23. String url = "jdbc:postgresql://127.0.0.1/postgres";
  24. String userid = "postgres";
  25. String passwd = "toto";
  26. int executeUpdate;
  27. con = DriverManager.getConnection(url, userid, passwd);
  28.  
  29. System.out.println("...sono pronto per iniziare. Vai!");
  30.  
  31.  
  32. Statement stmt = con.createStatement();
  33. stmt.executeUpdate("DROP table IF EXISTS mondiali2010.funclubs CASCADE;");
  34. executeUpdate = stmt.executeUpdate("create table mondiali2010.funclubs("
  35. + "nazione varchar(20) primary key,"
  36. + "sponsor varchar not null)");
  37. stmt.executeUpdate("DROP table IF EXISTS mondiali2010.socio CASCADE;");
  38. executeUpdate = stmt.executeUpdate("create table mondiali2010.socio("
  39. + "idsocio int primary key,"
  40. + "nome char(10) not null,"
  41. + "sesso char(1) not null)");
  42. stmt.executeUpdate("DROP table IF EXISTS mondiali2010.iscrizione CASCADE;");
  43. executeUpdate = stmt.executeUpdate("create table mondiali2010.iscrizione("
  44. + "socio int references mondiali2010.socio, "
  45. + "fun_club varchar(20) references mondiali2010.funclubs)");
  46. ResultSet rs;
  47. rs = stmt.executeQuery("select nazione,sponsor from mondiali2010.squadra;");
  48. PreparedStatement pstmt = con.prepareStatement("insert into mondiali2010.funclubs values (?,?);")copypaste;
  49. while(rs.next()){
  50. String n=rs.getString("nazione");
  51. String s=rs.getString("sponsor");
  52. System.out.println(n+s);
  53. pstmt.setString(1, n);
  54. pstmt.setString(2, s);
  55. pstmt.executeUpdate();
  56.  
  57. }
  58. //ResultSet rs=stmt.executeQuery("insert into mondiali2010.funclubs (?,?)");
  59. // Punto 1. Esecuzione query. Da completare.
  60.  
  61. //Punto 2: Stampa risultato query. Da completare.
  62.  
  63. //Chiusura oggetti: Da completare.
  64. System.out.println("...chiudo tutto!");
  65. stmt.close();
  66. con.close();
  67. } catch( Exception e ) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement