getInt(int)
是ResultSet
的函数。
在以下代码中,我无法理解为什么integer
返回的getInt(int)
不属于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
答案 0 :(得分:0)
使用此
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));
}