java从数据库提取的数据怎么传给swing界面?刚开始接触swing,求帮改一下 6C

package 教师人事信息管理系统;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class text {
static final int WIDTH = 300;
static final int HEIGHT = 400;
static JTextField text1;
static JTextField text2;
static JTextField text3;
static String sql;
static Connection conn;
static Statement stmt;
static ResultSet rs;
static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=高校教师人事信息管理系统数据库;";

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("测试窗口");
    JPanel pane = new JPanel();
    frame.setContentPane(pane);
    text1 = new JTextField(10);
    text2 = new JTextField(10);
    text3 = new JTextField(10);
    JLabel label1 = new JLabel("教师编号 ");
    JButton button = new JButton("搜索");
    JLabel label2 = new JLabel("查询结果 ");
    pane.add(label1);
    pane.add(text1);
    pane.add(button);
    pane.add(label2);
    pane.add(text3);
    button.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent Event) {
            String str1 = text1.getText();// 从文本框中提取文本


            sql = "select 姓名  from 教师人事信息  where 学位='"+ str1 + "'";
            try {
                // 连接数据库
                conn = DriverManager.getConnection(url, "zhaoqixuan", "123456");
                // 建立Statement对象
                stmt = conn.createStatement();

                rs = stmt.executeQuery(sql);

                while (rs.next()) {


                    String str3 = rs.getString("姓名");
                    text3.setText(str3); // 将结果赋给第三个文本框




                }
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库连接失败");
            }


        }
    });
    frame.setSize(WIDTH, HEIGHT);
    frame.setVisible(true);
}

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐