Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不允许将getInt(int)返回的值转换为字符串_Java_Swing_User Interface_Awt_Actionlistener - Fatal编程技术网

Java 不允许将getInt(int)返回的值转换为字符串

Java 不允许将getInt(int)返回的值转换为字符串,java,swing,user-interface,awt,actionlistener,Java,Swing,User Interface,Awt,Actionlistener,getInt(int)是ResultSet的函数。 在下面的代码中,我无法理解为什么getInt(int)返回的integer不是字符串。事实上,我不确定到底出了什么问题。运行程序后,当我在文本字段中输入一个整数并点击“Show employee”(显示员工)按钮时,它会在装箱应该发生的行中显示一些错误(在案例的块中:“Show employee”(显示员工))。所以经过检查,我觉得这就是问题所在(虽然我不确定) 下面的程序不完整。它应该构建一个GUI,其中有6个按钮用于检索和编辑数据库中的数据

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

下面的程序不完整。它应该构建一个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
用这个

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));
}

装箱成字符串
-没有这样的thing@Eran我的意思是把
int
转换成
String
@LorisSecuro谢谢你给我找到了一个副本。它回答了我的问题。通常情况下,我们会解释代码在回答一个问题时会做什么。