Advertisement
Guest User

baze vezbe

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 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.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17. *
  18. * @author FON
  19. */
  20. public class JDBCTest1 {
  21. public static void main(String[] args) {
  22. nadjiOdeljenja();
  23. }
  24.  
  25. private static void nadjiOdeljenja() {
  26. String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  27. String upit = "select * from odeljenje";
  28. try (Connection conn = DriverManager.getConnection(url, "student", "student");
  29. Statement st = conn.createStatement();
  30. ResultSet rs = st.executeQuery(upit);
  31. ) {
  32.  
  33. while(rs.next()){
  34. int sifra = rs.getInt(1);
  35. String naziv = rs.getString("nazivodelj");
  36. String grad = rs.getNString(3);
  37. System.out.println("Sifra: " + sifra +", naziv: " + naziv +", grad: " + grad);
  38. }
  39.  
  40.  
  41. } catch (SQLException ex) {
  42. Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE, null, ex);
  43. }
  44. }
  45.  
  46. }
  47.  
  48.  
  49. *****jdbc test 2
  50.  
  51.  
  52. /*
  53. * To change this license header, choose License Headers in Project Properties.
  54. * To change this template file, choose Tools | Templates
  55. * and open the template in the editor.
  56. */
  57. package JDBC;
  58.  
  59. import java.sql.Connection;
  60. import java.sql.DriverManager;
  61. import java.sql.PreparedStatement;
  62. import java.sql.SQLException;
  63. import java.util.logging.Level;
  64. import java.util.logging.Logger;
  65.  
  66. /**
  67. *
  68. * @author FON
  69. */
  70. public class JDBCTest2 {
  71. public static void main(String[] args) {
  72. dodajOdeljenje(150,"Proba","Smederevo");
  73. }
  74.  
  75. private static void dodajOdeljenje(int sifra, String naziv, String grad) {
  76. String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  77. String upit = "Inser into odeljenje values(?,?,?)";
  78.  
  79. try (
  80. Connection conn = DriverManager.getConnection(url, "student", "student");
  81. PreparedStatement ps = conn.prepareStatement(upit);
  82. ) {
  83.  
  84. conn.setAutoCommit(false);
  85. ps.setInt(1, sifra);
  86. ps.setString(2, naziv);
  87. ps.setString(3, grad);
  88.  
  89. int br = ps.executeUpdate();
  90.  
  91. if (br > 0) {
  92. conn.commit();
  93. }else{
  94. conn.rollback();
  95. }
  96. } catch (SQLException ex) {
  97. Logger.getLogger(JDBCTest2.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement