Guest User

Untitled

a guest
Feb 15th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. package ex3;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7.  
  8. public class Main {
  9. public static void main(String args[]) {
  10. Connection c = null;
  11. Statement statement = null;
  12.  
  13.  
  14. try {//Activitat 5 (connexió a la base de dades)
  15. Class.forName("org.postgresql.Driver");
  16. c = DriverManager
  17. .getConnection("jdbc:postgresql://localhost:5432/escola",
  18. "postgres", "postgres");
  19.  
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. System.err.println(e.getClass().getName()+": "+e.getMessage());
  23. System.exit(0);
  24. }
  25. System.out.println("Base de dades correctament.\n");
  26.  
  27.  
  28. try{//Activitat 5 (mostrar elements taula)
  29. statement = c.createStatement();
  30.  
  31. ResultSet rs = statement.executeQuery("select * from persona;");
  32. while (rs.next()){
  33. String dni = rs.getString("dni");
  34. String nom = rs.getString("nom");
  35. String cognom = rs.getString("cognom");
  36. String carrer = rs.getString("carrer");
  37. String ciutat = rs.getString("ciutat");
  38. int cod_pos = rs.getInt("cod_pos");
  39. int tel = rs.getInt("tel");
  40. System.out.println( "DNI = " + dni );
  41. System.out.println( "NOM = " + nom );
  42. System.out.println( "COGNOM = " + cognom );
  43. System.out.println( "CARRER = " + carrer );
  44. System.out.println( "CIUTAT = " + ciutat );
  45. System.out.println( "CODI POSTAL = " + cod_pos );
  46. System.out.println( "TELÈFON = " + tel );
  47. System.out.println();
  48. }
  49.  
  50. statement.close();
  51. c.close();
  52.  
  53. } catch ( Exception e ) {
  54. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  55. System.exit(0);
  56. }
  57. System.out.println("Consulta feta correctament.\n");
  58.  
  59.  
  60. try{//Exercici 6 (creació nova taula)
  61. statement = c.createStatement();
  62.  
  63. String sql = "create table materials("
  64. + "codi varchar(3),"
  65. + "descripcio varchar(100),"
  66. + "stock float(3),"
  67. + "preu float(6),"
  68. + "primary key (codi));";
  69. statement.executeUpdate(sql);
  70.  
  71. statement.close();
  72. c.close();
  73.  
  74. } catch ( Exception e ) {
  75. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  76. System.exit(0);
  77. }
  78. System.out.println("Taula creada correctament.\n");
  79.  
  80.  
  81. try{//Exercici 7 (insercions)
  82. statement = c.createStatement();
  83.  
  84. String sql = "insert into alumne (dni, nom, cognom, carrer, ciutat, cod_pos, tel, anymatriculacio)"
  85. + "values ('12345678A', 'Ian', 'Talamantes', 'C/ All 5', 'Barcelona', 12345, 123456789, 1);";
  86. statement.executeUpdate(sql);
  87.  
  88. sql = "insert into alumne (dni, nom, cognom, carrer, ciutat, cod_pos, tel, anymatriculacio)"
  89. + "values ('23456789B', 'Jose', 'Fernandez', 'C/ All 5', 'Barcelona', 12345, 234567890, 2);";
  90. statement.executeUpdate(sql);
  91.  
  92. sql = "insert into professor (dni, nom, cognom, carrer, ciutat, cod_pos, tel, departament)"
  93. + "values ('34567890C', 'Tipo', 'de Incognito', 'C/ Pebrot 6', 'Barcelona', 12345, 2345678901, 3);";
  94. statement.executeUpdate(sql);
  95.  
  96. sql = "insert into professor (dni, nom, cognom, carrer, ciutat, cod_pos, tel, departament)"
  97. + "values ('45678901D', 'Cosme', 'Fulanito', 'C/ Pebrot 7', 'Barcelona', 12345, 3456789012, 4);";
  98. statement.executeUpdate(sql);
  99.  
  100. statement.close();
  101. c.close();
  102.  
  103. } catch ( Exception e ) {
  104. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  105. System.exit(0);
  106. }
  107. System.out.println("Insercions creades correctament.\n");
  108.  
  109.  
  110. try{//Exercici 8 (consulta alumne)
  111. statement = c.createStatement();
  112. String sql = "select * from assignatura where idalumne='12345678A';";
  113. statement.executeUpdate(sql);
  114.  
  115. statement.close();
  116. c.close();
  117.  
  118. } catch ( Exception e ) {
  119. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  120. System.exit(0);
  121. }
  122. System.out.println("Consulta feta correctament.\n");
  123.  
  124.  
  125. try{//Exercici 9 (consulta 2 taules)
  126. statement = c.createStatement();
  127.  
  128. ResultSet rs = statement.executeQuery("select * from alumne;");
  129. while (rs.next()){
  130. String dni = rs.getString("dni");
  131. String nom = rs.getString("nom");
  132. String cognom = rs.getString("cognom");
  133. String carrer = rs.getString("carrer");
  134. String ciutat = rs.getString("ciutat");
  135. int cod_pos = rs.getInt("cod_pos");
  136. int tel = rs.getInt("tel");
  137. int anymatriculacio = rs.getInt("anymatriculacio");
  138. System.out.println( "DNI = " + dni );
  139. System.out.println( "NOM = " + nom );
  140. System.out.println( "COGNOM = " + cognom );
  141. System.out.println( "CARRER = " + carrer );
  142. System.out.println( "CIUTAT = " + ciutat );
  143. System.out.println( "CODI POSTAL = " + cod_pos );
  144. System.out.println( "TELÈFON = " + tel );
  145. System.out.println( "ANY MATRICULACIÓ = " + anymatriculacio );
  146. System.out.println();
  147. }
  148.  
  149. rs = statement.executeQuery("select * from professor;");
  150. while (rs.next()){
  151. String dni = rs.getString("dni");
  152. String nom = rs.getString("nom");
  153. String cognom = rs.getString("cognom");
  154. String carrer = rs.getString("carrer");
  155. String ciutat = rs.getString("ciutat");
  156. int cod_pos = rs.getInt("cod_pos");
  157. int tel = rs.getInt("tel");
  158. int departament = rs.getInt("departament");
  159. System.out.println( "DNI = " + dni );
  160. System.out.println( "NOM = " + nom );
  161. System.out.println( "COGNOM = " + cognom );
  162. System.out.println( "CARRER = " + carrer );
  163. System.out.println( "CIUTAT = " + ciutat );
  164. System.out.println( "CODI POSTAL = " + cod_pos );
  165. System.out.println( "TELÈFON = " + tel );
  166. System.out.println( "DEPARTAMENT = " + departament );
  167. System.out.println();
  168. }
  169.  
  170. statement.close();
  171. c.close();
  172.  
  173. } catch ( Exception e ) {
  174. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  175. System.exit(0);
  176. }
  177. System.out.println("Consulta feta correctament.");
  178.  
  179.  
  180. }
  181. }
Add Comment
Please, Sign In to add comment