DAO用于将数据从一个表插入另一个表的功能

时间:2016-10-23 15:44:10

标签: java mysql jdbc dao

我试图编写一个函数,将数据从一个表插入到另一个表中,与传统插入符相同,我做了类似的事情:

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块...我应该在那里放置任何结果集还是将其更改为语句?

1 个答案:

答案 0 :(得分:1)

看起来您在选择中缺少WHERE条件,这将指定您正在复制数据的确切调用:

以下是修改后的代码:

$( document ).ready(function() {
  var words = document.body.textContent || document.body.innerText,
    matches = words.match(/\d+x[^;]*;/g);
    function strDes(a, b) {
        a = a.split('x')[1];
        b = b.split('x')[1];
       if (a<b) return -1;
       else if (a>b) return 1;
       else return 0;
     }
     var a2=matches;
     a2.sort(strDes);


    $('#myDiv').text(a2).html(a2);