无法从JTextField中的数据库中获取记录

时间:2013-04-17 10:39:40

标签: java mysql jtextfield jcombobox selecteditem

你好我在这里尝试从数据库中获取记录,而我正在使用一个基于该选择的组合框我希望从文本域中的同一个表中获取相关记录..但不幸的是我得到了错误..可能有人给我片段我在代码中的错误在哪里?

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

Full StackTrace

  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)

1 个答案:

答案 0 :(得分:0)

请务必推进ResultSet JComboBox

中的ItemListener光标
ResultSet rs1 = stmt.executeQuery();

if (rs1.next()) {
    Integer qty = rs1.getInt(1);
    ...
}

请注意,这将使用检索到的第一个记录。如果WHERE不唯一,则需要更具体的productname子句 才能从数据库查询中返回单个记录。

编辑:来自此错误

java.sql.SQLException: Invalid value for getInt() - 'TCS'

在索引1列返回的值实际上不是有效的整数值(看起来是文本类型)。检查调用SQL

Select * from productorder where productname='<Some Product Here>'

在您的SQL客户端中,并确保您正在读取正确的索引以匹配您的qty整数变量。

如果数量是唯一要检索的数据,则可以优化查询以仅返回该信息

Select quantity from productorder where productname=?