思路:将程序简单的分为了一个主要的菜单类和其他的五个功能类,实现对数据库的信息的增删改查。
缺点:写了大量重复代码,实现的功能却不多,将每个类的界面和功能都写到了一个方法里,类与类之间完全分开,没有一点关联性
涉及知识点:
1数据库的连接
2.executeUpdate与excuteQuery的区别
3.Statement与PreparedStatement的区别
4.swing销毁当前窗口的方法
public class Menu extends JFrame{
private JScrollPane scrollpane;
private JButton button1, button2, button3,button4,button5;
private JPanel panel;
public static void main(String[] args) {
new Menu().initView();
}
private void initView() {
this.setTitle("学生信息管理系统");
/*id=new JLabel("学号");
name=new JLabel("姓名");
school=new JLabel("学院");
date=new JLabel("入学日期");*/
button1=new JButton("添加");
button2=new JButton("修改");
button3=new JButton("查询");
button4=new JButton("删除");
button5=new JButton("浏览");
panel=new JPanel();
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
this.add(panel);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Add().view();
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Change().view();
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Ask();
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Delete();
}
});
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Look().view();
}
});
this.setSize(450, 450);
this.setLocation(150, 150);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Add extends JFrame{
private JLabel id,name,school,date;
private JTextField fieldid,fieldname,fieldschool,fielddate;
private JButton button1, button2;
private JPanel panelid,panelbutton,panelname,panelschool,paneldate;
private String driver;
private String url;
private String user;
private String pass;
public void view() {
id=new JLabel("学号");
name=new JLabel("姓名");
school=new JLabel("学院");
date=new JLabel("入学日期");
button1=new JButton("确认");
button2=new JButton("重置");
fieldid=new JTextField("",20);
fielddate=new JTextField("",20);
fieldname=new JTextField("",20);
fieldschool=new JTextField("",20);
panelbutton=new JPanel(new GridLayout(1,1));
panelid=new JPanel();
panelname=new JPanel();
panelschool=new JPanel();
paneldate=new JPanel();
panelbutton.add(button1);
panelbutton.add(button2);
panelid.add(id);
panelid.add(fieldid);
panelname.add(name);
panelname.add(fieldname);
panelschool.add(school);
panelschool.add(fieldschool);
paneldate.add(date);
paneldate.add(fielddate);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection conn=null;
String sql="insert into sd(id,name,schol,date)"+"values(?,?,?,?)";
try {
Properties props = new Properties();// 加载文件属性
props.load(new FileInputStream("src\\jdbc.properties"));// 创建一个文件输入流
driver = props.getProperty("driver");// 获取数据库驱动
url = props.getProperty("url");// 获取想要操作的数据库及其信息
user = props.getProperty("user");// 获取用户名
pass = props.getProperty("password");// 获取密码
}catch(Exception e1) {
e1.printStackTrace();
}
try {
Class.forName(driver);
System.out.println("数据库连接成功");
conn = DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,fieldid.getText());
ps.setString(2,fieldname.getText());
ps.setString(3,fieldschool.getText());
ps.setString(4,fielddate.getText());
ps.executeUpdate();
}catch(Exception e2){
e2.printStackTrace();
}finally {
try{
conn.close();
System.out.println("MySQL 关闭成功");
}catch (SQLException c){
System.out.println("MySQL 关闭失败 ");
c.printStackTrace();
}
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fieldid.setText("");
fieldname.setText("");
fieldschool.setText("");
fielddate.setText("");
}
});
this.setLayout(new GridLayout(9,1));
this.add(panelid);
this.add(panelname);
this.add(panelschool);
this.add(paneldate);
this.add(panelbutton);
this.setSize(350, 300);
this.setLocation(300, 150);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Delete extends JFrame{
JFrame f=new JFrame();
private String driver;
private String url;
private String user;
private String pass;
JLabel jlnumber = new JLabel("学号:");
JTextField jtnumber = new JTextField("",20);
JButton buttondelete = new JButton("删除");
JButton buttonreturn = new JButton("返回");
public Delete() {
JPanel jpnumber = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jpnumber.add(jlnumber);
jpnumber.add(jtnumber);
jpforbutton.add(buttondelete);
jpforbutton.add(buttonreturn);
buttondelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String number= jtnumber.getText();
Connection conn=null;
Resultset res=null;
Statement stat=null;
String sql="delete from sd where id='"+number+"'";
Properties props=new Properties();
try {
props.load(new FileInputStream("src\\jdbc.properties"));
driver = props.getProperty("driver");// 获取数据库驱动
url = props.getProperty("url");// 获取想要操作的数据库及其信息
user = props.getProperty("user");// 获取用户名
pass = props.getProperty("password");
conn=DriverManager.getConnection(url, user, pass);
stat=conn.createStatement();
stat.executeUpdate(sql);
}
catch(SQLException e1) {
e1.printStackTrace();
}
catch(EOFException e2) {
e2.printStackTrace();
}
catch(FileNotFoundException e3) {
e3.printStackTrace();
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
}
});
buttonreturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.dispose();//销毁当前窗口
}});
f.setTitle("删除学生信息");
f.setLayout(new GridLayout(9,1));
f.add(jpnumber);
f.add(jpforbutton);
f.setLocation(400,300);
f.setSize(350,300);
f.setVisible(true);
}
}
public class Change extends JFrame{
private JLabel id,name,school,date;
private JTextField fieldid,fieldname,fieldschool,fielddate;
private JButton button1, button2;
private JPanel panelid,panelbutton,panelname,panelschool,paneldate;
private String driver;
private String url;
private String user;
private String pass;
void view() {
id=new JLabel("学号");
name=new JLabel("姓名");
school=new JLabel("学院");
date=new JLabel("入学日期");
button1=new JButton("修改");
button2=new JButton("返回");
fieldid=new JTextField("",20);
fielddate=new JTextField("",20);
fieldname=new JTextField("",20);
fieldschool=new JTextField("",20);
panelbutton=new JPanel(new GridLayout(1,1));
panelid=new JPanel();
panelname=new JPanel();
panelschool=new JPanel();
paneldate=new JPanel();
panelbutton.add(button1);
panelbutton.add(button2);
panelid.add(id);
panelid.add(fieldid);
panelname.add(name);
panelname.add(fieldname);
panelschool.add(school);
panelschool.add(fieldschool);
paneldate.add(date);
paneldate.add(fielddate);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sid =fieldid.getText();
String sname=fieldname.getText();
String sschool=fieldschool.getText();
String sdate=fielddate.getText();
Connection conn=null;
ResultSet res=null;
Statement stat=null;
String sql="select id,name,schol,date from sd";
try {
Properties props=new Properties();
props.load(new FileInputStream("src\\jdbc.properties"));
driver=props.getProperty("driver");
url=props.getProperty("url");
user=props.getProperty("user");
pass=props.getProperty("password");
Class.forName(driver);
conn=DriverManager.getConnection(url, user, pass);
stat=conn.createStatement();
res=stat.executeQuery(sql);
while(res.next()) {
if(res.getString(1).equals(fieldid.getText())) {
try {
Class.forName(driver);
}
catch(Exception d) {
System.out.println("jdbc fall");
d.printStackTrace();
}
String sql1="update sd set name='"+sname
+"' where id='"+fieldid.getText()+"'";
String sql2="update sd set schol='"+sschool
+"' where id='"+fieldid.getText()+"'";
String sql3="update sd set date='"+sdate
+"' where id='"+fieldid.getText()+"'";
try {
conn=DriverManager.getConnection(url, user, pass);
stat=conn.createStatement();
stat.executeUpdate(sql1);
stat.executeUpdate(sql2);
stat.executeUpdate(sql3);
}
catch(SQLException g) {
System.out.println("数据库更新失败");
g.printStackTrace();
}finally {
stat.close();
conn.close();
}
}
}
}catch(Exception e1) {
e1.printStackTrace();
}
finally {
try {
conn.close();
}catch(SQLException ar) {
ar.printStackTrace();
}
}
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Menu menu=new Menu();
}
});
this.setTitle("修改学生信息");
this.setLayout(new GridLayout(9,1));
this.add(panelid);
this.add(panelname);
this.add(panelschool);
this.add(paneldate);
this.add(panelbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Ask extends JFrame{
JLabel jlnumber = new JLabel("学号:");
JLabel jlname = new JLabel("姓名:");
JLabel jlbirthday = new JLabel("入学日期:");
JLabel jldepartment = new JLabel("学院:");
JTextField jtnumber = new JTextField("",20);
JTextField jname = new JTextField("",20);
JTextField jbirthday =new JTextField("",20);
JTextField jdepartment =new JTextField("",20);
JButton buttonask = new JButton("查询");
JButton buttonreturn = new JButton("返回");
public Ask() {
JPanel jpnumber = new JPanel();
JPanel jpname = new JPanel();
JPanel jpbirthday = new JPanel();
JPanel jpdepartment = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jpnumber.add(jlnumber);
jpnumber.add(jtnumber);
jpname.add(jlname);
jpname.add(jname);
jpdepartment.add(jldepartment);
jpdepartment.add(jdepartment);
jpbirthday.add(jlbirthday);
jpbirthday.add(jbirthday);
jpforbutton.add(buttonask);
jpforbutton.add(buttonreturn);
buttonask.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Connection conn = null;
ResultSet res = null;
Statement stat = null;
String sql = "SELECT id,name,schol,date from sd;";
try{
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception d){
System.out.println("jdbc fall");
d.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=GMT","root","980204");
stat=conn.createStatement();
res=stat.executeQuery(sql);
while (res.next())
{
if (res.getString(1).equals(jtnumber.getText()))
{
jname.setText(res.getString(2));
jdepartment.setText(res.getString(3));
jbirthday.setText(res.getString(4));
break;
}
}
}catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
try{
conn.close();
}catch(SQLException ar){
ar.printStackTrace();
}
}}}
);
buttonreturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Menu();
}
});
this.setTitle("查询学生信息");
this.setLayout(new GridLayout(9,1));
this.add(jpnumber);
this.add(jpname);
this.add(jpbirthday);
this.add(jpdepartment);
this.add(jpforbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
}
}
public class Look extends JFrame{
private JTextField textField;
private JButton button;
private JScrollPane jscrollpane;
private JPanel panel;
private JTable jtable;
Vector columnNames = null;
Vector rowData = null;
private String driver;
private String url;
private String user;
private String pass;
public void view(){
textField=new JTextField(20);
button=new JButton("关闭");
Connection conn=null;
PreparedStatement ps=null;
ResultSet res=null;
columnNames=new Vector();
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("学院");
columnNames.add("入学日期");
rowData =new Vector<>();
jtable=new JTable(rowData,columnNames);
jscrollpane =new JScrollPane(jtable);
try{
Properties prop=new Properties();
prop.load(new FileInputStream("src\\jdbc.properties"));
driver=prop.getProperty("driver");
url=prop.getProperty("url");
user=prop.getProperty("user");
pass=prop.getProperty("password");
}
catch(IOException e1){
e1.printStackTrace();
}
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, pass);
ps=conn.prepareStatement("select * from sd");
res=ps.executeQuery();
while (res.next())
{
Vector hang = new Vector();
hang.add(res.getString(1));
hang.add(res.getString(2));
hang.add(res.getString(3));
hang.add(res.getString(4));
rowData.add(hang);
}
}catch(SQLException e2) {
e2.printStackTrace();
}catch(Exception e3) {
e3.printStackTrace();
}finally{
try{
res.close();
ps.close();
conn.close();
System.out.println("close ok");
}catch (SQLException o){
o.printStackTrace();
System.out.println("go die 2");
}}
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
jtable = new JTable(rowData,columnNames);
jscrollpane = new JScrollPane(jtable);
this.add(jscrollpane);
this.setTitle("浏览学生信息");
this.setLayout(new GridLayout(2,5));
// this.add(button);
this.setLocation(300,300);
this.setSize(500,300);
this.setVisible(true);
this.setResizable(false);
}
}