2012-03-15 6 views
2

そのとき、ルート上の生徒の新しい予約を作成するアプリケーションを作成しました。 これは、これらの時間を含むデータベースを使用して衝突時間を計算する必要があります。PL/SQL/Java - SQLはループとif文とリストを選択します

生徒は12:00の試験を受験し、プログラムはその時間の他のテストをチェックし、衝突時間をチェックし、衝突があれば開始時間に5分を追加して再試行します。それが私が目指しているものです。

私はこれが完璧であるか正確ではないことを知っていますが、それが私がここにいる理由です。

予約クラス

public class Booking 
{ 
    private int bookingId; 
    private String route; 
    private int startTime; 
    private String bookingDate; 

    public Booking() 
    { 
     bookingId = 0000; 
     route = "No Route Entered"; 
     startTime = 0000; 
     bookingDate = "No Date entered"; 
    } 

    public int getBookingId() 
    { 
     return bookingId; 
    } 

    public String getRoute() 
    { 
     return route; 
    } 

    public int getStartTime() 
    { 
     return startTime; 
    } 

    public String getBookingDate() 
    { 
     return bookingDate; 
    } 

    public void setBookingId(int bookingId) 
    { 
     this.bookingId = bookingId; 
    } 

    public void setRoute(String route) 
    { 
     this.route = route; 
    } 

    public void setStartTime(int startTime) 
    { 
     this.startTime = startTime; 
    } 

    public void setBookingDate(String bookingDate) 
    { 
     this.bookingDate = bookingDate; 
    } 

    public Booking(int bookingId, String route, int startTime, String bookingDate) 
    { 
     setBookingId(bookingId); 
     setRoute(route); 
     setStartTime(startTime); 
     setBookingDate(bookingDate); 
    } 

    public String toString() 
    { 
     return "BookingId: " + getBookingId() + "\nRoute: " + getRoute() + "\nStart Time: " + getStartTime() + 
       "\nBooking Date: " + getBookingDate(); 
    } 
} 

MAIN CLASS

import java.sql.*; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import javax.swing.JOptionPane; 

public class Main { 

    public static void main(String[] args) throws SQLException { 
     //<editor-fold defaultstate="collapsed" desc="Creates new Student and booking"> 

     Student s1 = new Student(); 
     Booking b1 = new Booking(); 
     s1.setStudentId(Integer.parseInt(JOptionPane.showInputDialog("Enter ID for Student: [0001]"))); 
     s1.setFname(JOptionPane.showInputDialog("Enter first name of Student: ")); 
     s1.setLname(JOptionPane.showInputDialog("Enter last name of Student: ")); 
     s1.setAddress(JOptionPane.showInputDialog("Enter address for Student: ")); 
     s1.setPhoneNo(JOptionPane.showInputDialog("Enter phone number for Student: ")); 
     s1.setOtherDetails(JOptionPane.showInputDialog("Enter other details for Student: [Glasses?]")); 

     b1.setBookingId(0002); 
     b1.setStartTime(Integer.parseInt(JOptionPane.showInputDialog("Enter Start time for Booking: [1200]"))); 
     b1.setBookingDate(JOptionPane.showInputDialog("Enter Date for Booking: [01-JAN-12]")); 

     int records = 0; 

     List <Booking> allBookings = new ArrayList<Booking>(); 

     allBookings.add(b1); 

     for(Booking b:allBookings) { 

      JOptionPane.showMessageDialog(null, b1.getStartTime());//Get Start Time from user 

      //<editor-fold defaultstate="collapsed" desc="To select max time of all routes"> 
      try { 
       Class.forName("oracle.jdbc.driver.OracleDriver"); 
       //load the oracle driver...needs to be in classes folder in jre folder 
      } catch (ClassNotFoundException e) { 
       System.out.println(
         " Can't find class oracle.jdbc.driver.OracleDriver"); 
       System.exit(1); 
      } 

      Connection conn = null; 
      //new connection object 
      Statement stmtMax = null; 
      //new statemnt object 
      ResultSet maxTime = null; 
      //new record set object 
      try { 
       conn = DriverManager.getConnection("jdbc:oracle:thin:@oracle.tralee.ie:1521:orcl", 
         "*", "*"); 
       stmtMax = conn.createStatement(); 
       // create the statement for this connection 
       //</editor-fold> 

       maxTime = stmtMax.executeQuery(
       "SELECT MAX(LENGTH) FROM ROUTE"); 
       // get the results of select query and store in recordset object 
       while (maxTime.next()) { 
        // move to first/next record of recordset 
        JOptionPane.showMessageDialog(null, "Check: Max time of all routes: " + maxTime.getString(1)); 
        // output next record using string format 
       } 
       //<editor-fold defaultstate="collapsed" desc="Error handling for Select Statement"> 
       maxTime.close(); 
       maxTime = null; 
       stmtMax.close(); 
       stmtMax = null; 
       conn.close(); 
       conn = null; 

      } catch (SQLException e) { 
       System.out.println(" A SQL error: " + e.getMessage()); 
      } finally { 
       if (maxTime != null) { 
        try { 
         maxTime.close(); 
        } catch (SQLException ignore) { 
        } 
       } 

       if (stmtMax != null) { 
        try { 
         stmtMax.close(); 
        } catch (SQLException ignore) { 
        } 
       } 

       if (conn != null) { 
        try { 
         conn.close(); 
        } catch (SQLException ignore) { 
        } 
       } 
      } 
      // </editor-fold> 
      //<editor-fold defaultstate="collapsed" desc="To select all bookings within a time"> 
      try {     Class.forName("oracle.jdbc.driver.OracleDriver"); 
       //load the oracle driver...needs to be in classes folder in jre folder 
      } catch (ClassNotFoundException e) { 
       System.out.println(
         " Can't find class oracle.jdbc.driver.OracleDriver"); 
       System.exit(1); 
      } 

ERROR -

Connection conn2 = null; 
       //new connection object 
       Statement stmtTime = null; 
       //new statemnt object 
       ResultSet withinTime = null; 
       //new record set object 
       try { 
        conn2 = DriverManager.getConnection("jdbc:oracle:thin:@oracle.tralee.ie:1521:orcl", 
          "*", "*"); 
        stmtTime = conn2.createStatement(); 
        // create the statement for this connection 
        //</editor-fold> 

        withinTime = stmtTime.executeQuery(
        "SELECT * FROM BOOKINGS WHERE" + b1.getStartTime() + "<=" + b1.getStartTime() + "-" + maxTime + 
          "AND" + b1.getStartTime() + ">=" + b1.getStartTime() + "+" + maxTime); 
        // get the results of select query and store in recordset object 

        JOptionPane.showMessageDialog(null, " Check: Bookings within a time: \n" + withinTime.getString(1)); 

        while (withinTime.next()) { 
         // move to first/next record of recordset 
         JOptionPane.showMessageDialog(null, " Check: Bookings within a time: \n" + withinTime.getString(1)); 
         // output next record using string format 
        } 
        //<editor-fold defaultstate="collapsed" desc="Error handling for Select Statement"> 
        withinTime.close(); 
        withinTime = null; 
        stmtTime.close(); 
        stmtTime = null; 
        conn2.close(); 
        conn2 = null; 
       } catch (SQLException e) { 
        System.out.println(" A SQL error: " + e.getMessage()); 
       } finally { 
        if (withinTime != null) { 
         try { 
          withinTime.close(); 
         } catch (SQLException ignore) { 
         } 
        } 

        if (stmtTime != null) { 
         try { 
          stmtTime.close(); 
         } catch (SQLException ignore) { 
         } 
        } 

        if (conn2 != null) { 
         try { 
          conn2.close(); 
         } catch (SQLException ignore) { 
         } 
        } 
       } 
    //END OF ERROR 

//<editor-fold defaultstate="collapsed" desc="To select all free routes"> 
try {      Class.forName("oracle.jdbc.driver.OracleDriver"); 
        //load the oracle driver...needs to be in classes folder in jre folder 
       } catch (ClassNotFoundException e) { 
        System.out.println(
          " Can't find class oracle.jdbc.driver.OracleDriver"); 
        System.exit(1); 
       } 

       Connection conn3 = null; 
       //new connection object 
       Statement stmtFreeR = null; 
       //new statemnt object 
       ResultSet freeRoute = null; 
       //new record set object 
       try { 
        conn3 = DriverManager.getConnection("jdbc:oracle:thin:@oracle.tralee.ie:1521:orcl", 
          "*", "*"); 
        stmtFreeR = conn3.createStatement(); 
        // create the statement for this connection 

        freeRoute = stmtFreeR.executeQuery(
        " SELECT ROUTEID FROM Route MINUS SELECT ROUTEID FROM Booking "); 
        // get the results of select query and store in recordset object 

        while (freeRoute.next()) { 
         // move to first/next record of recordset 
         JOptionPane.showMessageDialog(null, "Check: Select all free RouteId's: " + freeRoute.getString(1)); 
         //JOptionPane.showMessageDialog(null, " the answer is " + fRoutes); 
         // output next record using string format 
        } 

      //<editor-fold defaultstate="collapsed" desc="To randomize free routes"> 

    //ERROR -- Does not do anything? 

       if(freeRoute != null) { 

       List RouteX = new ArrayList(); 

       while (freeRoute.next()) { 
        RouteX.add(freeRoute.getString(1)); 

        Collections.shuffle(RouteX); 

        JOptionPane.showMessageDialog(null, "Free routes list: " + RouteX); 

       } 
       } 
      else { 
       List RouteX = new ArrayList(); 

       while (freeRoute.next()) { 
        RouteX.add(freeRoute.getString(1)); 

        Collections.shuffle(RouteX); 
        JOptionPane.showMessageDialog(null,"Free routes list: " + RouteX); 
       } 
      } 

        //<editor-fold defaultstate="collapsed" desc="Error handling for Select Statement"> 
        freeRoute.close(); 
        freeRoute = null; 
        stmtFreeR.close(); 
        stmtFreeR = null; 
        conn3.close(); 
        conn3 = null; 

       } catch (SQLException e) { 
        System.out.println(" A SQL error: " + e.getMessage()); 

       } finally { 
        if (freeRoute != null) { 
         try { 
          freeRoute.close(); 
         } catch (SQLException ignore) { 
         } 
        } 

        if (stmtFreeR != null) { 
         try { 
          stmtFreeR.close(); 
         } catch (SQLException ignore) { 
         } 
        } 

        if (conn3 != null) { 
         try { 
          conn3.close(); 
         } catch (SQLException ignore) { 
         } 
        } 
       } 

       //<editor-fold defaultstate="collapsed" desc="To count number of routes"> 
       try { 
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
        //load the oracle driver...needs to be in classes folder in jre folder 
       } catch (ClassNotFoundException e) { 
        System.out.println(
          " Can't find class oracle.jdbc.driver.OracleDriver"); 
        System.exit(1); 
       } 

       Connection conn4 = null; 
       //new connection object 
       Statement stmtCountR = null; 
       //new statemnt object 
       // ResultSet countRoutes = null; 
       //new record set object 
       try { 
        conn4 = DriverManager.getConnection("jdbc:oracle:thin:@oracle.tralee.ie:1521:orcl", 
          "*", "*"); 
        stmtCountR = conn4.createStatement(); 
        // create the statement for this connection 
        //</editor-fold> 

       String sql = "SELECT COUNT(*) FROM ROUTE"; 
       PreparedStatement prest = conn4.prepareStatement(sql); 
       ResultSet rs = prest.executeQuery(); 
       while (rs.next()){ 
       records = rs.getInt(1); 

       } 
       //System.out.println("Number of records: " + records); 
         // move to first/next record of recordset 
         JOptionPane.showMessageDialog(null, " Number of total routes: " + records); 
         //JOptionPane.showMessageDialog(null, " the answer is " + fRoutes); 
         // output next record using string format 

        //<editor-fold defaultstate="collapsed" desc="Error handling for Select Statement"> 
        // countRoutes.close(); 
        //countRoutes = null; 
        rs.close(); 
        rs = null; 
        stmtCountR.close(); 
        stmtCountR = null; 
        conn4.close(); 
        conn4 = null; 

       } catch (SQLException e) { 
        System.out.println(" A SQL error: " + e.getMessage()); 

       }/* finally { 
        if (countRoutes != null) { 
         try { 
          countRoutes.close(); 
         } catch (SQLException ignore) { 
         } 
        }*/ 

        if (stmtCountR != null) { 
         try { 
          stmtCountR.close(); 
         } catch (SQLException ignore) { 
         } 
        } 

        if (conn4 != null) { 
         try { 
          conn4.close(); 
         } catch (SQLException ignore) { 
         } 
        } 
       } 

    //ERROR - for(r[X]) -- Looking to assign R with an incremented X value. i.e. R1, R2, R3 -- 

       for(int X = 1; X < records; X++) { 

        for(r[X]) { 

       //<editor-fold defaultstate="collapsed" desc="To check if RX is in Collision Table"> 
       try { 
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
        //load the oracle driver...needs to be in classes folder in jre folder 
       } catch (ClassNotFoundException e) { 
        System.out.println(
          " Can't find class oracle.jdbc.driver.OracleDriver"); 
        System.exit(1); 
       } 

       Connection conn5 = null; 
       //new connection object 
       Statement stmtFindRx = null; 
       //new statemnt object 
       ResultSet checkRx = null; 
       //new record set object 
       try { 
        conn5 = DriverManager.getConnection("jdbc:oracle:thin:@oracle.tralee.ie:1521:orcl", 
          "*", "*"); 
        stmtFindRx = conn5.createStatement(); 
        // create the statement for this connection 
         checkRx = stmtFindRx.executeQuery(
         "*********"); 
         // get the results of select query and store in recordset object 

         while (checkRx.next()) { 
         // move to first/next record of recordset 
         JOptionPane.showMessageDialog(null, " the answer is " + checkRx.getString(1)); 

         // output next record using string format 
        } 
        //<editor-fold defaultstate="collapsed" desc="Error handling for Select Statement"> 
        checkRx.close(); 
        checkRx = null; 
        stmtFindRx.close(); 
        stmtFindRx = null; 
        conn5.close(); 
        conn5 = null; 

       } catch (SQLException e) { 
        System.out.println(" A SQL error: " + e.getMessage()); 
       } finally { 
        if (checkRx != null) { 
         try { 
          checkRx.close(); 
         } catch (SQLException ignore) { 
         } 
        } 
        if (stmtFindRx != null) { 
         try { 
          stmtFindRx.close(); 
         } catch (SQLException ignore) { 
         } 
        } 

        if (conn5 != null) { 
         try { 
          conn5.close(); 
         } catch (SQLException ignore) { 
         } 
        } 
       } 
        } 

       } 

ERROR - 彼は同じに5minsを追加した場合、RXは=ルートは、入力された場合は、確認してください。

  if(R[X].equals(b1.getRoute())) { 

       b1.setStartTime(b1.getStartTime() + 0005); 
      } else { 

      String strConn = "jdbc:oracle:thin:@oracle.tralee.ie:1521:orcl"; 
      String strUser = "*"; 
      String strPassword = "*"; 

      try { 
      Driver drv = new oracle.jdbc.driver.OracleDriver(); 
      DriverManager.registerDriver(drv); 
      Connection conn6 = DriverManager.getConnection(strConn, strUser, strPassword); 
      //code to execute commands... 

      //Booking Insert 
      String query1 = "INSERT INTO Booking(BOOKINGID, BOOKINGTYPE, LNAME, STARTTIME, " + 
      "BOOKINGDATE, HISTORY) VALUES (?, ?, ?, ?, ?)"; 

      PreparedStatement pstmt1 = conn6.prepareStatement(query1); 
      pstmt1.setInt(1, b1.getBookingId()); 
      pstmt1.setDouble(3, b1.getStartTime()); 
      pstmt1.setString(4, b1.getBookingDate()); 
      pstmt1.executeUpdate(); 

      JOptionPane.showMessageDialog(null, "Booking Confirmed"); 

      conn6.close(); 
      } 

      catch(SQLException e) { 
      System.out.println(" A SQL error: " + e.getMessage()); 
       } 
      } 
      } 
     } 

私もまだについて気にいないよ、と私はそれが新しい接続毎回、ループ中を作成するためによくないことを知っている最後のエラー、私はこの後にソートされます。しかし、今のところ、私が求めていますことはあり

1)ないのはなぜこの文:

SELECT * FROM BOOKINGS WHERE" + 
b1.getStartTime() + "<=" + b1.getStartTime() + "-" + maxTime + 
"AND" + b1.getStartTime() + ">=" + b1.getStartTime() + "+" + maxTime); 

復帰開始時間を加えたすべてのルートの最大時間内に予約?どのように私はこれを修正するのですか?

2)これを調整して、selectが呼び出され、未予約の空きルートすべての値が返されるようにするには、リストに挿入してリストをシャッフルしてルート値を返します。

選択が実行され、このエラーが表示されますが、正しい値を返します。

ORA-00933:SQLコマンドが正常に)「ルートMINUS予約から選択したルートID FROMルートIDを選択」 を終了していません。

   while (freeRoute.next()) { 
        JOptionPane.showMessageDialog(null, "Check: Select all free RouteId's: " + freeRoute.getString(1)); 

       } 

      if(freeRoute != null) { 
      List RouteX = new ArrayList(); 

      while (freeRoute.next()) { 
       RouteX.add(freeRoute.getString(1)); 

       Collections.shuffle(RouteX); 

       JOptionPane.showMessageDialog(null, "Free routes list: " + RouteX); 
      } 
      } 
     else { 
      List RouteX = new ArrayList(); 

      while (freeRoute.next()) { 
       RouteX.add(freeRoute.getString(1)); 

       Collections.shuffle(RouteX); 

       JOptionPane.showMessageDialog(null,"Free routes list: " + RouteX); 
      } 
     } 

そして3)このステートメントのためには、私はそれがX値とRを割り当て、基本的にはfor文の各R [X値]その後、体のために を言いたいですか?

for(int X = 1; X < records; X++) { 

       for(r[X]) { 

は、私はここに多くのことを求めている知っている、誰かが、私は発射これらの事を得るために変更する必要があるものにいくつかの光を当てることができれば、それは非常に感謝します。

EDIT:

予約TABLE

CREATE BOOKING 
     (
     BOOKINGID NUMBER(4,0), 
     STARTTIME NUMBER(4,0), 
     BOOKINGDATE DATE, 
     EXAMINERID NUMBER(4,0), 
     STUDENTID NUMBER(4,0), 
     ROUTEID NUMBER(4,0), 
     CONSTRAINT BOOKING_PK PRIMARY KEY (BOOKINGID) 
CONSTRAINT EXAMINER_FK FOREIGN KEY (EXAMINERID) REFERENCES EXAMINER, 
CONSTRAINT STUDENT_FK FOREIGN KEY (STUDENTID) REFERENCES STUDENT, 
CONSTRAINT ROUTE_FK FOREIGN KEY (ROUTEID) REFERENCES ROUTE); 

答えて

0

ただ、最初のオフ、私はあなたは、MySQLの時間関数を見てすべきだと思います。 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_addtimeより具体的には、addtime関数。

私はあなたが確認され、それに対して列です正確に何種類かわからないけど、クエリは、開始時刻の予約表の列がどのようなものがあり

SELECT * FROM BOOKINGS WHERE" + b1.getStartTime() + "<=" + b1.getStartTime() + "-" + maxTime + "AND" + b1.getStartTime() + ">=" + b1.getStartTime() + "+" + maxTime); 

その列を検証するようではありません。実行現在のあなたのクエリは次のようになります。

select * from bookings where12<=12-5and12>=12+5 

私は非常にあなたが残りなく容易に声明をデバッグできるように、最初のクエリ文字列を代入し、出力する文をコンソールを使用して文字列を使用recommandうアプリケーション。また、クエリにユーザー入力権を渡しているので、パラメータ化されたクエリについて読むことをお勧めします。これはSQLインジェクションのための大きな扉です。このリンクを参照してください:

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

あなたがあなたの予約テーブルスキーマを投稿する場合、私たちはあなたの要件に基づいて、右のクエリを作ることができます確信しています。

EDIT

これは、あなたが必要な正確にクエリではないかもしれませんが、それはあなたに良い出発点を与えます。私は非常にあなたが使用しているツールのSQL Plusのクエリを最初にデバッグすることをお勧めします。

PreparedStatement prest; 
Connection conn2 = DriverManager.getConnection("jdbc:");//set you jdbc url 
String sql = "select * from bookings where bookingdate = ? and starttime between ? and ?"; 
prest = con.prepareStatement(sql); 
prest.setDate(1,b1.getBookingDate()); 
prest.setInt(2,b1.getStartTime()-5); 
prest.setInt(3,b1.getStartTime()+5); 
ResultSet rs1 = prest.executeQuery(); 
while(rs1.hasNexT()) 
    //extract data here 
+0

ああは、テーブルに追加するのを忘れ、私は私が後で時間に変更しそれまでは、時間が今あらゆる場所にいることを知っているが、病気の今の4桁の数字として扱います。病気のために予約テーブルに質問を更新してください。 – user1081326

+0

予約テーブルに追加 – user1081326

+0

@ user1081326上記の編集を参照してください – Stainedart

関連する問題