JAVA加上界面实现JDBC数据库的增删改
一、首先下载MySQL数据库
输入网址:
https://downloads.mysql.com/archives/community/
请求下载页,然后在出现的页面中选择Windows (x86, 64-bit), ZIP Archive 8.0.15(184.1M),然后单击Download(下载)按钮。
下载完解压后如同所示:
首次启动之前必须进行安全初始化。必须用管理员身份启动命令行窗口找到cmd.exe(window+r打开),然后在命令行进入MySQL安装目录的bin子目录,键入
mysqld --initialize-insecure
命令,回车确认。
初始化后,必须用管理员身份启动命令行窗口,然后进入MySQL安装目录的bin子目录下键入:
mysqld 或 mysqld -nt
回车确认,启动MySQL数据库服务器
二、下载navicat for mysql
下载链接:http://www.navicat.com.cn/download/navicat-for-mysql
下载完之后,打开软件,记住使用软件时前面打开的cmd.exe不要关掉
新建连接:
输入你取的连接名,再把你前面设的root的密码输入,点击连接测试
如果出现如下画面,表示连接成功,然后点击确定。
要是出现如下情况:
解决方案如下:
1.打开命令行cmd,进入MySQL的bin目录,然后输入mysql -u root -p,输入密码
2.然后输入
ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘password(这是指你的原先的密码)’ PASSWORD EXPIRE NEVER; #修改加密规则
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘password(你想要改为的密码)’; #更新一下用户的密码
FLUSH PRIVILEGES; #刷新权限
3.输入刚刚修改的密码,再次测试连接,惊喜地发现连接成功
三、下载JDBC-MySQL数据库连接器
下载链接:https://downloads.mysql.com/archives/c-j/
这里我选择的是8.0.15版本
下载完成后解压到自己想要的目录下
四、开始创建项目
java项目文件夹:
1.使用mysql创建数据库:
create database student;//创建数据库
//创建表
CREATE TABLE IF NOT EXISTS student(
StuID int(11) NOT NULL ,
Cid int(11) NOT NULL,
Sname varchar(20) NOT NULL,
Sex char(2) NOT NULL,
PRIMARY KEY (StuID));
//插入值
INSERT INTO student (StuID, Cid, Sname, Sex) VALUES
(1, '2', '张梅', '女'),
(2, '3', '王兰', '女'),
(3, '4', '李竹', '男'),
(4, '5', '赵菊', '女');
2.使用Eclipse导入jar包并创建连接;
创建一个java项目,我这里项目名称为A,首先建一个空的文件夹lib,将下载的JDBC-MySQL数据库连接器复制到文件夹里(我这里是)
然后右键-Build Path–Add External Archiver–选择JDBC驱动jar的存放位置–点击确定;
我这里使用包名为org.clarck.jdbc(你们可以自己定义包名)
DataSource.java(连接数据库)
package org.claarck.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DataSource {
public static Connection getConnection() {// 用这个方法获取mysql的连接
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");// 加载驱动类
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode = true&characterEncoding = utf-8&useSSL = false&serverTimezone = GMT", "root", "password");
// (url数据库的IP地址,user数据库用户名,password数据库密码)
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void close(Connection con, Statement st, ResultSet rs) throws ClassNotFoundException, SQLException{
if(rs != null) {
rs.close();
}
if(st != null) {
st.close();
}
if(con != null) {
con.close();
}
}
public static void main(String[] args) {// 测试数据库是否连接成功的方法
Connection conn = DataSource.getConnection();
System.out.println(conn);
}
}
这段代码老师给的是下面这段,但是在我的电脑上运行出错你们可以试试
package org.clarck.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException
;import java.sql.Statement;
public class DataSource {
private final String url = "jdbc:mysql://localhost:3306/student?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true";
private final String user = "root";
private final String password = "password";
//定义一个数据库的链接变量用于存放对象
public Connection getCon() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url,user,password);
return con;
}
public void close(Connection con, Statement st, ResultSet rs) throws ClassNotFoundException, SQLException{
if(rs != null) {
rs.close();
}
if(st != null) {
st.close();
}
if(con != null) {
con.close();
}
}
}
Student.java(实体类,为了便于操作)
package org.claarck.jdbc;
public class Student {
private int sid; // 学生编号
private String name; // 姓名
private String sex; // 性别
private int cid; // 班级编号
public Student() {
}
// 用于添加使用
public Student(String name, String sex, int cid) {
super();
this.name = name;
this.sex = sex;
this.cid = cid;
}
// 用于修改使用
public Student(int sid, String name, String sex, int cid) {
super();
this.sid = sid;
this.name = name;
this.sex = sex;
this.cid = cid;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
}
StudentDAO.java(用于操作数据库的增删改查)
package org.claarck.jdbc;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class StudentDAO extends DataSource {
// 添加的方法
public void add(Student student) throws ClassNotFoundException, SQLException {
Connection con = null;
PreparedStatement ps = null; // 预编译对象
String sql = "insert into Student(cid, sname, sex) values(?,?,?)";
try {
con = this.getConnection();
ps = con.prepareStatement(sql);
// 设置占位符对应的参数
ps.setInt(1, student.getCid());
ps.setString(2, student.getName());
ps.setString(3, student.getSex());
ps.executeUpdate();
} finally {
this.close(con, ps, null);
}
}
// 修改
public void update(Student student) throws ClassNotFoundException, SQLException {
Connection con = null;
PreparedStatement ps = null; // 预编译对象
String sql = "update Student set Cid = ?, Sname = ?, Sex = ? where StuID = ?";
try {
con = this.getConnection();
ps = con.prepareStatement(sql);
// 设置占位符对应的参数
ps.setInt(1, student.getCid());
ps.setString(2, student.getName());
ps.setString(3, student.getSex());
ps.setInt(4, student.getSid());
ps.executeUpdate();
} finally {
this.close(con, ps, null);
}
}
// 删除
public Student delete(int sid) throws ClassNotFoundException, SQLException {
Connection con = null;
PreparedStatement ps = null; // 预编译对象
ResultSet rs = null;
String sql = "delete from Student where StuID = ?";
String sql1 = "select * from Student where StuID = ?";
Student s = null;
try {
con = this.getConnection();
ps = con.prepareStatement(sql1);
// 设置占位符对应的参数
ps.setInt(1, sid);
rs = ps.executeQuery();
while (rs.next()) {
s = new Student(rs.getInt(1), rs.getString(3), rs.getString(4), rs.getInt(2));
}
ps = con.prepareStatement(sql);
// 设置占位符对应的参数
ps.setInt(1, sid);
ps.executeUpdate();
} finally {
this.close(con, ps, null);
return s;
}
}
// 查询
public List<Student> findAll() throws ClassNotFoundException, SQLException {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Student> list = new ArrayList<Student>();
String sql = "select * from Student";
try {
con = this.getConnection();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Student s = new Student(rs.getInt(1), rs.getString(3), rs.getString(4), rs.getInt(2));
list.add(s);
}
} finally {
this.close(con, ps, rs);
}
return list;
}
public Student findByName(String name) throws ClassNotFoundException, SQLException {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from Student where Sname = ?";
Student s = null;
try {
con = this.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
while (rs.next()) {
s = new Student(rs.getInt(1), rs.getString(3), rs.getString(4), rs.getInt(2));
}
} finally {
this.close(con, ps, rs);
}
return s;
}
}
yemian.java(建立窗口,从窗口打开)
package org.claarck.jdbc;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class yemian extends JFrame {
public yemian() {
init();// 初始化界面
}
void init() {
JFrame f = new JFrame("学生");// 创建一个窗口
Container con = f.getContentPane();
con.setBackground(Color.white);
//con.setLayout(new FlowLayout());//流布局
f.setBounds(500, 100, 380, 450);// 设置窗口大小
f.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f.setLayout(new FlowLayout());//浮动布局
f.setVisible(true);// 显示窗口
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton add = new JButton("增加");// 创建增加按钮
add.setBounds(50, 300, 60, 25);// 设置按钮大小
JButton delete = new JButton("删除");// 创建删除按钮
delete.setBounds(120, 300, 60, 25);
JButton update = new JButton("修改");// 创建修改按钮
update.setBounds(190, 300, 60, 25);
JButton find = new JButton("按名字查询");// 创建按名字查询按钮
find.setBounds(50, 330, 100, 25);
JButton findall = new JButton("查询所有");// 创建查询所有按钮
findall.setBounds(160, 330, 90, 25);
JButton b = new JButton("");// 多设置一个按钮其他按钮可以显示不被遮挡
b.setContentAreaFilled(false);// 是否填充
b.setBorderPainted(false);// 是否画边框
b.setEnabled(false);// 不可按
f.add(add);
f.add(delete);
f.add(find);
f.add(update);
f.add(findall);
f.add(b);
JTextArea area = new JTextArea();
area.setBounds(350, 150, 300, 150);
JScrollPane jsp = new JScrollPane(area);
try {
Student student = new Student();
StudentDAO dao = new StudentDAO();
List<Student> list = new ArrayList<Student>();
list = dao.findAll();
area.append("| StuID" + "\t" + "| Cid" + "\t" + "| Sname" + "\t" + "| Sex |\n");
area.append("|---------------------|---------------------|----------------------|-------|\n");
for (Student Student : list) {
area.append("| " + Student.getSid() + "\t| " + Student.getCid() + "\t| " + Student.getName()
+ "\t| " + Student.getSex() + " |\t" + "\n");
area.append("|---------------------|---------------------|----------------------|-------|\n");
}
} catch (Exception e2) {
}
f.add(area);
add.addActionListener(new AbstractAction() {// 添加按钮的点击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
f.setVisible(false);
TestAdd d = new TestAdd();
}
});
delete.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
f.setVisible(false);
TestDelete d = new TestDelete();
}
});
update.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
f.setVisible(false);
TestUpdate d = new TestUpdate();
}
});
find.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
f.setVisible(false);
TestFindByName d = new TestFindByName();
}
});
findall.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
f.setVisible(false);
TestFindAll d = new TestFindAll();
}
});
}
}
TestAdd.java(用于数据库的增加)
package org.claarck.jdbc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TestAdd {
public TestAdd() {
init();// 添加信息界面初始化
}
void init() {
JFrame f = new JFrame("添加学生信息");// 添加学生信息的窗口
f.setLayout(null);
f.setBounds(500, 100, 700, 540);// 窗口大小
f.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);// 显示窗口
JLabel Cid = new JLabel("Cid:");// 创建文本
Cid.setBounds(250, 150, 100, 25);
f.add(Cid);
JLabel Sname = new JLabel("Sname:");
Sname.setBounds(250, 200, 100, 25);
f.add(Sname);
JLabel Sex = new JLabel("Sex:");
Sex.setBounds(250, 250, 100, 25);
f.add(Sex);
JTextField 班级 = new JTextField();// 创建文本框
班级.setBounds(320, 150, 150, 25);
f.add(班级);
JTextField 姓名 = new JTextField();
姓名.setBounds(320, 200, 150, 25);
f.add(姓名);
JTextField 性别 = new JTextField();
性别.setBounds(320, 250, 150, 25);
f.add(性别);
JButton add = new JButton("确定添加");
add.setBounds(350, 350, 90, 25);
f.add(add);
add.addActionListener(new ActionListener() {// 设置添加的监听事件
@Override
public void actionPerformed(ActionEvent e) {
try {
String name = 姓名.getText();
String sex = 性别.getText();
Integer cid = Integer.parseInt(班级.getText());
Student student = new Student(name, sex, cid);
StudentDAO dao = new StudentDAO();
try {
dao.add(student);
JOptionPane.showMessageDialog(f, "添加完成");
f.setVisible(false);
yemian run = new yemian();
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
} finally {
System.out.println("发生了添加事件");
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(f, "添加失败");
f.setVisible(false);
yemian run = new yemian();
}
}
});
}
}
TestDelete.java(用于数据库的删除)
package org.claarck.jdbc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TestDelete {
public TestDelete() {
init();// 删除信息界面初始化
}
void init() {
JFrame f = new JFrame("删除学生信息");// 删除学生信息的窗口
f.setLayout(null);
f.setBounds(500, 100, 700, 640);// 窗口大小
f.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);// 显示窗口
JLabel Sid = new JLabel("Sid(要删除学生的学号):");// 创建文本
Sid.setBounds(150, 100, 170, 25);
f.add(Sid);
JTextField 学号 = new JTextField();// 创建文本框
学号.setBounds(320, 100, 150, 25);
f.add(学号);
JButton delete = new JButton("确定删除");
delete.setBounds(350, 350, 90, 25);
f.add(delete);
delete.addActionListener(new ActionListener() {// 设置删除的监听事件
@Override
public void actionPerformed(ActionEvent e) {
try {
Integer sid = Integer.parseInt(学号.getText());
Student student = new Student();
StudentDAO dao = new StudentDAO();
try {
student = dao.delete(sid);
if (student != null) {
JOptionPane.showMessageDialog(f, "删除完成");
f.setVisible(false);
yemian run = new yemian();
}
else {
JOptionPane.showMessageDialog(f, "删除失败");
f.setVisible(false);
yemian run = new yemian();
}
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
} finally {
System.out.println("发生了删除事件");
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(f, "删除失败");
f.setVisible(false);
yemian run = new yemian();
}
}
});
}
}
TestUpdate.java(用于数据库的修改)
package org.claarck.jdbc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TestUpdate {
public TestUpdate() {
init();// 修改信息界面初始化
}
void init() {
JFrame f = new JFrame("修改学生信息");// 修改学生信息的窗口
f.setLayout(null);
f.setBounds(500, 100, 700, 640);// 窗口大小
f.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);// 显示窗口
JLabel Sid = new JLabel("Sid(输入要修改学生的学号):");// 创建文本
Sid.setBounds(150, 100, 170, 25);
f.add(Sid);
JLabel Cid = new JLabel("Cid(将班级改为):");// 创建文本
Cid.setBounds(150, 150, 170, 25);
f.add(Cid);
JLabel Sname = new JLabel("Sname(将姓名改为):");
Sname.setBounds(150, 200, 170, 25);
f.add(Sname);
JLabel Sex = new JLabel("Sex(将性别改为):");
Sex.setBounds(150, 250, 170, 25);
f.add(Sex);
JTextField 学号 = new JTextField();// 创建文本框
学号.setBounds(320, 100, 150, 25);
f.add(学号);
JTextField 班级 = new JTextField();// 创建文本框
班级.setBounds(320, 150, 150, 25);
f.add(班级);
JTextField 姓名 = new JTextField();
姓名.setBounds(320, 200, 150, 25);
f.add(姓名);
JTextField 性别 = new JTextField();
性别.setBounds(320, 250, 150, 25);
f.add(性别);
JButton update = new JButton("确定修改");
update.setBounds(350, 350, 90, 25);
f.add(update);
update.addActionListener(new ActionListener() {// 设置修改的监听事件
@Override
public void actionPerformed(ActionEvent e) {
try {
String name = 姓名.getText();
String sex = 性别.getText();
Integer sid = Integer.parseInt(学号.getText());
Integer cid = Integer.parseInt(班级.getText());
Student student = new Student(sid,name, sex, cid);
StudentDAO dao = new StudentDAO();
try {
dao.update(student);
JOptionPane.showMessageDialog(f, "修改完成");
f.setVisible(false);
yemian run = new yemian();
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
} finally {
System.out.println("发生了修改事件");
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(f, "修改失败");
f.setVisible(false);
yemian run = new yemian();
}
}
});
}
}
TestFindAll.java(用于数据库的查询)
package org.claarck.jdbc;
import javax.swing.*;
import com.mysql.cj.xdevapi.Statement;
import java.awt.*;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class TestFindAll {
public TestFindAll() {
init();// 查询信息界面初始化
}
void init() {
JFrame f = new JFrame("查询所有学生信息");// 查询所有学生信息的窗口
f.setBounds(500, 100, 380, 450);// 窗口大小
f.setLayout(new FlowLayout());
f.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);// 显示窗口
try {
Student student = new Student();
StudentDAO dao = new StudentDAO();
List<Student> list = new ArrayList<Student>();
list = dao.findAll();
JTextArea area = new JTextArea();
area.setBounds(50, 150, 300, 150);
JScrollPane jsp = new JScrollPane(area);
area.append("| StuID" + "\t" + "| Cid" + "\t" + "| Sname" + "\t" + "| Sex |\n");
area.append("|---------------------|---------------------|----------------------|-------|\n");
for (Student Student : list) {
area.append("| " + Student.getSid() + "\t| " + Student.getCid() + "\t| " + Student.getName()
+ "\t| " + Student.getSex() + " |\t" + "\n");
area.append("|---------------------|---------------------|----------------------|-------|\n");
}
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
}
});
JButton findall = new JButton("返回");
findall.setBounds(100, 340, 100, 25);
f.add(area);
f.add(findall);
findall.addActionListener(new ActionListener() {// 设置查询的监听事件
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f,"查询完成");
f.setVisible(false);
yemian run = new yemian();
}
});
} catch (Exception e2) {
JOptionPane.showMessageDialog(f,"查询失败");
f.setVisible(false);
yemian run = new yemian();
}
}
}
TestFindByName.java(用于数据库的条件查询)
package org.claarck.jdbc;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestFindByName {
public TestFindByName() {
init();// 查询信息界面初始化
}
void init() {
JFrame f = new JFrame("查询学生信息");// 查询学生信息的窗口
f.setLayout(null);
f.setBounds(500, 100, 700, 640);// 窗口大小
f.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);// 显示窗口
JLabel Sname = new JLabel("Sname:");// 创建文本
Sname.setBounds(250, 100, 100, 25);
f.add(Sname);
JTextField 姓名 = new JTextField();// 创建文本框
姓名.setBounds(320, 100, 150, 25);
f.add(姓名);
JButton sel = new JButton("确定查询");
sel.setBounds(350, 350, 90, 25);
f.add(sel);
JFrame f1 = new JFrame("查询学生信息");// 查询学生信息的窗口
f1.setLayout(null);
f1.setBounds(280, 100, 380, 330);// 窗口大小
f1.setLayout(new FlowLayout());
f1.setLocationRelativeTo(null);// 把窗口设置到屏幕中心
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sel.addActionListener(new ActionListener() {// 设置查询的监听事件
@Override
public void actionPerformed(ActionEvent e) {
try {
String sname = 姓名.getText();
Student Student = new Student();
StudentDAO dao = new StudentDAO();
try {
Student = dao.findByName(sname);
f.setVisible(false);
f1.setVisible(true);// 显示窗口
JTextArea area = new JTextArea();
area.setBounds(30, 50, 300, 150);
JScrollPane jsp = new JScrollPane(area);
area.append("| StuID" + "\t" + "| Cid" + "\t" + "| Sname" + "\t" + "| Sex |\n");
area.append("|---------------------|---------------------|----------------------|-------|\n");
area.append("| " + Student.getSid() + "\t| " + Student.getCid() + "\t| "
+ Student.getName() + "\t| " + Student.getSex() + " |\t" + "\n");
area.append("|---------------------|---------------------|----------------------|-------|\n");
JButton findall = new JButton("返回");
findall.setBounds(100, 240, 100, 25);
f1.add(area);
f1.add(findall);
findall.addActionListener(new ActionListener() {// 设置查询的监听事件
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f, "查询完成");
f1.setVisible(false);
yemian run = new yemian();
}
});
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
} finally {
System.out.println("发生了查询事件");
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(f1, "查询失败");
f1.setVisible(false);
yemian run = new yemian();
}
}
});
}
}
Test.java(主函数)
package org.claarck.jdbc;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
yemian y = new yemian();
}
}