实现标准:点击按钮后,会自动显示数据库内容
最好有讲解太感谢了急急急😭😭😭
2条回答 默认 最新
- 404警告 2021-07-03 13:13关注
所需数据库驱动如下代码是操作mysql数据库:
package ui; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { new Test(); } public Test() { initialize(); } private void initialize() { JFrame frame = new JFrame(); frame.setTitle("连接数据库"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setBackground(new Color(176, 196, 222)); frame.setBounds(100, 100, 450, 300); frame.getContentPane().setLayout(null); // 窗口显示在页面中间 frame.setLocationRelativeTo(null); // 窗口不允许调整大小 frame.setResizable(false); JButton button = new JButton("连接"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/teachersystem_db", "root", "root"); if (conn != null) { JOptionPane.showMessageDialog(null, "连接成功", "连接提示", JOptionPane.INFORMATION_MESSAGE); frame.dispose(); // 关闭当前页面 } else { JOptionPane.showMessageDialog(null, "连接失败", "连接提示", JOptionPane.ERROR_MESSAGE); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); button.setBackground(Color.LIGHT_GRAY); button.setBounds(155, 213, 113, 27); frame.getContentPane().add(button); frame.setVisible(true); } }
运行效果:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 对于知识的学以致用的解释
- ¥50 三种调度算法报错 有实例
- ¥15 关于#python#的问题,请各位专家解答!
- ¥200 询问:python实现大地主题正反算的程序设计,有偿
- ¥15 smptlib使用465端口发送邮件失败
- ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存
- ¥15 CST保存项目时失败