Advertisement
Guest User

Untitled

a guest
May 16th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 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 jdbc;
  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. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18. *
  19. * @author student
  20. */
  21. public class JDBCTest1 {
  22.  
  23. public static void main(String[] args) {
  24. dodajProizvod(543,19985678,"SW",555.333,1,12,"hue","helo be humble");
  25. nadjiProizvode();
  26.  
  27. }
  28.  
  29. private static void nadjiProizvode() {
  30. String url = "jdbc:derby://localhost:1527/sample";
  31. String upit = "select * from product";
  32. try (Connection conn = DriverManager.getConnection(url,
  33. "app", "app");
  34. Statement stat = conn.createStatement();
  35. ResultSet rs = stat.executeQuery(upit)) {
  36.  
  37. while (rs.next()) {
  38. int prId = rs.getInt(1);
  39. int manId = rs.getInt(2);
  40. String desc = rs.getString("DESCRIPTION");
  41. double cost = rs.getDouble(4);
  42.  
  43. System.out.println("ID: " + prId
  44. + "\t\t Naziv: " + desc
  45. + "\t\t Cena: " + cost);
  46. }
  47.  
  48. } catch (SQLException ex) {
  49.  
  50. Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE,
  51. null, ex);
  52.  
  53. }
  54. }
  55.  
  56. private static void dodajProizvod(int prId, int manId, String prodCode, double cost, int quan, double markup, String ava, String desc) {
  57.  
  58. String url = "jdbc:derby://localhost:1527/sample";
  59. String upit = "insert into product values(?,?,?,?,?,?,?,?)";
  60.  
  61. try (Connection conn = DriverManager.getConnection(url,
  62. "app", "app");
  63. PreparedStatement stat = conn.prepareStatement(upit)) {
  64.  
  65. conn.setAutoCommit(false);
  66.  
  67. stat.setInt(1, prId);
  68. stat.setInt(2, manId);
  69. stat.setString(3, prodCode);
  70. stat.setDouble(4, cost);
  71. stat.setInt(5, quan);
  72. stat.setDouble(6, markup);
  73. stat.setString(7, ava);
  74. stat.setString(8, desc);
  75.  
  76. int count = stat.executeUpdate();//vraca broj updatovanih redova
  77.  
  78. if (count > 0) {
  79. conn.commit();
  80. } else {
  81. conn.rollback();
  82. }
  83.  
  84. } catch (SQLException ex) {
  85.  
  86. Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE,
  87. null, ex);
  88. }
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement