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 lab_jdbc;
- import java.sql.*;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class Lab_JDBC {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", "inf132344");
- connectionProps.put("password", "inf132344");
- try {
- conn = DriverManager.getConnection(
- "jdbc:oracle:thin:@admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl",
- connectionProps);
- System.out.println("Połączono z bazą danych");
- } catch (SQLException ex) {
- Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE,
- "nie udało się połączyć z bazą danych", ex);
- System.exit(-1);
- }
- Statement stmt = null;
- ResultSet rs = null;
- try {
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select id_prac, nazwisko, placa_pod " +
- "from pracownicy");
- while (rs.next()) {
- System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " +
- rs.getFloat(3));
- }
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- // zad 1
- stmt = null;
- rs = null;
- try {
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select count(id_zesp), id_zesp from pracownicy group by id_zesp");
- while (rs.next()) {
- System.out.println(rs.getInt(1) + " pracownikow w zespole " + rs.getInt(2));
- }
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- // zad 2
- stmt = null;
- rs = null;
- try {
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery("select nazwisko, placa_pod from pracownicy where etat='ASYSTENT' order by placa_pod DESC");
- rs.last();
- System.out.println("najmniej " + rs.getString(1) + " " + rs.getFloat(2));
- rs.absolute(3);
- System.out.println("trzeci " + rs.getString(1) + " " + rs.getFloat(2));
- rs.absolute(-2);
- System.out.println("przed ostatni " + rs.getString(1) + " " + rs.getFloat(2));
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- //zad3
- stmt = null;
- rs = null;
- int [] zwolnienia={150, 200, 230};
- String [] zatrudnienia={"Kandefer", "Rygiel", "Boczar"};
- try {
- int changes;
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY );
- for (int i = 0; i < zwolnienia.length; i++) {
- changes = stmt.executeUpdate("DELETE FROM pracownicy WHERE id_prac= " + zwolnienia[i]);
- }
- System.out.println("Usunieto");
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- //zad 3 czesc 2
- try {
- int changes;
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
- for (int i = 0; i < zatrudnienia.length; i++) {
- changes = stmt.executeUpdate("INSERT into pracownicy(id_prac,nazwisko)" + "VALUES(13*i + 103, zatrudnienia[i])");
- }
- System.out.println("Dodano");
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- //zad 4
- stmt = null;
- rs = null;
- try {
- conn.setAutoCommit(false);
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select nazwa" +
- "from etaty");
- while (rs.next()) {
- System.out.println(rs.getString(1));
- }
- // dodawanie
- int changes;
- stmt = null;
- stmt = conn.createStatement();
- changes = stmt.executeUpdate("INSERT INTO etaty(nazwa) VALUES('Wozny')");
- // wyswietlenie
- stmt = null;
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select nazwa" +
- "from etaty");
- while (rs.next()) {
- System.out.println(rs.getString(1));
- }
- //wycofanie
- conn.rollback();
- // wyswietlenie po wycofaniu
- stmt = null;
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select nazwa" +
- "from etaty");
- while (rs.next()) {
- System.out.println(rs.getString(1));
- }
- // dodawanie i zapisanie
- stmt = null;
- stmt = conn.createStatement();
- changes = stmt.executeUpdate("INSERT INTO etaty(nazwa) VALUES('Wozny')");
- // wyswietlenie
- stmt = null;
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select nazwa" +
- "from etaty");
- while (rs.next()) {
- System.out.println(rs.getString(1));
- }
- conn.commit();
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- //zad 5 polecenia przygotowane
- String [] nazwiska={"Woźniak", "Dąbrowski", "Kozłowski"};
- int [] place_2={1300, 1700, 1500};
- String []etaty={"ASYSTENT", "PROFESOR", "ADIUNKT"};
- try {
- stmt = conn.prepareStatement("INSERT into pracownicy(nazwisko,placa_pod,etat) VALUES(?,?,?)");
- for (int i = 0; i < nazwiska.length; i++) {
- stmt.setFloat(1,nazwiska[i]);
- stmt.setString(2,place_2[i]);
- stmt.setInt(3,etaty[i]);
- int changes = stmt.executeUpdate();
- }
- } catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) { /* kod obsługi */ }
- }
- }
- try {
- conn.close();
- } catch (SQLException ex) {
- Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("Odlaczenie od bazy danych");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement