Home > Software engineering >  SQL date cannot be converted to Lang.string
SQL date cannot be converted to Lang.string

Time:09-24

This void function tests the QCManual Results table. When reaching:

String date = (String) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.RUNDATE_INDEX);

Throws Error

public static void checkManualResult(QCManualResultsTableModel qcModel, int row)
    {
        ArrayList<QcResultViolation> violations;
        RuleEvaluator evaluator = null;
        MultiMap<QcRules, QcSteps> ruleMap = new MultiMap<>();
        if (qcModel.getRowCount() > 0)
        {
           //try {
            Integer resultInstId = (Integer) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.INSTID_INDEX);
            Integer qcLotId = (Integer) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.LOTID_INDEX);
            Integer resultLevel = (Integer) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.LEVEL_INDEX);
            Integer testNumber = (Integer) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.TESTNUM_INDEX);
            Double result = (Double) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.RESULT_INDEX);
            String date = (String) qcModel.getValueAt(row, QCManualResultsTableModel.QCManualResult.RUNDATE_INDEX);
            evaluator = new RuleEvaluator(resultInstId);
            ruleMap = getRules(resultInstId, resultLevel, qcLotId); 
            if (ruleMap.size() > 0 && result != null)
            {
                evaluator.SetRuleMap(ruleMap);
                violations = evaluator.CheckResult(testNumber, resultLevel, qcLotId, resultInstId, new BigDecimal(result), date);
            } 

Stack Trace Error:

java.lang.ClassCastException: java.sql.Date cannot be cast to java.lang.String at org.css.QCApproval.QCUtil.checkManualResult(QCUtil.java:88) at org.css.QCTables.QCManualResultsTableModel.setValueAt(QCManualResultsTableModel.java:418) at javax.swing.JTable.setValueAt(JTable.java:2741) at org.jdesktop.swingx.JXTable.setValueAt(JXTable.java:1473) at javax.swing.JTable.editingStopped(JTable.java:4723) at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141) at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:368) at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:233) at org.jdesktop.swingx.table.NumberEditorExt.stopCellEditing(NumberEditorExt.java:129) at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(DefaultCellEditor.java:385) at javax.swing.JTextField.fireActionPerformed(JTextField.java:508) at javax.swing.JTextField.postActionEvent(JTextField.java:721) at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836) at javax.swing.JFormattedTextField$CommitAction.actionPerformed(JFormattedTextField.java:1124) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1662) at javax.swing.JComponent.processKeyBinding(JComponent.java:2879) at javax.swing.JComponent.processKeyBindings(JComponent.java:2926) at javax.swing.JComponent.processKeyEvent(JComponent.java:2842) at java.awt.Component.processEvent(Component.java:6293) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4872) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1887) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727) at java.awt.Component.dispatchEventImpl(Component.java:4742) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747) at java.awt.EventQueue.access$300(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:706) at java.awt.EventQueue$3.run(EventQueue.java:704) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:720) at java.awt.EventQueue$4.run(EventQueue.java:718) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:717) at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159) [catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

CodePudding user response:

You cannot cast a string object to be a date-time object. The types are unrelated.

You can ask a date-time object to generate text.

Never use the terribly flawed legacy date-time types such as java.sql.Date. Use only java.time classes.

To retrieve a value from a column of a type akin to the SQL standard type DATE, use java.time.LocalDate.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

Generate text in standard ISO 8601 format: YYYY-MM-DD.

String output = ld.toString() ;

To generate text in other formats, use DateTimeFormatter and DateTimeFormatterBuilder classes. These have been covered extensively, so search to learn more.

  • Related