Advertisement
Guest User

TestJDBC1

a guest
Apr 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 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 TESTJDBC1 {
  20.  
  21. public static void main(String[] args) {
  22. dodajOdeljenje(555, "LABIS", "Beograd");
  23. }
  24.  
  25. public static void dodajOdeljenje(int sifra, String naziv, String grad) {
  26. String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  27. String upit = "insert into odeljenje values (?,?,?)";
  28.  
  29. try (Connection konekcija = DriverManager.getConnection(url, "student", "student");
  30. PreparedStatement stat = konekcija.prepareStatement(upit);) {
  31.  
  32. konekcija.setAutoCommit(false);
  33.  
  34. stat.setInt(1, sifra);
  35. stat.setString(2, naziv);
  36. stat.setString(3, grad);
  37.  
  38. int count = stat.executeUpdate();
  39.  
  40. if(count == 1){
  41. konekcija.commit();
  42. } else{
  43.  
  44. konekcija.rollback();
  45. }
  46.  
  47. } catch (SQLException ex) {
  48. Logger.getLogger(TESTJDBC1.class.getName()).log(Level.SEVERE, null, ex);
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement