Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package sqlite;
  7. import java.sql.*;
  8. /**
  9.  *
  10.  * @author pokrasa
  11.  */
  12. public class SQLite {
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     void wypisz(){
  18.         Connection c = null;
  19.         Statement stmt = null;
  20.        
  21.         try
  22.         {
  23.             Class.forName("org.sqlite.JDBC");
  24.         c = DriverManager.getConnection("jdbc:sqlite:pracownicy.db");
  25.         c.setAutoCommit(true);
  26.         System.out.println("Opened database successfully");
  27.  
  28.         stmt = c.createStatement();
  29.         ResultSet rs = stmt.executeQuery( "SELECT * FROM pensje WHERE rocznik>'1990-01-01';" );
  30.         while ( rs.next() ) {
  31.         String  nazwisko = rs.getString("nazwisko");
  32.                 String rocznik = rs.getString("rocznik");
  33.         int pensja  = rs.getInt("pensja");
  34.                 int id = rs.getInt("ID");
  35.         System.out.println(id +" "+ nazwisko + "  " +pensja);
  36.         }
  37.         rs.close();
  38.         stmt.close();
  39.         c.close();
  40.         }
  41.         catch( Exception e)
  42.         {
  43.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  44.         System.exit(0);
  45.         }
  46.     }
  47.    
  48.     public static void main(String[] args) {
  49.         Connection c = null;
  50.         Statement stmt = null;
  51.        
  52.         try
  53.         {
  54.             Class.forName("org.sqlite.JDBC");
  55.         c = DriverManager.getConnection("jdbc:sqlite:pracownicy.db");
  56.         c.setAutoCommit(true);
  57.         System.out.println("Opened database successfully");
  58.  
  59.         stmt = c.createStatement();
  60.             String sql = "CREATE TABLE pensje " +
  61.                     "(ID INT PRIMARY KEY NOT NULL, " +
  62.                     "nazwisko VARCHAR(255) NOT NULL, " +
  63.                     "rocznik DATE NOT NULL, " +
  64.                     "pensja DECIMAL(8,2) NOT NULL)";
  65.            
  66.             stmt.executeUpdate(sql);
  67.            
  68.             sql = "INSERT INTO pensje (ID, nazwisko, rocznik, pensja) " +
  69.                     "VALUES (1,'Roman','1996-02-23', 2500.50)";
  70.             stmt.executeUpdate(sql);
  71.             sql = "INSERT INTO pensje (ID, nazwisko, rocznik, pensja) " +
  72.                     "VALUES (2,'Andrzej','1982-01-15', 3500.50)";
  73.             stmt.executeUpdate(sql);
  74.             sql = "UPDATE pensje SET pensja=pensja+(1,10*pensja) WHERE rocznik>'1990-01-01'";
  75.             stmt.executeUpdate(sql);
  76.         rs.wypisz();
  77.         stmt.close();
  78.         c.close();
  79.         }
  80.         catch( Exception e)
  81.         {
  82.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  83.         System.exit(0);
  84.         }
  85.     }
  86.    
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement