簡體   English   中英

DAO 函數將數據從一個表插入到另一個表

[英]DAO Function to insert data from one table to another

我正在嘗試編寫一個函數,以與傳統插入相同的方式將數據從一個表插入到另一個表,我做了這樣的事情:

public class MySQLPriorityDAO implements PriorityDAO {

    final String INSERTFROM = "INSERT INTO priorities (callId, priorityNum, employeeCod) SELECT callId, priorityNum, employeeCod FROM calls";
    final String GETALL = "SELECT * FROM priorities";

    private Connection con;
    private PreparedStatement pstm;
    private Statement stm = null;
    private ResultSet rs = null;


    public MySQLPriorityDAO(Connection con) {
        this.con = con;
    }


    @Override
    public void insert(Priority o) throws DAOException {
        try{
           pstm = con.prepareStatement(INSERTFROM);
           /* \/ error under */
           pstm.setInt(1, o.getCallId());
           pstm.setInt(2, o.getPriorityNum());
           pstm.setInt(3, o.getEmployeeCod());

           if(pstm.executeUpdate() == 0 ){
              throw new DAOException("The update couldn't been saved");
           }  
        } catch (SQLException ex) {
         /* And it stops here */
            throw new DAOException("SQL Error", ex);
        } finally {
           if(pstm != null){
               try {
                   pstm.close();
               } catch (SQLException ex) {
                   throw new DAOException("Error to close connection", ex);
               }
           }
        }
    }

這個想法是在用戶將數據從另一個表單插入另一個表之后執行這個函數,這個函數從另一個表中選擇插入數據並放入另一個(優先級):

private void btSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {                                           
            Call call = new Call();
            Priority priority = new Priority();
            MySQLDaoManager man = new MySQLDaoManager("root", "", "localhost", "attendances", 3306);  

            try {
                call.setPriority(cbPriorityDetail.getSelectedItem().toString());
                call.setPriorityCod(Integer.parseInt(tfIdPriorityDetail.getText()));
                chamado.setEmployeeCod(Integer.parseInt(tfEmployeeCod.getText()));
                chamado.setInitialDate(new java.sql.Date(((java.util.Date)tfInitialDate.getValue()).getTime()));
                chamado.setInitialTime(Time.valueOf(tfInitialTime.getText()));
                chamado.setFinalTime(Time.valueOf(tfFinalTime.getText()));

              // this one is to insert all the data filled in the blank textfield as usual
                man.getCallDAO().insert(call);
              // here that i'm trying to pick the data from the call table to put in the priority table 
                man.getPriorityDAO().insert(priority);

                if(tfCallId.getText().length() != 0){
                     call.setCallId(Long.parseLong(tfCallId.getText()));
                     man.getCallDAO().update(call);
                }
                if(tfEmployeeCod.getText() != null && tfInitialDate.getValue() != null && tfInitialTime.getValue() != null && tfFinalTime.getValue() != null){
                    JOptionPane.showMessageDialog(rootPane, "Data Inserted!", "Successful", JOptionPane.INFORMATION_MESSAGE);
                    btNewActionPerformed(evt);
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Please fill the fields again", "Not Successful", JOptionPane.INFORMATION_MESSAGE);
                    btNewActionPerformed(evt);
                }
/* IT IS RETURNING THIS BLOCK ABOVE SHOWING MySQL Error   */
            } catch (DAOException ex) {
                JOptionPane.showMessageDialog(rootPane, "MySQL Error", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(rootPane, "Error to establish connection", "Error", JOptionPane.ERROR_MESSAGE);
        }
} 

生成的異常堆棧跟蹤:

run:
br.com.jdbc.dao.DAOException: SQL Error
    at br.com.jdbc.victor.dao.daoentities.MySQLPriorityDAO.insert(MySQLPriorityDAO.java:58)
    at br.com.jdbc.victor.dao.daoentities.MySQLPriorityDAO.insert(MySQLPriorityDAO.java:28)
    at br.com.jdbc.victor.view.FormNewCall.btSubmitActionPerformed(FormNewCall.java:367)
    at br.com.jdbc.victor.view.FormNewCall.access$500(FormNewCall.java:22)
    at br.com.jdbc.victor.view.FormNewCall$6.actionPerformed(FormNewCall.java:146)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
    at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184)
    at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229)
    at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:227)
    at java.awt.Dialog.show(Dialog.java:1084)
    at java.awt.Component.show(Component.java:1671)
    at java.awt.Component.setVisible(Component.java:1623)
    at java.awt.Window.setVisible(Window.java:1014)
    at java.awt.Dialog.setVisible(Dialog.java:1005)
    at br.com.jdbc.victor.view.FormNewCall.lambda$main$0(FormNewCall.java:543)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3840)
    at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3784)
    at br.com.jdbc.victor.dao.entidadesdao.MySQLPriorityDAO.insert(MySQLPriorityDAO.java:50)
    ... 62 more

我在 cmd 上做了它並且它工作正常沒有錯誤,在這里它返回帶有 DAOException 的 catch 塊......我應該把任何結果集放在那里還是將它更改為語句?

看起來您在選擇中缺少 WHERE 條件,這將指定您正在復制數據的確切調用:

這是修改后的代碼:

final String INSERTFROM = "INSERT INTO priorities " + 
                          "(callId, priorityNum, employeeCod) " + 
                          "SELECT callId, priorityNum, employeeCod " + 
                          "FROM calls WHERE callId=?"; // note WHERE here

@Override
public void insert(Priority o) throws DAOException {
    try{
       pstm = con.prepareStatement(INSERTFROM);
       /* \/ error under */
       pstm.setInt(1, o.getCallId());
       // no need in setting other values, since they are copied 
       // from the calls table

       if(pstm.executeUpdate() == 0 ){
          throw new DAOException("The update couldn't been saved");
       }  
    } catch (SQLException ex) {
     /* And it stops here */
        throw new DAOException("SQL Error", ex);
    } finally {
       if(pstm != null){
           try {
               pstm.close();
           } catch (SQLException ex) {
               throw new DAOException("Error to close connection", ex);
           }
       }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM