【问题标题】:Convering the value returned by getInt(int) to String is not allowed [duplicate]不允许将 getInt(int) 返回的值转换为 String [重复]
【发布时间】:2017-12-21 00:35:45
【问题描述】:

getInt(int)ResultSet 的函数。 在下面的代码中,我无法理解为什么getInt(int) 返回的integer 没有进入String。实际上,我完全不确定出了什么问题。运行程序后,当我在文本字段中输入一个整数并点击“显示员工”按钮时,它会在应该进行装箱的行(case: "Show emplyee" 块内)显示一些错误。所以经过检查,我觉得这就是问题所在(虽然我不确定)。

以下程序不完整。它应该构建一个 GUI,它有 6 个按钮来检索和编辑数据库(datab4)中的数据和一个文本字段来输入输入并显示输出。我是 Java Swing 的新手。如果有人能纠正我,那将是很大的帮助。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GUI_emp implements ActionListener {

JFrame f;
JTextField tf;

GUI_emp() {
    f = new JFrame();
    f.setSize(300, 600);
    f.setVisible(true);
    tf = new JTextField("Enter id of Employee here");
    f.add(tf);
    JButton b1 = new JButton("Add Employee");
    JButton b2 = new JButton("Edit Employee");
    JButton b3 = new JButton("Delete Employee");
    JButton b4 = new JButton("Show Employee");
    JButton bup = new JButton("Up");
    JButton bdo = new JButton("Down");

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    bup.addActionListener(this);
    bdo.addActionListener(this);
    f.add(b1);
    f.add(b2);
    f.add(b3);
    f.add(b4);
    f.add(bup);
    f.add(bdo);

    f.setLayout(new GridLayout(7, 1));

}

public static void main(String[] args) throws Exception {
    new GUI_emp();
}

public void datab4(JButton b) throws Exception {

    //Class.forName("org.apache.derby.jdbc.ClientDriver");
    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/datab4", "soc", "soc");
    PreparedStatement ps;

    String ch = b.getText();
    switch (ch) {

        case "Add Employee": {
            int i = Integer.valueOf(tf.getText());
            ps = con.prepareStatement("insert into datab4 values(?,?,?,?)");
            ps.setInt(1, i);
            String name = JOptionPane.showInputDialog("Enter name of employee");
            String salary = JOptionPane.showInputDialog("Enter salary of employee");
            String desig = JOptionPane.showInputDialog("Enter designation of employee");
            ps.setString(2, name);
            ps.setInt(3, Integer.valueOf(salary));
            ps.setString(4, desig);
            ps.execute();
            ps.close();
            break;
        }
        case "Edit Employee": {
            int i = Integer.valueOf(tf.getText());
            ps = con.prepareStatement("update datab4 set Name=?, Salary=?, Designation=? where ID = ?");
            String name = JOptionPane.showInputDialog("Enter name of employee");
            String salary = JOptionPane.showInputDialog("Enter salary of employee");
            String desig = JOptionPane.showInputDialog("Enter designation of employee");
            ps.setInt(1, i);
            ps.setString(2, name);
            ps.setInt(3, Integer.valueOf(salary));
            ps.setString(4, desig);
            ps.executeUpdate();
            ps.close();
            break;
        }
        case "Delete Employee": {
            int i = Integer.valueOf(tf.getText());
            ps= con.prepareStatement("delete from datab4 where ID = ?");
            ps.setInt(1, i);
            ps.executeUpdate();
            ps.close();
            break;

        }
        case "Show Employee": {
            int i = Integer.valueOf(tf.getText());
            ps = con.prepareStatement("select * from datab4 where ID = ?");
            ps.setInt(1, i);
            ResultSet rs = ps.executeQuery(); 
            tf.setText(String.valueOf(rs.getInt(1))+". "+"Name: "+rs.getString(2)+" Salary: "+String.valueOf(rs.getInt(3))+" Designation: "+rs.getString(4));//ERROR
            break;
        }



    }
}

public void actionPerformed(ActionEvent e) {
    JButton jb = (JButton) (e.getSource());
    try {
        datab4(jb);
    } catch (Exception ex) {
        Logger.getLogger(GUI_emp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

这是错误:

PM gui_emp.GUI_emp actionPerformed
SEVERE: null
java.sql.SQLException: Invalid operation at current cursor position.
    at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.client.am.ClientResultSet.getInt(Unknown Source)
    at gui_emp.GUI_emp.datab4(GUI_emp.java:105)
    at gui_emp.GUI_emp.actionPerformed(GUI_emp.java:117)
    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:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    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:80)
    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.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: ERROR XJ121: Invalid operation at current cursor position.
    at org.apache.derby.client.am.ClientResultSet.checkForValidCursorPosition(Unknown Source)
    at org.apache.derby.client.am.ClientResultSet.checkGetterPreconditions(Unknown Source)
    ... 39 more

【问题讨论】:

  • boxed into a String - 没有这样的东西
  • @Eran 我的意思是将int 转换为String
  • @LorisSecuro 感谢您找到我的副本。它回答了我的问题。

标签: java swing user-interface awt actionlistener


【解决方案1】:

使用这个

ResultSet rs = ps.executeQuery(); 
while(rs.next()){
    tf.setText(String.valueOf(rs.getInt(1))+". "+"Name: "+rs.getString(2)+" Salary: "+String.valueOf(rs.getInt(3))+" Designation: "+rs.getString(4));
}

【讨论】:

  • 通常,一旦回答问题,就会解释代码的作用。
猜你喜欢
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2018-04-25
  • 2016-06-15
相关资源
最近更新 更多