import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class win {
public static void main(String args[]) {
JFrame frame = new JFrame("软工223-2218140321-蔡昌昊(原创)");
frame.setBounds(400, 300, 800, 400);
FlowLayout flowLayout = new FlowLayout();
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.setLayout(flowLayout);
scrollPane.setPreferredSize(new Dimension(700, 100));
textArea.setLineWrap(true);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JButton button1 = new JButton("查看信息");
JButton button2 = new JButton("随机查询");
JButton button3 = new JButton("模糊查询");
JButton button4 = new JButton("条件查询");
JButton button5 = new JButton("增添信息");
JButton button6 = new JButton("删除信息");
JButton button7 = new JButton("修改信息");
textArea.setVisible(true);
frame.add(scrollPane);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);//至少可以按照两个字段查询,能够模糊查询
frame.add(button6);
frame.add(button7);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
String t = textArea.getText();
queryByFuzzy(t);
}
}
});
}
public static void queryByFuzzy(String keyword){
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB("mybase", "root", "123456");
if (con == null) {
System.out.println("连接数据库失败!");
return ;
}
String c1 = "库存 > 0";//条件1
String c2 = "图书名 Like '%" + keyword + "%'"; //模糊查询条件2,这里只是一个示例,实际应用中需要根据实际情况进行模糊匹配处理
String sqlStr = "select * from 图书 where " + c1 + " and " + c2 + " order by 价格";
try {
sql = con.createStatement();
rs = sql.executeQuery(sqlStr);
while (rs.next()) {
String 图书编号 = rs.getString("图书编号");
String 图书名 = rs.getString("图书名");
String 作者 = rs.getString("作者");
String 类别 = rs.getString("类别");
String 价格 = rs.getString("价格");
String 库存 = rs.getString("库存");
textArea.setText(textArea.getText() + "图书编号: " + 图书编号 + ", 图书名: " + 图书名 + ", 作者: " +
作者 + ", 类别: " + 类别 + ", 价格: " + 价格 +
", 库存: " + 库存 + "\n");
}
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
我都 textArea.setText(textArea.getText()这个一直标红,求告知如何将下面的信息输出到我都textArea