Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package postgres;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Postgres
  6. {
  7.     public static void main(String[] args) throws SQLException
  8.     {
  9.         try
  10.         {
  11.             Class.forName("org.postgresql.Driver");
  12.         }
  13.         catch (ClassNotFoundException cnfe)
  14.         {
  15.             System.err.println("Couldn't find driver class.");
  16.         }
  17.  
  18.         Connection connection = null;
  19.         connection = DriverManager.getConnection(
  20.            "jdbc:postgresql://localhost:5432/uni",
  21.            "postgres",
  22.            "password"
  23.         );
  24.  
  25.         Statement stmt = connection.createStatement();
  26.  
  27.         Integer rest[][] = new Integer[2][11];
  28.  
  29.         rest[0][0]  = 7654; rest[1][0]  = 100;
  30.         rest[0][1]  = 7654; rest[1][1]  = 105;
  31.         rest[0][2]  = 7654; rest[1][2]  = 102;
  32.         rest[0][3]  = 7654; rest[1][3]  = 104;
  33.         rest[0][4]  = 7654; rest[1][4]  = 106;
  34.         rest[0][5]  = 7499; rest[1][5]  = 101;
  35.         rest[0][6]  = 7499; rest[1][6]  = 103;
  36.         rest[0][7]  = 7499; rest[1][7]  = 105;
  37.         rest[0][8]  = 7521; rest[1][8]  = 108;
  38.         rest[0][9]  = 7521; rest[1][9]  = 107;
  39.         rest[0][10] = 7521; rest[1][10] = 104;
  40.  
  41.         for(int i = 0; i < 100; i++)
  42.         {
  43.             for(int j = 0; j < 11; j++)
  44.             {
  45.                 stmt.executeQuery(
  46.                         "SELECT " +
  47.                         "   Customer.name   AS customer, " +
  48.                         "   Employee.name   AS employee, " +
  49.                         "   Department.name AS department " +
  50.                         "FROM " +
  51.                         "   Customer " +
  52.                         "JOIN " +
  53.                         "   resp_for " +
  54.                         "   ON " +
  55.                         "       Customer.custno = resp_for.custno " +
  56.                         "   AND " +
  57.                         "       Customer.custno = '" + rest[1][j] + "' " +
  58.                         "JOIN " +
  59.                         "   Employee " +
  60.                         "   ON " +
  61.                         "       resp_for.empno = Employee.empno " +
  62.                         "   AND " +
  63.                         "       Employee.empno = '" + rest[0][j] +"' " +
  64.                         "JOIN " +
  65.                         "   Department " +
  66.                         "   ON " +
  67.                         "       Employee.deptno = Department.deptno"
  68.                 );
  69.             }
  70.         }
  71.  
  72. //      while (rs.next())
  73. //      {
  74. //          System.out.println("############################");
  75. //
  76. //          System.out.println("Kunde: ");
  77. //          System.out.println(rs.getString("customer"));
  78. //
  79. //          System.out.println("Angesteller: ");
  80. //          System.out.println(rs.getString("employee"));
  81. //
  82. //          System.out.println("Abeteilung: ");
  83. //          System.out.println(rs.getString("department"));
  84. //      }
  85.  
  86. /*
  87.  * CREATE TABLE Employee (
  88.  *  empno INTEGER NOT NULL,
  89.  *  name VARCHAR(40) NOT NULL,
  90.  *  position VARCHAR(40) NOT NULL,
  91.  *  mngrno INTEGER NOT NULL,
  92.  *  hire_date DATE NOT NULL,
  93.  *  salary NUMERIC(10,2) NOT NULL,
  94.  *  commission REAL,
  95.  *  deptno INTEGER NOT NULL,
  96.  *  PRIMARY KEY (empno),
  97.  *  CONSTRAINT emp_dept_fk
  98.  *  FOREIGN KEY (deptno) REFERENCES Department,
  99.  *  CONSTRAINT emp_emp_fk FOREIGN KEY (mngrno) REFERENCES Employee
  100.  * );
  101.  *
  102.  * CREATE TABLE Customer (
  103.  *  custno INTEGER,
  104.  *  name VARCHAR(50) NOT NULL,
  105.  *  address VARCHAR(40),
  106.  *  phone VARCHAR(12) NOT NULL,
  107.  *  comments TEXT,
  108.  *  cred_lim NUMERIC(10,2),
  109.  *  CONSTRAINT cust_pk PRIMARY KEY (custno)
  110.  * );
  111.  *
  112.  * CREATE TABLE resp_for (
  113.  *  empno INTEGER NOT NULL,
  114.  *  custno INTEGER NOT NULL,
  115.  *  PRIMARY KEY (empno,custno),
  116.  *  CONSTRAINT resp_emp_fk FOREIGN KEY (empno) REFERENCES Employee ON UPDATE CASCADE ON DELETE CASCADE,
  117.  *  CONSTRAINT resp_cust_fk FOREIGN KEY (custno) REFERENCES Customer ON DELETE CASCADE ON UPDATE CASCADE
  118.  * );
  119.  *
  120.  * CREATE TABLE Department (
  121.  *  deptno INTEGER,
  122.  *  name VARCHAR(20),
  123.  *  location VARCHAR(40),
  124.  *  PRIMARY KEY(deptno)
  125.  * );
  126.  *
  127.  */
  128.  
  129.         connection.close();
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement