Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- create user testeo with password 'testeo';
- alter user testeo with createdb;
- ACBMenu menu = new ACBMenu();
- @SuppressWarnings("unused")
- Connection conn = null;
- int option;
- DBAccessor dbaccessor = new DBAccessor();
- dbaccessor.init();
- conn = dbaccessor.getConnection();
- do{
- // menu
- }
- // ======================================================0
- private String dbname;
- private String host;
- private String port;
- private String user;
- private String passwd;
- private String schema;
- Connection conn = null;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- // Asi hago el init
- public void init() {
- Properties p = new Properties();
- InputStream pStream = this.getClass().getClassLoader().getResourceAsStream("db.properties");
- try {
- p.load(pStream);
- this.dbname = p.getProperty("dbname");
- this.host = p.getProperty("host");
- this.port = p.getProperty("port");
- this.user = p.getProperty("user");
- this.passwd = p.getProperty("passwd");
- this.schema = p.getProperty("schema");
- } catch (IOException e) {
- String message = "ERROR: db.properties file could not be found";
- System.err.println(message);
- throw new RuntimeException(message, e);
- }
- }
- //Asi hago la conexion
- public Connection getConnection() throws ClassNotFoundException, SQLException{
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql://"+this.host+":"+this.port+"/"+this.dbname;
- conn = DriverManager.getConnection(url,this.user,this.passwd);
- conn.setAutoCommit(false);
- conn.createStatement().executeQuery("SET search_path TO " + this.schema);
- //st = conn.createStatement();
- return conn;
- }
- ===========================================================================
- public Connection getConnection() throws SQLException {
- String url = null;
- try {
- // Driver
- Class.forName("org.postgresql.Driver");
- StringBuffer sbUrl = new StringBuffer();
- sbUrl.append("jdbc:postgresql:");
- if (host != null && !host.equals("")) {
- sbUrl.append("//").append(host);
- if (port != null && !port.equals("")) {
- sbUrl.append(":").append(port);
- }
- }
- sbUrl.append("/").append(dbname);
- url = sbUrl.toString();
- conn = DriverManager.getConnection(url, this.user, this.passwd);
- conn.setAutoCommit(false);
- } catch (ClassNotFoundException e1) {
- System.err.println("ERROR: driver fail");
- System.err.println(e1.getMessage());
- } catch (SQLException e2) {
- System.err.println("No conectado);
- System.err.println(e2.getMessage());
- }
- if (conn != null) {
- Statement statement = null;
- try {
- statement = conn.createStatement();
- statement.executeUpdate("SET search_path TO " + this.schema);
- System.out.println("CONECTADO);
- System.out.println();
- } catch (SQLException e) {
- System.err.println("ERROR: Unable to set search_path");
- System.err.println(e.getMessage());
- } finally {
- try {
- statement.close();
- } catch (SQLException e) {
- System.err.println("ERROR: Closing statement");
- System.err.println(e.getMessage());
- }
- }
- }
- return conn;
- }
- //Asi hago update en ejecucion (hay que pasarle el conn)
- Statement st = cn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
- ResultSet rs = st.executeQuery("SELECT * FROM patient WHERE pat_number = " + pat_number);
- while (rs.next()) {
- System.out.println(
- rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
- System.out.println("Introdueix la nova adreça: ");
- String adr = br.readLine();
- System.out.println("Introdueix la nova ciutat:");
- String city = br.readLine();
- rs.updateString("address", adr);
- rs.updateString("city", city);
- rs.updateRow();
- st.executeUpdate("UPDATE player SET team_name = NULL WHERE federation_license_code LIKE '" + fede + "'");
- //Asi hago PreparedStatement
- String s = "INSERT INTO match_statistics VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
- PreparedStatement ps = conn.prepareStatement(s);
- BufferedReader fr = new BufferedReader(new FileReader("src/estadistiques.csv")); String line = null;
- fr.readLine();
- boolean eof = false;
- StringTokenizer token;
- String output[] = new String[22];
- int i = 0;
- do {
- line = fr.readLine();
- if (line == null) {
- eof = true;
- } else {
- token = new StringTokenizer(line, ",");
- while (token.hasMoreElements()) {
- output[i] = token.nextToken();
- i = i + 1;
- }
- i = 0;
- ps.clearParameters();
- ps.setString(1, output[0]);
- ps.setDate(3, java.sql.Date.valueOf(output[2]));
- ps.executeUpdate();
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement