Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package JDBC;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author FON
- */
- public class JDBCTest1 {
- public static void main(String[] args) {
- nadjiOdeljenja();
- }
- private static void nadjiOdeljenja() {
- String url = "jdbc:oracle:thin:@localhost:1521:orcl";
- String upit = "select * from odeljenje";
- try (Connection conn = DriverManager.getConnection(url, "student", "student");
- Statement st = conn.createStatement();
- ResultSet rs = st.executeQuery(upit);
- ) {
- while(rs.next()){
- int sifra = rs.getInt(1);
- String naziv = rs.getString("nazivodelj");
- String grad = rs.getNString(3);
- System.out.println("Sifra: " + sifra +", naziv: " + naziv +", grad: " + grad);
- }
- } catch (SQLException ex) {
- Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- *****jdbc test 2
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package JDBC;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author FON
- */
- public class JDBCTest2 {
- public static void main(String[] args) {
- dodajOdeljenje(150,"Proba","Smederevo");
- }
- private static void dodajOdeljenje(int sifra, String naziv, String grad) {
- String url = "jdbc:oracle:thin:@localhost:1521:orcl";
- String upit = "Inser into odeljenje values(?,?,?)";
- try (
- Connection conn = DriverManager.getConnection(url, "student", "student");
- PreparedStatement ps = conn.prepareStatement(upit);
- ) {
- conn.setAutoCommit(false);
- ps.setInt(1, sifra);
- ps.setString(2, naziv);
- ps.setString(3, grad);
- int br = ps.executeUpdate();
- if (br > 0) {
- conn.commit();
- }else{
- conn.rollback();
- }
- } catch (SQLException ex) {
- Logger.getLogger(JDBCTest2.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement