java写的学生管理系统
今天在逛论坛的时候看到有个大兄弟放了个C语言写的 学生管理系统.然后就心血来潮正好最近刚学了jdbc 于是就做了个学生管理系统.
不过我是 将每个功能都独立封装在了一个类中.
我gui并没有学的太深.
所以把它们整合在一起于我而言还是相当有难度的.
再有:
就是程序里面用的是本地的mysql
库名叫:jichangxiu
表名叫:stu
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.gjt.mm.mysql.Driver;
public class Test01
{
public static void main(String[] args)
{
new addFrame();
new changeFrame();
new deleteFrame();
new selectFrame();
new showFrame();
}
}
//修改某个学生信息
class changeFrame extends JFrame
{
changeFrame()
{
super("修改某个学生信息");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(300, 300, 400, 450);
this.setLayout(new FlowLayout());
this.setResizable(false);
init();
this.setVisible(true);
}
private void init()
{
add(new myJpanel());
}
class myJpanel extends JPanel
{
myJpanel()
{
this.setPreferredSize(new Dimension(370, 552));
init();
}
private void init()
{
JPanel jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(370, 30));
jp1.add(new JLabel("学号:"));
JTextField id = new JTextField(20);
jp1.add(id);
add(jp1);
JPanel jp2 = new JPanel();
jp2.setPreferredSize(new Dimension(370, 30));
jp2.add(new JLabel("姓名:"));
JTextField name = new JTextField(20);
jp2.add(name);
add(jp2);
JButton tijiao1 = new JButton("提交");
add(tijiao1);
JButton reset = new JButton("重置");
add(reset);
add(new JLabel("----------------------------------------------------------------------------------"));
JPanel jp5 = new JPanel();
jp5.setPreferredSize(new Dimension(370, 170));
jp5.add(new JLabel("学号:"));
JTextField newid = new JTextField(27);
// newid.setEditable(false);
jp5.add(newid);
jp5.add(new JLabel("姓名:"));
JTextField newname = new JTextField(27);
// newname.setEditable(false);
jp5.add(newname);
jp5.add(new JLabel("性别:"));
JTextField newsex = new JTextField(27);
// newsex.setEditable(false);
jp5.add(newsex);
jp5.add(new JLabel("年龄:"));
JTextField newage = new JTextField(27);
jp5.add(newage);
jp5.add(new JLabel("学分:"));
JTextField newscroll = new JTextField(27);
jp5.add(newscroll);
jp5.add(new JLabel("地址:"));
JTextField newaddress = new JTextField(27);
jp5.add(newaddress);
add(jp5);
JPanel jp3 = new JPanel();
jp3.setPreferredSize(new Dimension(620, 30));
JTextField show = new JTextField(20);
show.setEditable(false);
jp3.add(show);
add(jp3);
JPanel jp4 = new JPanel();
jp4.setPreferredSize(new Dimension(370, 30));
JButton tijiao2 = new JButton("提交");
jp4.add(tijiao2);
add(jp4);
tijiao1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String hao = id.getText(); //学号
String ming = name.getText();//姓名
if(hao.equals(""))
{
show.setText("学号不能为空");
}else if(ming.equals(""))
{
show.setText("姓名不能为空");
}else
{
show.setText("正在连接数据库!请稍后!");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jichangxiu", "root", "hz981225");
show.setText("连接数据库成功!");
String sql = "select * from stu where name=? and id=?;";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, ming);
ps.setString(2, hao);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
rs.previous();
while(rs.next())
{
String id = rs.getString("id");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
double score = rs.getDouble("score");
String address = rs.getString("address");
newid.setText(id);
newname.setText(name);
newsex.setText(sex);
newage.setText(age+"");
newscroll.setText(score+"");
newaddress.setText(address);
}
show.setText("数据读取成功!");
}else
{
show.setText("找不到该学生的相关信息");
}
}catch(ClassNotFoundException e1)
{
e1.printStackTrace();
show.setText("学生删除失败");
}catch(SQLException e1)
{
e1.printStackTrace();
show.setText("学生删除失败");
}
}
}
});
tijiao2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String id1 = id.getText(); //学号
String name1 = name.getText();//姓名
String hao = newid.getText(); //学号
String ming = newname.getText();//姓名
String bie = newsex.getText(); //性别
String ling = newage.getText(); //年龄
int nian = Integer.parseInt(ling);
String cheng = newscroll.getText(); //成绩
double ji = Double.parseDouble(cheng);
String dizhi = newaddress.getText();//地址
if(hao.equals(""))
{
show.setText("学号不能为空");
}else if(ming.equals(""))
{
show.setText("姓名不能为空");
}else if(bie.equals(""))
{
show.setText("性别不能为空");
}else if(ling.equals(""))
{
show.setText("年龄不能为空");
}else if(cheng.equals(""))
{
show.setText("成绩不能为空");
}else if(dizhi.equals(""))
{
show.setText("地址不能为空");
}else
{
show.setText("正在提交到数据库!请稍后!");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jichangxiu", "root", "hz981225");
String sql = "update stu set id=?,name=?,sex=?,age=?,score=?,address=? where name=? and id=?;";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, hao);
ps.setString(2, ming);
ps.setString(3, bie);
ps.setInt(4, nian);
ps.setDouble(5, ji);
ps.setString(6, dizhi);
ps.setString(7, name1);
ps.setString(8, id1);
int rows = ps.executeUpdate();
if(rows == 1)
{
show.setText("学生修改成功");
id.setText("");
name.setText("");
newid.setText("");
newname.setText("");
newsex.setText("");
newage.setText("");
newscroll.setText("");
newaddress.setText("");
}
}catch(ClassNotFoundException e1)
{
e1.printStackTrace();
show.setText("学生添加失败");
}catch(SQLException e1)
{
e1.printStackTrace();
show.setText("学生添加失败");
}
}
}
});
reset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
id.setText("");
name.setText("");
show.setText("重置成功");
}
});
}
}
}
//删除某个学生
class deleteFrame extends JFrame
{
deleteFrame()
{
super("删除某个学生信息");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(300, 300, 350, 200);
this.setLayout(new FlowLayout());
this.setResizable(false);
init();
this.setVisible(true);
}
private void init()
{
add(new myJpanel());
}
class myJpanel extends JPanel
{
myJpanel()
{
this.setPreferredSize(new Dimension(310, 200));
init();
}
private void init()
{
JPanel jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(620, 30));
jp1.add(new JLabel("学号:"));
JTextField id = new JTextField(20);
jp1.add(id);
add(jp1);
JPanel jp2 = new JPanel();
jp2.setPreferredSize(new Dimension(620, 30));
jp2.add(new JLabel("姓名:"));
JTextField name = new JTextField(20);
jp2.add(name);
add(jp2);
JPanel jp3 = new JPanel();
jp3.setPreferredSize(new Dimension(620, 30));
JTextField show = new JTextField(20);
show.setEditable(false);
jp3.add(show);
add(jp3);
JPanel jp4 = new JPanel();
jp4.setPreferredSize(new Dimension(620, 30));
JButton tijiao = new JButton("提交");
JButton reset = new JButton("重置");
jp4.add(tijiao);
jp4.add(reset);
add(jp4);
tijiao.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String hao = id.getText(); //学号
String ming = name.getText();//姓名
if(hao.equals(""))
{
show.setText("学号不能为空");
}else if(ming.equals(""))
{
show.setText("姓名不能为空");
}else
{
show.setText("正在删除数据库信息!请稍后!");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jichangxiu", "root", "hz981225");
String sql = "delete from stu where ? = id and ? = name;";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, hao);
ps.setString(2, ming);
int rows = ps.executeUpdate();
if(rows == 1)
{
show.setText("学生删除成功");
id.setText("");
name.setText("");
}
}catch(ClassNotFoundException e1)
{
e1.printStackTrace();
show.setText("学生删除失败");
}catch(SQLException e1)
{
e1.printStackTrace();
show.setText("学生删除失败");
}
}
}
});
reset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
id.setText("");
name.setText("");
show.setText("重置成功");
}
});
}
}
}
//添加一个学生信息:
class addFrame extends JFrame
{
addFrame()
{
super("添加一个学生信息");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(300, 300, 350, 350);
this.setLayout(new FlowLayout());
this.setResizable(false);
init();
this.setVisible(true);
}
private void init()
{
add(new myJpanel());
}
class myJpanel extends JPanel
{
myJpanel()
{
this.setPreferredSize(new Dimension(300, 300));
init();
}
private void init()
{
JPanel jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(620, 30));
jp1.add(new JLabel("学号:"));
JTextField id = new JTextField(20);
jp1.add(id);
add(jp1);
JPanel jp2 = new JPanel();
jp2.setPreferredSize(new Dimension(620, 30));
jp2.add(new JLabel("姓名:"));
JTextField name = new JTextField(20);
jp2.add(name);
add(jp2);
JPanel jp3 = new JPanel();
jp3.setPreferredSize(new Dimension(620, 30));
jp3.add(new JLabel("性别:"));
JTextField sex = new JTextField(20);
jp3.add(sex);
add(jp3);
JPanel jp4 = new JPanel();
jp4.setPreferredSize(new Dimension(620, 30));
jp4.add(new JLabel("年龄:"));
JTextField age = new JTextField(20);
jp4.add(age);
add(jp4);
JPanel jp5 = new JPanel();
jp5.setPreferredSize(new Dimension(620, 30));
jp5.add(new JLabel("学分:"));
JTextField scroe = new JTextField(20);
jp5.add(scroe);
add(jp5);
JPanel jp6 = new JPanel();
jp6.setPreferredSize(new Dimension(620, 30));
jp6.add(new JLabel("地址:"));
JTextField address = new JTextField(20);
jp6.add(address);
add(jp6);
JPanel jp7 = new JPanel();
jp7.setPreferredSize(new Dimension(620, 30));
JTextField show = new JTextField(20);
show.setEditable(false);
jp7.add(show);
add(jp7);
JPanel jp8 = new JPanel();
jp8.setPreferredSize(new Dimension(620, 30));
JButton tijiao = new JButton("提交");
JButton reset = new JButton("重置");
jp8.add(tijiao);
jp8.add(reset);
add(jp8);
tijiao.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String hao = id.getText(); //学号
String ming = name.getText();//姓名
String bie = sex.getText(); //性别
String ling = age.getText(); //年龄
int nian = Integer.parseInt(ling);
String cheng = scroe.getText(); //成绩
double ji = Double.parseDouble(cheng);
String dizhi = address.getText();//地址
if(hao.equals(""))
{
show.setText("学号不能为空");
}else if(ming.equals(""))
{
show.setText("姓名不能为空");
}else if(bie.equals(""))
{
show.setText("性别不能为空");
}else if(ling.equals(""))
{
show.setText("年龄不能为空");
}else if(cheng.equals(""))
{
show.setText("成绩不能为空");
}else if(dizhi.equals(""))
{
show.setText("地址不能为空");
}else
{
show.setText("正在提交到数据库!请稍后!");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jichangxiu", "root", "hz981225");
String sql = "insert into stu values(?,?,?,?,?,?);";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, hao);
ps.setString(2, ming);
ps.setString(3, bie);
ps.setInt(4, nian);
ps.setDouble(5, ji);
ps.setString(6, dizhi);
int rows = ps.executeUpdate();
if(rows == 1)
{
show.setText("学生添加成功");
id.setText("");
name.setText("");
sex.setText("");
age.setText("");
scroe.setText("");
address.setText("");
}
}catch(ClassNotFoundException e1)
{
e1.printStackTrace();
show.setText("学生添加失败");
}catch(SQLException e1)
{
e1.printStackTrace();
show.setText("学生添加失败");
}
}
}
});
reset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
id.setText("");
name.setText("");
sex.setText("");
age.setText("");
scroe.setText("");
address.setText("");
show.setText("重置成功!");
}
});
}
}
}
//显示所有学生信息
class showFrame extends JFrame
{
showFrame()
{
super("显示所有学生信息");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(300, 300, 650, 400);
this.setLayout(new FlowLayout());
this.setResizable(false);
init();
this.setVisible(true);
}
private void init()
{
add(new myJpanel());
}
class myJpanel extends JPanel
{
myJpanel()
{
this.setBackground(Color.white);
this.setPreferredSize(new Dimension(620, 350));
init();
}
private void init()
{
JButton jb = new JButton("显示所有学生信息");
add(jb);
JButton clear = new JButton("清除数据");
add(clear);
JTextArea jta = new JTextArea(17,55);
jta.setEditable(false);
jta.setLineWrap(true);
add(new JScrollPane(jta));
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jichangxiu", "root", "hz981225");
String sql = "select * from stu";
PreparedStatement ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
rs.previous();
jta.setText("");
int count = 1 ;
while(rs.next())
{
String id = rs.getString("id");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String score = rs.getString("score");
String address = rs.getString("address");
jta.append("--------------------------------\r\n");
jta.append("学生"+count++ + "\r\n");
jta.append("学号:" + id + "\r\n");
jta.append("姓名:" + name + "\r\n");
jta.append("性别:" + sex + "\r\n");
jta.append("年龄:" + age + "\r\n");
jta.append("学分:" + score + "\r\n");
jta.append("地址:" + address + "\r\n");
jta.append("--------------------------------\r\n");
// jta.append("学号\t姓名\t性别\t年龄\t学分\t地址\r\n");
// jta.append(id + " " + name + "\t" + sex + "\t" + age + "\t" + score + "\t" + address+"\r\n");
}
}else
{
jta.append("数据库里面空空如野!");
}
}catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}catch(SQLException e1)
{
e1.printStackTrace();
}
}
});
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setText("");
}
});
}
}
}
//查找某个学生的信息
class selectFrame extends JFrame
{
selectFrame()
{
super("查找某个学生的信息");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(300, 300, 600, 400);
this.setLayout(new FlowLayout());
this.setResizable(false);
init();
this.setVisible(true);
}
private void init()
{
add(new myJpanel());
}
class myJpanel extends JPanel
{
myJpanel()
{
this.setBackground(Color.white);
this.setPreferredSize(new Dimension(550, 350));
init();
}
private void init()
{
add(new JLabel("请输入要查找的学生的姓名:"));
JTextField jtf = new JTextField(20);
add(jtf);
JButton tijiao = new JButton("提交");
add(tijiao);
JButton clear = new JButton("清除");
add(clear);
JTextArea jta = new JTextArea(17,48);
jta.setEditable(false);
jta.setLineWrap(true);
add(new JScrollPane(jta));
tijiao.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = jtf.getText();
if(str.equals(""))
{
jta.append("你没有输入姓名\r\n");
}else
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jichangxiu", "root", "hz981225");
String sql = "select * from stu where ? = name";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, str);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
String id = rs.getString("id");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String score = rs.getString("score");
String address = rs.getString("address");
jta.append("--------------------------------\r\n");
jta.append("学号:" + id + "\r\n");
jta.append("姓名:" + name + "\r\n");
jta.append("性别:" + sex + "\r\n");
jta.append("年龄:" + age + "\r\n");
jta.append("学分:" + score + "\r\n");
jta.append("地址:" + address + "\r\n");
jta.append("--------------------------------\r\n");
}else
{
jta.append("没有找到你要找的学生!\r\n");
}
}catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}catch(SQLException e1)
{
e1.printStackTrace();
}
}
}
});
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setText("");
}
});
}
}
} 这样看着好别扭 您好我是小白是把这些代码 连接一起么直接输出还是 哪个部分是JAVA哪些部分是连接数据库的代码
还望楼主指导下
然后你想表达什么 第一次写那么长的代码.... 那一夜谁懂22 发表于 2018-7-9 19:53
然后你想表达什么
主要刚学了jdbc
拿来练手的.
楼主,加油。支持一下。 夏橙M兮 发表于 2018-7-9 20:07
楼主,加油。支持一下。
嗯, 以后还有太多需要学习.:lol 典型的增删改查收下了 楼主可以加个qq指导一下吗 楼主我发现一个错误,jta.append("数据库里面空空如野!"); 这个野字不对