Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import java.sql.SQLException;
  2. import java.sql.*;
  3.  
  4.  
  5. public class Main {
  6. public static void main(String[] args) throws SQLException {
  7. SQLConnection connection = new SQLConnection();
  8. connection.drukuj();
  9. System.out.println();
  10. connection.wstawNowy(connection.maxPensja());
  11. connection.drukuj();
  12. }
  13. }
  14.  
  15.  
  16.  
  17. public class SQLConnection {
  18.  
  19. public Connection c = null;
  20. public static Statement stmt = null;
  21.  
  22. public SQLConnection(){
  23. try {
  24. Class.forName("org.sqlite.JDBC");
  25. c = DriverManager.getConnection("jdbc:sqlite:main");
  26. c.setAutoCommit(true);
  27. System.out.println("Opened database successfully");
  28. stmt = c.createStatement();
  29. } catch ( Exception e ) {
  30. System.out.println("blad \n");
  31. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  32. System.exit(0);
  33. }
  34. System.out.println("Operation done successfully");
  35. }
  36.  
  37. public static void wstawNowy(int pensja) throws SQLException {
  38. stmt.executeUpdate("INSERT INTO pracownicy VALUES (" + "\"Lukasiak\"," + "\"1997\"," + pensja + ")");
  39. }
  40.  
  41. public static int maxPensja() throws SQLException {
  42. ResultSet rs = stmt.executeQuery("SELECT * FROM pracownicy WHERE pensja = (SELECT max(pensja) FROM pracownicy);");
  43. int pensja = 0;
  44. while ( rs.next() ) {
  45. String nazwisko = rs.getString("nazwisko");
  46. String rocznik = rs.getString("rocznik");
  47. pensja = rs.getInt("pensja");
  48. System.out.println( nazwisko + " " + rocznik + " " + pensja);
  49. }
  50. return pensja;
  51. }
  52.  
  53. public static void drukuj() throws SQLException {
  54. ResultSet rs = stmt.executeQuery( "SELECT * FROM pracownicy;" );
  55. while ( rs.next() ) {
  56. String nazwisko = rs.getString("nazwisko");
  57. String rocznik = rs.getString("rocznik");
  58. int pensja = rs.getInt("pensja");
  59. System.out.println( nazwisko + " " + rocznik + " " + pensja);
  60. }
  61. }
  62. }
  63.  
  64.  
  65. sqlite3
  66. .databases
  67. create table main.pracownicy (nazwisko char(20), rocznik char(4), pensja int);
  68. .tables
  69. insert into pensje values ("Tomek", "1969", 1222);
  70. insert into pensje values ("Piotr", "1997",
  71. 1333);
  72. select * from pracownicy;
  73. .backup main
  74. .quit
  75.  
  76. sqlite3
  77. .restore main
  78. .tables
  79. select * from pracownicy;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement