Advertisement
Guest User

Untitled

a guest
Jan 13th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.sql.*;
  6.  
  7. public class Main {
  8.  
  9.     private final String url ="jdbc:postgresql://www.studenti.famnit.upr.si/banana_melone";
  10.     private final String user = "goran_tubic";
  11.     private final String password = "12qw34er";
  12.    
  13.     public Connection connect() throws SQLException{
  14.         return DriverManager.getConnection(this.url,this.user,this.password);
  15.     }
  16.    
  17.     public void izpisiMesta(ResultSet rs){
  18.         try {
  19.             while(rs.next()){
  20.                 System.out.println(rs.getInt("id") + ". " +rs.getString("name") + " s populacijo: " + rs.getString("population"));
  21.             }
  22.         } catch (SQLException e) {
  23.             // TODO Auto-generated catch block
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.    
  28.     public void slovenskaMesta(){
  29.         String query = "SELECT id, name, population FROM CITY WHERE countryCode =?;";
  30.         try {
  31.             Connection connection = connect();
  32.             PreparedStatement statement = connection.prepareStatement(query);
  33.             statement.setString(1, "SVN"); //zaradi injections
  34.             ResultSet rs = statement.executeQuery();
  35. //          rs.next(); //gres na prvo vrstico z rezultati
  36.             izpisiMesta(rs);
  37.             connection.close();
  38.         } catch (SQLException e) {
  39.             System.out.println(e.getMessage());
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.    
  44.     public void dodajMesta() throws IOException, FileNotFoundException{
  45.         //create sequence city_id_seq start 4079
  46.         //alter table city alter id set default nextval('city_id_seq'); //nastavi field na auto increment oz na 'city_id_seq'
  47.         try {
  48.             Connection connection = connect();
  49.             BufferedReader br = new BufferedReader(new FileReader("mesta.txt"));
  50.             String sql = "INSERT INTO city(name,population,district, countryCode) Values(?,?,?,'SVN')";
  51.             PreparedStatement statement = connection.prepareStatement(sql);
  52.             while(true){
  53.                 String vrstica = br.readLine();
  54.                 if(vrstica == null) break;
  55.                 String [] tok = vrstica.trim().split(",");
  56.                 statement.setString(1, tok[0]);
  57.                 statement.setInt(2, Integer.parseInt(tok[1]));
  58.                 statement.setString(3, tok[2]);
  59.                 statement.addBatch();          
  60.             }
  61.             statement.execute();
  62.             br.close();
  63.         } catch (SQLException e) {
  64.             e.printStackTrace();
  65.         }
  66.        
  67.     }
  68.    
  69.    
  70.     public int numberOfCities(){
  71.         String query = "SELECT COUNT(*) FROM CITY;";
  72.         int count = 0;
  73.         try {
  74.             Connection connection = connect();
  75.             Statement statement = connection.createStatement();
  76.             ResultSet rs = statement.executeQuery(query);
  77.             rs.next(); //gres na prvo vrstico z rezultati
  78.             count = rs.getInt(1);
  79.             connection.close();
  80.             return count;
  81.         } catch (SQLException e) {
  82.             System.out.println(e.getMessage());
  83.             e.printStackTrace();
  84.         }
  85.        
  86.         return -1;     
  87.     }
  88.    
  89.    
  90.     public static void main(String[] args) {
  91.         Main main = new Main();
  92.         System.out.println(main.numberOfCities());
  93.         main.slovenskaMesta();
  94.  
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement