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 main;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- 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 Main {
- private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
- private static String username = "student";
- private static String password ="student";
- public static void main(String[] args) {
- System.out.println("Pre unosa: ");
- nadjiSvaOdeljenja();
- dodajOdeljenje(959, "LABIS", "Beograd");
- System.out.println("Posle unosa: ");
- nadjiSvaOdeljenja();
- }
- public static void nadjiSvaOdeljenja(){
- String query = "SELECT * FROM odeljenje";
- try (Connection connection = DriverManager.getConnection(url,username ,password );
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery(query);) {
- while(rs.next()){
- int sifraOdeljenja = rs.getInt("sifraodelj");
- String nazivOdeljenja = rs.getString(2);
- String grad = rs.getString("grad");
- System.out.println("Sifra: " + sifraOdeljenja + ", Naziv: " + nazivOdeljenja + ", Grad: "+ grad);
- }
- //rs.close();
- //stmt.close();
- //connection.close();
- } catch (SQLException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void dodajOdeljenje(int sifraOdeljenja, String nazivOdeljenja, String grad){
- try {
- String query = "INSERT INTO odeljenje VALUES (?,?,?)";
- Connection connection = DriverManager.getConnection(url, username, password);
- connection.setAutoCommit(false);
- PreparedStatement ps = connection.prepareStatement(query);
- ps.setInt(1, sifraOdeljenja);
- ps.setString(2, nazivOdeljenja);
- ps.setString(3, grad);
- int number = ps.executeUpdate();
- if(number == 1){
- connection.commit();
- }else{
- connection.rollback();
- }
- ps.close();
- connection.close();
- } catch (SQLException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement