Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.SQLException;
- import java.sql.*;
- public class Main {
- public static void main(String[] args) throws SQLException {
- SQLConnection connection = new SQLConnection();
- connection.drukuj();
- System.out.println();
- connection.wstawNowy(connection.maxPensja());
- connection.drukuj();
- }
- }
- public class SQLConnection {
- public Connection c = null;
- public static Statement stmt = null;
- public SQLConnection(){
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:main");
- c.setAutoCommit(true);
- System.out.println("Opened database successfully");
- stmt = c.createStatement();
- } catch ( Exception e ) {
- System.out.println("blad \n");
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- System.out.println("Operation done successfully");
- }
- public static void wstawNowy(int pensja) throws SQLException {
- stmt.executeUpdate("INSERT INTO pracownicy VALUES (" + "\"Lukasiak\"," + "\"1997\"," + pensja + ")");
- }
- public static int maxPensja() throws SQLException {
- ResultSet rs = stmt.executeQuery("SELECT * FROM pracownicy WHERE pensja = (SELECT max(pensja) FROM pracownicy);");
- int pensja = 0;
- while ( rs.next() ) {
- String nazwisko = rs.getString("nazwisko");
- String rocznik = rs.getString("rocznik");
- pensja = rs.getInt("pensja");
- System.out.println( nazwisko + " " + rocznik + " " + pensja);
- }
- return pensja;
- }
- public static void drukuj() throws SQLException {
- ResultSet rs = stmt.executeQuery( "SELECT * FROM pracownicy;" );
- while ( rs.next() ) {
- String nazwisko = rs.getString("nazwisko");
- String rocznik = rs.getString("rocznik");
- int pensja = rs.getInt("pensja");
- System.out.println( nazwisko + " " + rocznik + " " + pensja);
- }
- }
- }
- sqlite3
- .databases
- create table main.pracownicy (nazwisko char(20), rocznik char(4), pensja int);
- .tables
- insert into pensje values ("Tomek", "1969", 1222);
- insert into pensje values ("Piotr", "1997",
- 1333);
- select * from pracownicy;
- .backup main
- .quit
- sqlite3
- .restore main
- .tables
- select * from pracownicy;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement