Guest User

Untitled

a guest
Jan 14th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package jdbcukol;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.Random;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16.  
  17. class Ukol1
  18. {
  19. Connection con;
  20.  
  21. public Ukol1() {
  22. con = getConnection();
  23.  
  24. Statement stm;
  25. try {
  26. stm = con.createStatement();
  27.  
  28. ResultSet rs = stm.executeQuery("select * from products");
  29.  
  30. Random rand = new Random();
  31.  
  32. int num = rand.nextInt(50);
  33.  
  34. rs.absolute(num);
  35. rs.next();
  36.  
  37. int x = rs.getInt("products_quantity");
  38.  
  39. System.out.println(num + ". " + x);
  40.  
  41. x += 100;
  42.  
  43. stm.execute("UPDATE PRODUCTS SET PRODUCTS_QUANTITY=" + x + " WHERE products_id=" + num);
  44.  
  45. ResultSet rs2 = stm.executeQuery("select * from products");
  46.  
  47. rs2.absolute(num);
  48. rs2.next();
  49.  
  50. x = rs2.getInt("products_quantity");
  51.  
  52. System.out.println(num + ". " + x);
  53.  
  54. con.close();
  55.  
  56. } catch (SQLException ex) {
  57. Logger.getLogger(Ukol1.class.getName()).log(Level.SEVERE, null, ex);
  58. }
  59.  
  60. }
  61.  
  62.  
  63. public static Connection getConnection(){
  64. Connection con = null;
  65. try {
  66. con = DriverManager.getConnection(
  67. "jdbc:mysql://pca1035a.vsb.cz/shop", "shop", "shop");
  68. } catch (SQLException e) {
  69.  
  70. }
  71. return con;
  72. }
  73. }
  74.  
  75. /**
  76. *
  77. * @author tomas
  78. */
  79. public class JDBCukol {
  80.  
  81.  
  82.  
  83.  
  84. /**
  85. * @param args the command line arguments
  86. */
  87. public static void main(String[] args) {
  88. try {
  89. // TODO code application logic here
  90. Class.forName("com.mysql.jdbc.Driver");
  91. } catch (ClassNotFoundException ex) {
  92. Logger.getLogger(JDBCukol.class.getName()).log(Level.SEVERE, null, ex);
  93. }
  94.  
  95. Ukol1 u1 = new Ukol1();
  96.  
  97.  
  98. }
  99. }
Add Comment
Please, Sign In to add comment