您好,我正在尝试从数据库中获取记录,而我正在使用一个基于该选择的组合框我想从文本字段中的同一个表中获取相关记录..但不幸的是我得到了错误..谁能给我片段我在哪里代码错误?
try {
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testdatabase", "root", "ubuntu123");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select productname,quantity from productorder");
// ResultSet rs1 = st.executeQuery("Select quantity from productorder");
while (rs.next()) {
jComboBox1.addItem(rs.getString("productname"));
}
jComboBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
try {
String str = (String) jComboBox1.getSelectedItem();
//Statement st1 = con.createStatement();
PreparedStatement stmt = (PreparedStatement) con.prepareStatement("Select * from productorder where productname=?");
stmt.setString(1, str);
ResultSet rs1 = stmt.executeQuery();
jTextField2.setText(str);
Integer qty=rs1.getInt(1); // Error in this line at com.mylogin.Testingproductmaster$1.itemStateChanged(Testingproductmaster.java:58)
jTextField1.setText(qty.toString());
} catch (SQLException ex) {
Logger.getLogger(Testingproductmaster.class.getName()).log(Level.SEVERE, null, ex);
}
全栈跟踪
18 Apr, 2013 5:14:33 PM com.mylogin.Testingproductmaster$1 itemStateChanged
SEVERE: null
java.sql.SQLException: Invalid value for getInt() - 'TCS'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2725)
at com.mylogin.Testingproductmaster$1.itemStateChanged(Testingproductmaster.java:62)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1222)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1279)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1326)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:117)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:105)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:574)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:620)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:831)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6389)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3268)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:497)
at java.awt.Component.processEvent(Component.java:6154)
at java.awt.Container.processEvent(Container.java:2045)
at java.awt.Component.dispatchEventImpl(Component.java:4750)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4576)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
at java.awt.Container.dispatchEventImpl(Container.java:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2518)
at java.awt.Component.dispatchEvent(Component.java:4576)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.awt.EventQueue$2.run(EventQueue.java:629)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.awt.EventQueue$3.run(EventQueue.java:643)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)