Advertisement
Guest User

Untitled

a guest
May 11th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package databaza;
  7.  
  8. import java.sql.*;
  9.  
  10. /**
  11.  *
  12.  * @author student
  13.  */
  14. public class Main {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.         // TODO code application logic here
  21.         String userName = "root";
  22.         String password = "";
  23.         String URL = "jdbc:mysql://localhost/zamestnanci";
  24.         Connection con = null;
  25.         Statement stmt = null;
  26.  
  27.         try {
  28.             Class.forName("com.mysql.jdbc.Driver");
  29.             con = DriverManager.getConnection(URL, userName, password);
  30.         } catch (Exception e) {
  31.             System.out.println("SQL Exception: " + e.toString());
  32.         }
  33.  
  34.         // vsetci
  35.         try {
  36.             stmt = con.createStatement();
  37.  
  38.             String SQL = "SELECT * FROM zamestnanci";
  39.             ResultSet result = stmt.executeQuery(SQL);
  40.             System.out.println("Vsetci:");
  41.             while (result.next()) {
  42.                 System.out.println(result.getString("meno") + ":"
  43.                         + result.getString("pocet_deti") + ":"
  44.                         + result.getString("bydlisko") + ":"
  45.                         + result.getString("stav"));
  46.             }
  47.             System.out.println();
  48.         } catch(SQLException ex) {
  49.             System.out.println("SQLException: " + ex.getMessage());
  50.         }
  51.        
  52.         // bezdetny
  53.         try {
  54.             stmt = con.createStatement();
  55.  
  56.             String SQL = "SELECT * FROM zamestnanci WHERE pocet_deti < 1";
  57.             ResultSet result = stmt.executeQuery(SQL);
  58.             System.out.println("Bezdetny:");
  59.             while (result.next()) {
  60.                 System.out.println(result.getString("meno") + ":"
  61.                         + result.getString("pocet_deti") + ":"
  62.                         + result.getString("bydlisko") + ":"
  63.                         + result.getString("stav"));
  64.             }
  65.             System.out.println();
  66.         } catch(SQLException ex) {
  67.             System.out.println("SQLException: " + ex.getMessage());
  68.         }
  69.  
  70.          // nitrancania
  71.         try {
  72.             stmt = con.createStatement();
  73.  
  74.             String SQL = "SELECT * FROM zamestnanci WHERE bydlisko LIKE '%Nitra%'";
  75.             ResultSet result = stmt.executeQuery(SQL);
  76.             System.out.println("Nitrancania:");
  77.             while (result.next()) {
  78.                 System.out.println(result.getString("meno") + ":"
  79.                         + result.getString("pocet_deti") + ":"
  80.                         + result.getString("bydlisko") + ":"
  81.                         + result.getString("stav"));
  82.             }
  83.             System.out.println();
  84.         } catch(SQLException ex) {
  85.             System.out.println("SQLException: " + ex.getMessage());
  86.         }
  87.        
  88.        
  89.         // priemerny pocet deti
  90.         try {
  91.             stmt = con.createStatement();
  92.  
  93.             String SQL = "SELECT AVG(pocet_deti) AS pocet_deti FROM zamestnanci";
  94.             ResultSet result = stmt.executeQuery(SQL);
  95.             System.out.println("Priemerny pocet deti:");
  96.             while (result.next()) {
  97.                 System.out.println(result.getString("pocet_deti"));
  98.             }
  99.             System.out.println();
  100.         } catch(SQLException ex) {
  101.             System.out.println("SQLException: " + ex.getMessage());
  102.         }
  103.  
  104.          // pocet slobodnych
  105.         try {
  106.             stmt = con.createStatement();
  107.  
  108.             String SQL = "SELECT count(stav) AS stav FROM zamestnanci WHERE stav LIKE 'slobodny'";
  109.             ResultSet result = stmt.executeQuery(SQL);
  110.             System.out.println("Pocet slobodnych:");
  111.             while (result.next()) {
  112.                 System.out.println(result.getString("stav"));
  113.             }
  114.             System.out.println();
  115.         } catch(SQLException ex) {
  116.             System.out.println("SQLException: " + ex.getMessage());
  117.         }
  118.  
  119.          // pocet ludi mimo NR
  120.         try {
  121.             stmt = con.createStatement();
  122.  
  123.             String SQL = "SELECT count(bydlisko) AS stav FROM";
  124.             ResultSet result = stmt.executeQuery(SQL);
  125.             System.out.println("Pocet ludi mimo NR:");
  126.             while (result.next()) {
  127.                 System.out.println(result.getString("stav"));
  128.             }
  129.             System.out.println();
  130.             stmt.close();
  131.             con.close();
  132.         } catch(SQLException ex) {
  133.             System.out.println("SQLException: " + ex.getMessage());
  134.         }
  135.  
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement