Guest User

Untitled

a guest
Mar 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.DataInputStream;
  3. import java.io.EOFException;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.sql.*;
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. public class baza_3 {
  11.         void zwieksz(Statement st, String miesiac){
  12.                 try{
  13.                         st.executeUpdate("UPDATE dane SET kwota=kwota+10 WHERE miesiac='"+miesiac+"';");
  14.             } catch (SQLException e) {
  15.                 System.err.println("Wyjatek z zapytania SQL: " + e.getMessage());
  16.                 e.printStackTrace(System.err);
  17.             }
  18.         }
  19.  
  20.         void wydruk(Statement st){
  21.                 try{
  22.                 ResultSet rs = st.executeQuery("SELECT * FROM dane;");
  23.  
  24.                 while (rs.next()) {
  25.                         System.out.printf("Nazwisko: ");
  26.                         System.out.println(rs.getString(1));
  27.                         System.out.printf("Miesiac : ");
  28.                         System.out.println(rs.getInt(2));
  29.                         System.out.printf("Kwota   : ");
  30.                         System.out.println(rs.getInt(3));
  31.                         System.out.println("\n");
  32.                     }
  33.                 rs.close();
  34.  
  35.             } catch (SQLException e) {
  36.                 System.err.println("Wyjatek z zapytania SQL: " + e.getMessage());
  37.                 e.printStackTrace(System.err);
  38.             }
  39.         }
  40.  
  41.      public static void main(String[] args)throws IOException{
  42.                 baza_3 baza = new baza_3();
  43.  
  44.                 String url = "jdbc:postgresql://localhost/baza?user=login&password=haslo";
  45.                 try{
  46.                         Class.forName("org.postgresql.Driver");
  47.                         Connection db = DriverManager.getConnection(url);
  48.  
  49.                         Statement st = db.createStatement();
  50.  
  51.                         baza.zwieksz(st, args[0]);
  52.                         baza.wydruk(st);
  53.  
  54.                         st.close();
  55.  
  56.                 } catch (SQLException e) {
  57.                   System.err.println("Wyjatek z zapytania SQL: " + e.getMessage());
  58.                   e.printStackTrace(System.err);
  59.                 } catch (ClassNotFoundException e) {
  60.                   System.err.println("Wyjatek przy ladowaniu klas: " + e.getMessage());
  61.                   e.printStackTrace(System.err);
  62.                 }
  63.  
  64.  
  65.         }
  66. }
Add Comment
Please, Sign In to add comment