Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. /*
  2.  
  3. create user testeo with password 'testeo';
  4.  
  5. alter user testeo with createdb;
  6.  
  7.  
  8. ACBMenu menu = new ACBMenu();
  9.  
  10. @SuppressWarnings("unused")
  11. Connection conn = null;
  12.  
  13. int option;
  14. DBAccessor dbaccessor = new DBAccessor();
  15. dbaccessor.init();
  16.  
  17. conn = dbaccessor.getConnection();
  18. do{
  19. // menu
  20. }
  21.  
  22. // ======================================================0
  23.  
  24. private String dbname;
  25. private String host;
  26. private String port;
  27. private String user;
  28. private String passwd;
  29. private String schema;
  30. Connection conn = null;
  31. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  32.  
  33. // Asi hago el init
  34.  
  35. public void init() {
  36. Properties p = new Properties();
  37. InputStream pStream = this.getClass().getClassLoader().getResourceAsStream("db.properties");
  38. try {
  39. p.load(pStream);
  40. this.dbname = p.getProperty("dbname");
  41. this.host = p.getProperty("host");
  42. this.port = p.getProperty("port");
  43. this.user = p.getProperty("user");
  44. this.passwd = p.getProperty("passwd");
  45. this.schema = p.getProperty("schema");
  46.  
  47. } catch (IOException e) {
  48. String message = "ERROR: db.properties file could not be found";
  49. System.err.println(message);
  50. throw new RuntimeException(message, e);
  51. }
  52. }
  53.  
  54.  
  55. //Asi hago la conexion
  56.  
  57. public Connection getConnection() throws ClassNotFoundException, SQLException{
  58. Class.forName("org.postgresql.Driver");
  59.  
  60. String url = "jdbc:postgresql://"+this.host+":"+this.port+"/"+this.dbname;
  61.  
  62. conn = DriverManager.getConnection(url,this.user,this.passwd);
  63. conn.setAutoCommit(false);
  64.  
  65. conn.createStatement().executeQuery("SET search_path TO " + this.schema);
  66.  
  67.  
  68. //st = conn.createStatement();
  69.  
  70. return conn;
  71. }
  72.  
  73. ===========================================================================
  74.  
  75. public Connection getConnection() throws SQLException {
  76. String url = null;
  77. try {
  78. // Driver
  79. Class.forName("org.postgresql.Driver");
  80. StringBuffer sbUrl = new StringBuffer();
  81. sbUrl.append("jdbc:postgresql:");
  82. if (host != null && !host.equals("")) {
  83. sbUrl.append("//").append(host);
  84. if (port != null && !port.equals("")) {
  85. sbUrl.append(":").append(port);
  86. }
  87. }
  88. sbUrl.append("/").append(dbname);
  89. url = sbUrl.toString();
  90.  
  91. conn = DriverManager.getConnection(url, this.user, this.passwd);
  92. conn.setAutoCommit(false);
  93.  
  94. } catch (ClassNotFoundException e1) {
  95. System.err.println("ERROR: driver fail");
  96. System.err.println(e1.getMessage());
  97. } catch (SQLException e2) {
  98. System.err.println("No conectado);
  99. System.err.println(e2.getMessage());
  100. }
  101.  
  102. if (conn != null) {
  103. Statement statement = null;
  104. try {
  105. statement = conn.createStatement();
  106. statement.executeUpdate("SET search_path TO " + this.schema);
  107. System.out.println("CONECTADO);
  108. System.out.println();
  109.  
  110. } catch (SQLException e) {
  111. System.err.println("ERROR: Unable to set search_path");
  112. System.err.println(e.getMessage());
  113. } finally {
  114. try {
  115. statement.close();
  116. } catch (SQLException e) {
  117. System.err.println("ERROR: Closing statement");
  118. System.err.println(e.getMessage());
  119. }
  120. }
  121. }
  122. return conn;
  123. }
  124.  
  125.  
  126. //Asi hago update en ejecucion (hay que pasarle el conn)
  127.  
  128. Statement st = cn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
  129. ResultSet rs = st.executeQuery("SELECT * FROM patient WHERE pat_number = " + pat_number);
  130. while (rs.next()) {
  131. System.out.println(
  132. rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
  133. System.out.println("Introdueix la nova adreça: ");
  134. String adr = br.readLine();
  135. System.out.println("Introdueix la nova ciutat:");
  136. String city = br.readLine();
  137. rs.updateString("address", adr);
  138. rs.updateString("city", city);
  139. rs.updateRow();
  140.  
  141.  
  142. st.executeUpdate("UPDATE player SET team_name = NULL WHERE federation_license_code LIKE '" + fede + "'");
  143.  
  144.  
  145. //Asi hago PreparedStatement
  146.  
  147.  
  148. String s = "INSERT INTO match_statistics VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  149.  
  150. PreparedStatement ps = conn.prepareStatement(s);
  151. BufferedReader fr = new BufferedReader(new FileReader("src/estadistiques.csv")); String line = null;
  152. fr.readLine();
  153.  
  154. boolean eof = false;
  155. StringTokenizer token;
  156. String output[] = new String[22];
  157. int i = 0;
  158.  
  159. do {
  160. line = fr.readLine();
  161. if (line == null) {
  162. eof = true;
  163. } else {
  164. token = new StringTokenizer(line, ",");
  165. while (token.hasMoreElements()) {
  166. output[i] = token.nextToken();
  167. i = i + 1;
  168. }
  169.  
  170. i = 0;
  171.  
  172. ps.clearParameters();
  173.  
  174. ps.setString(1, output[0]);
  175. ps.setDate(3, java.sql.Date.valueOf(output[2]));
  176.  
  177. ps.executeUpdate();
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement