前言
这是一个基于JavaFx+Mysql的图书管理系统,该系统可以实现注册登录功能。
可以对图书实现增删改查等基本功能,还可以为学生办理借书功能,若书未被借走,则可正常借出;若已被借出,则不能再被借。
项目完整源代码放在文章末尾。
一、页面效果展示
1. 注册页面
2. 登录页面
3.系统主页面
4.借阅图书
5. 数据库建表
二、连接数据库
需要插入一个 jar包,然后通过JDBC建立连接。
(jar包放在文章末尾)`````
// 先建立连接
String URL="jdbc:mysql://127.0.0.1:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//jdbc:mysql//服务器地址/数据库名
String USER=" ";//用户名
String PASSWORD=" ";//密码
Connection con=DriverManager.getConnection(URL, USER, PASSWORD);
// 接着执行 sql语句
PreparedStatement pst =con.prepareStatement(sql) ;
// 查看 sql语句的执行结果
ResultSet rs = pstexecuteQuery();
三、主要功能实现
1.增加信息
// 这个是 点击 增加信息按钮
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Connection con=null;
if (a1.getText().isEmpty()||a2.getText().isEmpty()||a3.getText().isEmpty()||a4.getText().isEmpty()||a5.getText().isEmpty()){
warn shibai = new warn(stage1,"请输入完整的信息");
}else {
try {
con = DriverManager.getConnection(DBHelp.URL, DBHelp.USER, DBHelp.PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
String sqll= "insert into information (ID,name,press,author,state) values (?,?,?,?,?);";
PreparedStatement pst11 = null;
try {
pst11 = con.prepareStatement(sqll);
pst11.setString(1,a1.getText());
pst11.setString(2,a2.getText());
pst11.setString(3,a3.getText());
pst11.setString(4,a4.getText());
pst11.setString(5,a5.getText());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
pst11.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
stage1.close();
stage.close();
Mywindow mywindow = new Mywindow();
}
}
});
}
2. 预定图书
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (a1.getText().isEmpty() || a2.getText().isEmpty() || a3.getText().isEmpty()){
warn w1 = new warn(stage2," 请输入完整信息!");
}else {
Connection connection = null;
try {
connection = DriverManager.getConnection(DBHelp.URL,DBHelp.USER,DBHelp.PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
String sql1 = "update information set state=?,lendname=?,lendsex=?,lendnum=? where ID=?;";
try {
PreparedStatement preparedStatement=connection.prepareStatement(sql1);
preparedStatement.setString(1,"已借出");
preparedStatement.setString(2,a1.getText());
preparedStatement.setString(3,a2.getText());
preparedStatement.setString(4,a3.getText());
preparedStatement.setString(5,s);
preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
stage2.close();
stage.close();
Mywindow mywindow = new Mywindow();
}
}
});
3.归还图书
if (s1.equals("存在")){
warn warn = new warn(stage,"该书已经未被借出!");
} else if (!s1.equals("存在")){
String sql2 = "update information set state=? , lendname=?,lendsex=?,lendnum=? where ID = ?";
try {
Connection connection1 = DriverManager.getConnection(DBHelp.URL,DBHelp.USER,DBHelp.PASSWORD);
PreparedStatement preparedStatement = connection1.prepareStatement(sql2);
preparedStatement.setString(1,"存在");
preparedStatement.setString(2," ");
preparedStatement.setString(3," ");
preparedStatement.setString(4," ");
preparedStatement.setString(5,s);
preparedStatement.executeUpdate();
stage1.close();
Mywindow mywindow = new Mywindow();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
4.查询图书
String sql3 = "select * from information where (name like '"+s+"%'||name like '%"+s+"'||name like '%"+s+"%'|| name = '"+s+"')";
Connection connection = null;
try {
connection = DriverManager.getConnection(DBHelp.URL,DBHelp.USER,DBHelp.PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql3);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
ResultSet resultSet = null;
try {
resultSet = preparedStatement.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (s.isEmpty()){
sql3 = "select * from information";
}
try {
leader_log.mywindow.date(table,sql3);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
5. 删除图书
Student row = table.getSelectionModel().getSelectedItem();
if (row == null){
warn w2 = new warn(stage,"请选择您要删除的图书!");
}else {
Connection connection = null;
try {
connection = DriverManager.getConnection(DBHelp.URL, DBHelp.USER, DBHelp.PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
String s = row.getID();
String sql1 = "delete from information where ID = ?;";
PreparedStatement preparedStatement=null;
try {
preparedStatement=connection.prepareStatement(sql1);
preparedStatement.setString(1,s);
preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
stage.close();
Mywindow mywindow = new Mywindow();
}
四、完整源代码展示
由于代码过长,所以本文展示部分的代码,完整代码在下方的百度网盘里,有需要的可以自行提取。
链接:https://pan.baidu.com/s/19_eubeUksGQBkttVa8bALQ?pwd=ojbk
提取码:ojbk