java - 如何在 MySQL 数据中将 jlist 作为单个块插入
问题描述
我创建了一个 java 软件应用程序,其中有一个表单,其中包含很少的 JTextField 和一个 JList。JList 中的项目也由用户在同一页面中存在的 JTextField 的帮助下插入。我想在单击提交按钮时将 JTextField 和 JList 保存在 MySQL 数据库中。我已经成功地将 JTextField 数据插入到 MySQL 数据库表中,但问题是我不知道如何将 JList 保存在单个块中。只是为了练习,我尝试保存 JList 并为数据库中的每个项目保存新行。
有什么办法可以将整个 JList 添加到单个块中。
或者
如果不是,我如何将 JList 与 MySQL 中的剩余数据链接起来。
我知道我们一次只能问一个问题,但另一个问题是如何在 JTable 中以 JList 的形式将 JList 从数据库检索到 JTable 以及所有其他详细信息。
DefaultListModel dlm= new DefaultListModel();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String userInput= jTextField1.getText();
dlm.addElement(userInput);
jList1.setModel(dlm);
jTextField1.setText(null);
}
String item="";
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
for(int i = 0; i < jList1.getModel().getSize(); i++)
{ item = (String)jList1.getModel().getElementAt(i);
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/a","root","");
PreparedStatement stmt = conn.prepareStatement("insert into listt (listitem) values (?)");
stmt.setString(1,item);
stmt.execute();
conn.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "Done");
}
上面的代码有两个按钮,btn1 将 JTextField 数据添加到 JList,btn2 将 JList 项添加到数据库。它将每个项目保存在数据库的新行中,但我想将它保存在单个块中的单个行中,并与其他数据一起保存。
用于将数据从 MySQL 显示到 JList 和 jTable 的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/a","root","");
String query="SELECT listitem FROM listt;";
PreparedStatement prepstmt=conn.prepareStatement(query);
ResultSet rs=prepstmt.executeQuery();
while(rs.next()){
String item= rs.getString(1);
dlm.addElement(item);
jList1.setModel(dlm);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
当前 JTable 结果的图像。![ https://imgur.com/dPlnLVu]
Jtable 的代码在这里:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Update_table();
}
private void Update_table(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/ft","root","");
String query="SELECT * FROM ftt;";
PreparedStatement prepstmt=conn.prepareStatement(query);
ResultSet rs=prepstmt.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
listitem 列中的项目应该在每一行的 JList 中,并且 null 显示不显示 iit 是在将 jlist 值保存到数据库时在 Mysql 数据库中自动生成的。
其余的事情都解决了,但我仍然需要在 jTable 中生成 JList。
解决方案
String allitem="";
for(int i = 0;i<jList1.getModel().getSize();i++)
{
allitem = (String)jList1.getModel().getElementAt(i)+"::"+allitem;
}
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/a", "root", "");
PreparedStatement stmt = conn.prepareStatement("insert into listt (listitem) values (?)");
stmt.setString(1, allitem);
stmt.execute();
conn.close();
}catch(
Exception e)
{
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
试试这个
推荐阅读
- swift - 如何检查句子的语法或句法是否正确(Swift 中的简单语法检查)?
- python - TypeError:'int'对象不可迭代,同时迭代二维数组
- c++ - 怎么做才能不切断空格后的字符串
- macos - MacOS 应用程序或代码会使部分屏幕变黑?
- python - 使用 control.step_response 无法获得预期的输出
- django - 请求数据中的 DJANGO OPENCENSUS url 字段太长
- r - 如何使循环和附加更有效
- python - 将数据附加到python中的文件时重复数据
- arduino - ESP32 Heltec wifi LoRa,Waveshare e-Paper 4.2",如何通过 SPI 连接以及如何在显示屏上显示内容(使用哪个库)?
- r - 如何绘制贝叶斯套索贝塔系数