Advertisement
Guest User

dfgdfgdfg

a guest
Feb 15th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 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 cv1;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. /**
  15. *
  16. * @author xsevcikj
  17. */
  18. public class Cv1 {
  19.  
  20. /**
  21. * @param args the command line arguments
  22. */
  23. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  24. Statement st = connect();
  25. select(st);
  26. count(st);
  27. Double cena = cenaKnihy("asgfg", st);
  28. System.out.println(cena);
  29. zlava("hahabebe", st);
  30. }
  31.  
  32. public static Statement connect() throws ClassNotFoundException, SQLException{
  33. Class.forName("org.apache.derby.jdbc.ClientDriver");
  34. Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
  35. Statement st = con.createStatement();
  36. return st;
  37. }
  38.  
  39.  
  40. public static void select(Statement st) throws SQLException{
  41. ResultSet rs = st.executeQuery("SELECT * FROM APP.KNIHA");
  42. while (rs.next()){
  43. System.out.println(rs.getString(1));
  44. }
  45. }
  46.  
  47. public static void count(Statement st) throws SQLException{
  48. ResultSet rs = st.executeQuery("SELECT count(*) FROM kniha");
  49. while (rs.next()){
  50. System.out.println("" + rs.getInt(1));
  51. }
  52. }
  53.  
  54. public static Double cenaKnihy(String meno, Statement st) throws SQLException{
  55. ResultSet rs = st.executeQuery("SELECT cena FROM kniha WHERE nazov='"+meno+"'");
  56. double cena = -1.00;
  57. if (!rs.next()){
  58. System.out.println("Nemam taku knihu");
  59. return cena;
  60. }
  61.  
  62. return rs.getDouble(1);
  63. }
  64.  
  65. public static boolean pridajKnihu(String nazov, double cena, Statement st) throws SQLException{
  66. ResultSet rs = st.executeQuery("SELECT * FROM kniha WHERE nazov='"+nazov+"'");
  67. if (!rs.next()){
  68. return false;
  69. }
  70. st.executeUpdate("INSERT INTO kniha " + "VALUES ('"+nazov+"', '"+cena+"')");
  71. return true;
  72. }
  73.  
  74. public static void zlava(String nazov, Statement st) throws SQLException{
  75. Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
  76. PreparedStatement ps = con.prepareStatement("UPDATE kniha SET cena = ? WHERE nazov = ?");
  77. ResultSet rs = st.executeQuery("SELECT cena FROM kniha WHERE nazov='"+nazov+"'");
  78. if (rs.next()){
  79. Double cena = rs.getDouble(1);
  80. cena = cena * 0.8;
  81. ps.setDouble(1, cena);
  82. ps.setString(2, nazov);
  83. ps.executeUpdate();
  84. ps.close();
  85. }
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement