[英]Java - get the value of selected list item
如何获取列表的值?
这是我的两个列表,从列表中选择这两个值(如标记所示)后,我想在代码中的此位置将这两个元素(字符串)插入数据库中:
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, txtFieldName.getText());
pst.setString(2, txtFieldNumber.getText());
// pst.setString(3, listDay.);
在最后一个位置,我想选择用户在UI两个列表中选择的值:
JButton btnNewButton_2 = new JButton("Speichern"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
try {JButton btnNewButton_2 = new JButton("Speichern"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
try {java.sql.Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/dblabor","root",""); String query = "INSERT INTO Teach(Prof,Laborname,Room,Day,Time) VALUES(?,?,?,?,?)"; PreparedStatement pst = con.prepareStatement(query);
java.sql.Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/dblabor","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");
}
`
谢谢大家。
如何获取列表的值?
有人告诉您只需使用:
Object value = list.getSelectedValue();
System.out.println(value);
您显示值了吗?
您是否阅读了有关JDBC数据库访问的教程?
我相信你需要使用executeUpate()
的方法PreparedStatement
更改数据。
如果使用的是Swing,请尝试使用javax.swing.JList#getSelectedValue
方法从JList对象获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.