Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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");


            }


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
Posted
Updated 25-Jun-18 1:35am

1 solution

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 )[^].
 
Share this answer
 
Comments
Member 13814158 25-Jun-18 7:01am    
@Richard Mac Cutchan , thx for your answer. When I do it without a parse like this pst.setObject(4, listDay.getSelectedValue());
pst.setObject(5, listTime.getSelectedValue()); does not work too
Richard MacCutchan 25-Jun-18 7:16am    
Then do what I suggested and actually check the values that you are trying to send to the database. If necessary use your debugger to see exactly what is happening in your code.
Maciej Los 26-Jun-18 4:01am    
5ed!
Richard MacCutchan 26-Jun-18 4:17am    
:thumbsup:
Member 13814158 3-Jul-18 16:07pm    
?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900