Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ZADANIE 1:
- /*
- * 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 lab01;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class Lab01 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", "inf132310");
- connectionProps.put("password", "inf132310");
- 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");
- Statement stmt1 = null;
- Statement stmt2 = null;
- ResultSet rs1 = null;
- ResultSet rs2 = null;
- try
- {
- stmt1 = conn.createStatement();
- stmt2 = conn.createStatement();
- rs1 = stmt1.executeQuery("select count(*) as lprac_total from pracownicy");
- rs2 = stmt2.executeQuery("select count(*) as lprac, id_zesp from pracownicy group by id_zesp");
- rs1.next();
- System.out.println("Zatrudniono " + rs1.getInt("lprac_total") + " pracowników, w tym:");
- while (rs2.next()) {
- System.out.println(rs2.getInt("lprac") + " w zespole " + rs2.getInt("id_zesp") + ",");
- }
- }
- catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- }
- finally {
- if (rs1 != null) {
- try
- {
- rs1.close();
- }
- catch (SQLException e) {
- /* kod obsługi */
- }
- }
- if (stmt1 != null) {
- try {
- stmt1.close();
- }
- catch (SQLException e) {
- /* kod obsługi */
- }
- }
- }
- }
- 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);
- }
- try {
- conn.close();
- // TODO code application logic here
- } catch (SQLException ex) {
- Logger.getLogger(Lab01.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("exit db");
- }
- }
- ZADANIE 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 lab01;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class Lab01 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", "inf132310");
- connectionProps.put("password", "inf132310");
- 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");
- Statement stmt1 = null;
- Statement stmt2 = null;
- ResultSet rs1 = null;
- ResultSet rs2 = null;
- try
- {
- stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs1 = stmt1.executeQuery("select nazwisko from pracownicy where etat='ASYSTENT' order by placa_pod desc");
- rs1.last();
- System.out.println(rs1.getString("nazwisko"));
- rs1.absolute(-3);
- System.out.println(rs1.getString("nazwisko"));
- rs1.absolute(-2);
- System.out.println(rs1.getString("nazwisko"));
- }
- catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia" + ex.toString());
- }
- finally {
- if (rs1 != null) {
- try
- {
- rs1.close();
- }
- catch (SQLException e) {
- /* kod obsługi */
- }
- }
- if (stmt1 != null) {
- try {
- stmt1.close();
- }
- catch (SQLException e) {
- /* kod obsługi */
- }
- }
- }
- }
- 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);
- }
- try {
- conn.close();
- // TODO code application logic here
- } catch (SQLException ex) {
- Logger.getLogger(Lab01.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("exit db");
- }
- }
- ZADANIE 3:
- /*
- * 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 lab01;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class Lab01 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", "inf132310");
- connectionProps.put("password", "inf132310");
- 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");
- Statement stmt1 = null;
- Statement stmt2 = null;
- ResultSet rs1 = null;
- ResultSet rs2 = null;
- try
- {
- int [] zwolnienia={150, 200, 230};
- String [] zatrudnienia={"Kandefer", "Rygiel", "Boczar"};
- int [] zatrudnienia_id_prac={240,250,260};
- stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- for(int i=0;i<3;i++) {
- stmt1.executeUpdate("delete from pracownicy where id_prac=" + zwolnienia[i]);
- stmt1.executeUpdate("insert into pracownicy(id_prac, nazwisko) values('" + zatrudnienia_id_prac[i] + "', '" + zatrudnienia[i] + "')");
- }
- stmt1.close();
- }
- catch (SQLException ex) {
- System.out.println("Bład wykonania polecenia " + ex.toString());
- }
- finally {
- if (rs1 != null) {
- try
- {
- rs1.close();
- }
- catch (SQLException e) {
- /* kod obsługi */
- }
- }
- if (stmt1 != null) {
- try {
- stmt1.close();
- }
- catch (SQLException e) {
- /* kod obsługi */
- }
- }
- }
- }
- 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);
- }
- try {
- conn.close();
- // TODO code application logic here
- } catch (SQLException ex) {
- Logger.getLogger(Lab01.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("exit db");
- }
- }
- ZADANIE 4:
Add Comment
Please, Sign In to add comment