DAO 函数将数据从一个表插入到另一个表
Posted
技术标签:
【中文标题】DAO 函数将数据从一个表插入到另一个表【英文标题】:DAO Function to insert data from one table to another 【发布时间】:2017-03-05 10:07:34 【问题描述】:我正在尝试编写一个函数,该函数以与传统插入相同的方式将数据从一个表插入另一个表,我做了这样的事情:
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 块...我应该将任何结果集放在那里还是将其更改为语句?
【问题讨论】:
下定决心。该语句使用的数据是来自您的 Java 代码还是来自calls
表?现在 SQL 语句从 calls
表中复制数据,并且 SQL 语句没有任何 ?
参数标记,这意味着它不能接受来自您的 Java 代码的数据,但您仍然提供来自 Java 的 3 个值,这就是错误消息的内容:您调用setInt(1, ...)
,但没有参数标记。
就是这样!!这个想法是这样的,从calls
表中捕获所有使用的数据并用这3 个字段填充priorities
表......所以,在这种情况下,我必须启动statement
方法,复制所有@ 987654331@数据一一填入priorities
表后是不是?!
你是什么意思“一个接一个”? INSERT-from-SELECT 语句是海量复制。与select语句的WHERE
子句匹配的所有记录(你没有,所以calls
中的所有条记录)将被复制到目标表中。
你在INSERTFROM
中的插入没有任何参数,所以你当然会得到一个错误提示 "java.sql.SQLException: Parameter index out of range (1 > number参数个数,即 0)"
附带说明:sn-ps 仅适用于 javascript、html 和 CSS;不要将它们用于 Java 代码。
【参考方案1】:
看起来您在选择中缺少 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);
【讨论】:
嘿!我现在以这种方式完成了您向我展示的方式,代码执行良好,直到 'if(pstm.executeUpdate() == 0 )' 部分,但它不执行更新并在其下抛出 DAOException,并且SQLException 在我不知道为什么之后......当我在 cmd 中执行它时它工作正常!! 我认为它没有捕捉到 callId 你能提供堆栈跟踪吗? 是的,它生成了这个堆栈跟踪:br.com.jdbc.dao.DAOException:更新无法保存在 br.com.jdbc.victor.dao.daoentities.MySQLPriorityDAO.insert(MySQLPriorityDAO .java:51) 在 br.com.jdbc.victor.dao.daoentities.MySQLPriorityDAO.insert(MySQLPriorityDAO.java:28) 在 br.com.jdbc.victor.view.FormNewCall.btSubmitActionPerformed(FormNewCall.java:368) 在br.com.jdbc.victor.view.FormNewCall.access$500(FormNewCall.java:22) 在 br.com.jdbc.victor.view.FormNewCall$6.actionPerformed(FormNewCall.java:146) 当我调试代码时,我发现它没有从calls
表中获取id
,可能是因为我无法从中捕获所有数据表以上是关于DAO 函数将数据从一个表插入到另一个表的主要内容,如果未能解决你的问题,请参考以下文章
如何在展平嵌套字段后将数据从一个 bigquery 表流式插入到另一个表?