Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 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.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author FON
  18. */
  19. public class JDBCTest2 {
  20. public static void main(String[] args) {
  21. dodajOdeljenje(100 , "Neko odeljenje", "Beograd");
  22. }
  23.  
  24. private static void dodajOdeljenje(int sifra, String naziv, String grad) {
  25.  
  26. String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  27. String upit = "insert into odeljenje values (?,?,?)";
  28.  
  29. try (
  30. Connection conn =
  31. DriverManager.getConnection(url, "student", "student");
  32. PreparedStatement ps = conn.prepareStatement(upit);
  33. ){
  34.  
  35. ps.setInt(1, sifra);
  36. ps.setString(2, naziv);
  37. ps.setString(3, grad);
  38.  
  39. int br = ps.executeUpdate();
  40.  
  41. conn.setAutoCommit(false);
  42.  
  43. if(br > 0) {
  44. conn.commit();
  45. }else {
  46. conn.rollback();
  47. }
  48.  
  49. } catch (SQLException ex) {
  50. Logger.getLogger(JDBCTest2.class.getName()).log(Level.SEVERE, null, ex);
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement