getInt(int)
is a function of ResultSet
.
In the following code i am unable to understand why the integer
returned by the getInt(int)
is not into a String
. Actually i am exactly not sure what is going wrong. After running the program , when i enter an integer in the text field and hit the 'Show employee' button, it shows some error in the line where the boxing should be taking place (inside the block of case: "Show emplyee"
). So after examination, i feel that is the problem (although i am not sure).
The program below is incomplete. It is supposed to build a GUI which has 6 buttons to retrieve and edit data in a database(datab4) and a textfield to enter inputs and also show outputs. I am new to Java Swing. Would be great help if someone could correct me.
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);
}
}
}
Here is the error:
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