Java - 获取所选列表项的值 [英] Java - get the value of selected list item

查看:109
本文介绍了Java - 获取所选列表项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何获得列表的价值。我想在数据库表中显示值。

这是我的两个列表,从列表中选择这两个值后(标记为)我想将这两个元素(字符串)插入到我的数据库中在我的代码中的这个帖子:





Hi guys,

how can I get the value of an list. I want to show the value in a database table.
This are my two lists and after selecting these two values from the list (as marked) I want to insert these two elements(strings) into my database at this postition in my code:


JButton btnNewButton_2 = new JButton("Save Lesson");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try { 
    java.sql.Connection con = DriverManager.getConnection       ("jdbc:mysql://localhost:3306/LessonDB","root",""); 
String query = "INSERT INTO Teach(Prof,Laborname,Room,Day,Time) VALUES(?,?,?,?,?)";
        PreparedStatement pst = con.prepareStatement(query);
                pst.setString(1, txtFieldName.getText());
                pst.setString(2, txtFieldLaborname.getText());
                pst.setInt(3, Integer.parseInt(txtFieldRoom.getText()));
                pst.setString(4, (String) listDay.getSelectedValue());
                pst.setString(5, (String) listTime.getSelectedValue());
                pst.execute();
                pst.close();


                JOptionPane.showMessageDialog(null, "Succssesfull");


            }

            catch(Exception e1) {

                JOptionPane.showMessageDialog(null, "Wrong");


            }





但我没有错误或类似的东西。什么都没发生。现在输出。



我尝试了什么:



我试过了with



But I get no error or something like that. Just nothing happens. Now output.

What I have tried:

I tried it with

pst.setString(4, (String) listDay.getSelectedValue());
pst.setString(5, (String) listTime.getSelectedValue());



但这不起作用


but this does not work

推荐答案

首先,不要假设您的解析函数和强制转换将提供您期望的值。单独执行这些操作并检查结果是否有效。其次,检查执行语句的返回值: PreparedStatement.execute (Java Platform SE 7) [ ^ ]。
Firstly, do not assume that your parse functions and casts will provide the values you expect. Do those separately and check that the results are valid. Secondly, check the return value of your execute statement: PreparedStatement.execute (Java Platform SE 7 )[^].


这篇关于Java - 获取所选列表项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆