怎么使用Java+mysql实现学籍管理系统
导读:本文共8803.5字符,通常情况下阅读需要29分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:一、myswql数据库表格项目使用mysql数据库,有2张表格。一张用户表用于登录验证,一张学生表,用于增删改查。creattablet_user(idintprimarykeyauto_increment,login_namevarchar(255),login_pwdvarchar(255),real_namevarchar(255),);insertintot_user(id,l... ...
目录
(为您整理了一些要点),点击可以直达。一、myswql数据库表格
项目使用mysql数据库,有2张表格。一张用户表用于登录验证,一张学生表,用于增删改查。
creattablet_user(
idintprimarykeyauto_increment,
login_namevarchar(255),
login_pwdvarchar(255),
real_namevarchar(255),
);
insertintot_user(id,login_name,login_pwd,real_name)
values('akm',"123",'萝卜蹲');
CREATETABLEt_user(
idchar(12)PRIMARYKEY,
namechar(6),
pwdvarchar(255),
);
二、功能实现
1.实际演示
1.1登录界面
在用户烂输入:akm
密码栏输入:123
点击登录按钮,就可以直接进入系统。
如果输入错误,状态栏会显示登录失败,并清空登录账户和密码。
1.2系统主界面
系统主界面由5个按钮组成
添加学生信息,在主界面中选择添加按钮并点击进入添加界面,如上图所示。在界面中添加相应的学生信息,id,姓名,年龄 学籍等。
1.3查询信息
通过id查询学生信息。
1.4遍历信息
1.5 删除信息
输入id直接删除。
1.6 更新信息
2.test.java文件源码
项目只有一个test文件,没有封装,有需要的小伙伴可以自己进行封装。
代码如下:
packagecom.company;
importjavax.swing.;
importjava.awt.;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.*;
importjava.sql.Statement;
importjava.util.Scanner;
publicclassTest{
staticConnectionconn;
staticStatementstatement;
publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
login();
}
publicstaticvoidcontrol(){
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(newFlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,300,200);
JButtonbutton=newJButton("更新");
JButtonbutton1=newJButton("遍历");
JButtonbutton2=newJButton("删除");
JButtonbutton3=newJButton("添加");
JButtonbutton4=newJButton("查询");
jf.add(button);
jf.add(button1);
jf.add(button2);
jf.add(button3);
jf.add(button4);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
update();
}
});
button1.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
query();
}
});
button2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
delete();
}
});
button3.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
insert();
}
});
button4.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){onequery();}});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
publicstaticvoidupdate(){
conn=getConnection();
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(newFlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,300,200);
JLabellabel1=newJLabel("年龄");
JTextFieldagetext=newJTextField("",10);
JLabellabel2=newJLabel("id");
JTextFieldidtext=newJTextField("",10);
JLabellabel3=newJLabel("学籍");
JTextFieldaddresstext=newJTextField("",10);
JLabellabel4=newJLabel("姓名");
JTextFieldnametext=newJTextField("",5);
JTextFieldout=newJTextField("更新状态",20);
JButtonbutton=newJButton("更新");
jf.add(label1);
jf.add(agetext);
jf.add(label2);
jf.add(idtext);
jf.add(label3);
jf.add(addresstext);
jf.add(label4);
jf.add(nametext);
jf.add(out);
jf.add(button);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
PreparedStatementps=null;
Stringage=agetext.getText();
Stringid=idtext.getText();
Stringaddress=addresstext.getText();
Stringname=nametext.getText();
try{
//更新数据的sql语句
Stringsql="updatestudentsetage=?,address=?,name=?whereid=?";
ps=conn.prepareStatement(sql);
ps.setString(1,agetext.getText());
ps.setString(2,addresstext.getText());
ps.setString(3,nametext.getText());
ps.setString(4,idtext.getText());
intcount=ps.executeUpdate();//记录操作次数
//输出插入操作的处理结果
System.out.println("user表中更新"+count+"条数据");
ps.close();
//关闭数据库连接
conn.close();
out.setText("更新成功!!!!!!!!");
//创建用于执行静态sql语句的Statement对象,st属局部变量
}catch(SQLExceptiona){
System.out.println("更新数据失败");
}}
});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
publicstaticvoidquery(){
PreparedStatementps=null;
conn=getConnection();
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(null);
jf.setBounds(400,300,350,200);
JButtonbutton=newJButton("查询");
JTextAreajm=newJTextArea("ID\t姓名\t年龄\t学籍");//显示界面
jm.setBounds(10,50,350,100);//定义显示界面位置
jf.add(button);
jf.add(jm);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
PreparedStatementps=null;
try{
Stringsql="select*fromStudent";
//创建用于执行静态sql语句的Statement对象,statement属局部变量
statement=conn.createStatement();//获取操作对象
ResultSetresultSet=statement.executeQuery(sql);//executeQuery执行单个SQL语句,返回单个ResultSet对象是
while(resultSet.next())//循环没有数据的时候返回flase退出循环
{
IntegerId=resultSet.getInt("id");//resultSet.next()是一个光标
Stringname=resultSet.getString("name");//getString返回的值一定是string
Integerage=resultSet.getInt("age");
Stringaddress=resultSet.getString("address");
//Stringadress=resultSet.getString("adress");
//输出查到的记录的各个字段的值
jm.append("\n"+Id+"\t"+name+"\t"+age+"\t"+address);
}
statement.close();
conn.close();
}catch(SQLExceptionb){
System.out.println("查询失败!!!!!!!!");
}
}
});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
publicstaticvoiddelete(){
conn=getConnection();
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(newFlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,300,200);
JLabellabel2=newJLabel("id");
JTextFieldidtext=newJTextField("",10);
JTextFieldout=newJTextField("删除状态",20);
JButtonbutton=newJButton("删除");
jf.add(label2);
jf.add(idtext);
jf.add(out);
jf.add(button);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Stringid=idtext.getText();
PreparedStatementps=null;
try{
//删除数据的sql语句
Stringsql="deletefromStudentwhereid=?";
ps=conn.prepareStatement(sql);
ps.setString(1,idtext.getText());
intcount=ps.executeUpdate();//记录操作次数
//输出插入操作的处理结果
System.out.println("student表中删除"+count+"条数据");
ps.close();
out.setText("删除成功!!!!!!!!");
//关闭数据库连接
conn.close();
}catch(SQLExceptionc){
System.out.println("删除数据失败");
}
}
});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
publicstaticvoidinsert(){
//首先要获取连接,即连接到数据库
conn=getConnection();
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(newFlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,300,200);
JLabellabel3=newJLabel("id");
JTextFieldidtext=newJTextField("",10);
JLabellabel1=newJLabel("年龄");
JTextFieldagetext=newJTextField("",10);
JLabellabel2=newJLabel("姓名");
JTextFieldnametext=newJTextField("",10);
JLabellabel4=newJLabel("学籍");
JTextFieldaddresstext=newJTextField("",5);
JTextFieldout=newJTextField("添加状态",20);
JButtonbutton=newJButton("添加");
jf.add(label3);
jf.add(idtext);
jf.add(label1);
jf.add(agetext);
jf.add(label2);
jf.add(nametext);
jf.add(label4);
jf.add(addresstext);
jf.add(out);
jf.add(button);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Stringage=agetext.getText();
Stringname=nametext.getText();
Stringid=idtext.getText();
Stringaddress=addresstext.getText();
try{
PreparedStatementps=null;
//插入数据的sql语句
Stringsql="INSERTINTOStudent(id,age,name,address)VALUES(?,?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,idtext.getText());
ps.setString(2,agetext.getText());
ps.setString(3,nametext.getText());
ps.setString(4,addresstext.getText());
intcount=ps.executeUpdate();//记录操作次数
//输出插入操作的处理结果
System.out.println("向user表中插入"+count+"条数据");
ps.close();
out.setText("添加成功!!!!!!!!");
//关闭数据库连接
conn.close();
}catch(SQLExceptiond){
System.out.println("插入数据失败"+d.getMessage());
}}
});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
publicstaticbooleanlogin(){
conn=getConnection();
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(newFlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,300,200);
JLabellabel1=newJLabel("用户名");
JTextFieldusernametext=newJTextField("",20);
JLabellabel2=newJLabel("密码");
JPasswordFieldpwdtext=newJPasswordField("",20);
JTextFieldout=newJTextField("登录状态",20);
JButtonbutton=newJButton("登录");
jf.add(label1);
jf.add(usernametext);
jf.add(label2);
jf.add(pwdtext);
jf.add(out);
jf.add(button);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//插入数据的sql语句
PreparedStatementps=null;
try{
statement=conn.createStatement();//获取操作对象
Stringx=usernametext.getText();
Stringy=pwdtext.getText();
Stringsql="selectfromuser";
ResultSetresultSet=statement.executeQuery(sql);
while(resultSet.next()){
Stringa=resultSet.getString("login_name");
Stringb=resultSet.getString("login_pwd");
if(a.equals(x)&&b.equals(y))
{
control();
out.setText("!!!!!登录成功!!!!!");
}
elseif(x!=a&&y!=b){
out.setText("登录失败,请重新输入");
}
}
usernametext.setText("");
pwdtext.setText("");
}catch(SQLExceptionthrowables){
throwables.printStackTrace();
}
}
});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
booleanok=true;
returnok;
}
publicstaticvoidonequery(){
PreparedStatementps=null;
conn=getConnection();
JFramejf=newJFrame("学生学籍管理系统");
jf.setLayout(newFlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,350,200);
JLabellabel3=newJLabel("id");
JTextFieldidtext=newJTextField("",10);
JLabellabel1=newJLabel("条件");
JTextFieldatext=newJTextField("",10);
JButtonbutton=newJButton("查询");
//JTextAreajm=newJTextArea("ID\t姓名\t年龄\t学籍");//显示界面
//jm.setBounds(10,50,350,100);//定义显示界面位置
jf.add(label3);
jf.add(idtext);
jf.add(button);
//jf.add(jm);
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
PreparedStatementps=null;
Stringid=idtext.getText();
conn=getConnection();
try{
Stringsql="selectfromstudentwhereid=?";
//创建用于执行静态sql语句的Statement对象,statement属局部变量
ps=conn.prepareStatement(sql);//获取操作对象
ps.setString(1,idtext.getText().toString());
ResultSetresultSet=ps.executeQuery();//executeQuery执行单个SQL语句,返回单个ResultSet对象是
while(resultSet.next())//循环没有数据的时候返回flase退出循环
{
IntegerId=resultSet.getInt("id");//resultSet.next()是一个光标
Stringname=resultSet.getString("name");//getString返回的值一定是string
Integerage=resultSet.getInt("age");
Stringaddress=resultSet.getString("address");
System.out.println(Id+""+name+""+age+""+address+"");
//输出查到的记录的各个字段的值
//jm.append("\n"+Id+"\t"+name+"\t"+age+"\t"+address);
}
statement.close();
conn.close();
}catch(SQLExceptionm){
System.out.println(m.getMessage());
System.out.println("查询失败!!!!!!!!");
}
}
});
jf.setVisible(true);
jf.setResizable(false);
button.setSize(40,20);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
publicstaticConnectiongetConnection(){
//创建用于连接数据库的Connection对象
Connectionconnection=null;
try{
//加载Mysql数据驱动
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("数据库驱动加载成功");
Stringurl="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
//创建数据连接
connection=DriverManager.getConnection(url,"root","root");
System.out.println("数据库连接成功");
}catch(ClassNotFoundException|SQLExceptione){
System.out.println("数据库连接失败"+e.getMessage());//处理查询结果
}
returnconnection;
}}
怎么使用Java+mysql实现学籍管理系统的详细内容,希望对您有所帮助,信息来源于网络。