弹窗帮助类
DialogUtil
| package com.qf.utils; |
| |
| import javafx.scene.control.Alert; |
| import javafx.scene.control.ButtonBar; |
| import javafx.scene.control.ButtonType; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.util.Optional; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class DialogUtil { |
| public static boolean Dialog(Alert.AlertType alertType, String title, String header, String message) { |
| Alert alert = new Alert(alertType, message, |
| new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE), |
| new ButtonType("确认", ButtonBar.ButtonData.YES)); |
| alert.setTitle(title); |
| alert.setHeaderText(header); |
| |
| Optional<ButtonType> buttonType = alert.showAndWait(); |
| |
| return buttonType.get().getButtonData().equals(ButtonBar.ButtonData.YES); |
| } |
| } |
| |
Md5加密工具类
Md5Util
原因:
为了防止开发者可以随意登录用户数据,必须需要对密码进行不可逆加密。
注意:
登录的时候进行对比加密后的消息;而不是对密码进行解密。
| package com.qf.utils; |
| import java.math.BigInteger; |
| import java.security.MessageDigest; |
| import java.security.NoSuchAlgorithmException; |
| public class Md5Util { |
| |
| |
| |
| |
| |
| public static String md5(String str){ |
| |
| MessageDigest md5 = null; |
| try { |
| md5 = MessageDigest.getInstance("MD5"); |
| |
| String string = new BigInteger(1, |
| md5.digest(str.getBytes())).toString(16); |
| |
| return fillMd5(string); |
| } catch (NoSuchAlgorithmException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| private static String fillMd5(String md5){ |
| return md5.length()==32?md5:fillMd5("0"+md5); |
| } |
| } |
登录用户信息共享空间
Session
作用:
方便用户登录后,各个功能都能方便的调用信息。
| package com.qf.utils; |
| import com.qf.domain.User; |
| |
| |
| |
| public class Session { |
| |
| private static User user; |
| |
| public static void setUser(User user) { |
| Session.user = user; |
| } |
| |
| public static User getUser() { |
| return user; |
| } |
| } |
| |
用户登录流程
controller层处理
LoginController
| package com.qf.controller; |
| import com.qf.MainApplication; |
| import com.qf.domain.User; |
| import com.qf.service.UserService; |
| import com.qf.service.impl.UserServiceImpl; |
| import com.qf.utils.DialogUtil; |
| import com.qf.utils.Md5Util; |
| import com.qf.utils.Session; |
| import javafx.fxml.FXML; |
| import javafx.scene.control.Alert; |
| import javafx.scene.control.PasswordField; |
| import javafx.stage.Stage; |
| import javafx.scene.control.TextField; |
| |
| |
| public class LoginController { |
| |
| private Stage LoginStage; |
| |
| public Stage getLoginStage() { |
| return LoginStage; |
| } |
| |
| public void setLoginStage(Stage loginStage) { |
| LoginStage = loginStage; |
| } |
| |
| @FXML |
| private TextField usernameInput; |
| @FXML |
| private PasswordField passwordInput; |
| |
| |
| |
| |
| @FXML |
| void login() { |
| |
| String username = usernameInput.getText(); |
| String password = passwordInput.getText(); |
| |
| if (username.trim().equals("") || password.trim().equals("")) { |
| DialogUtil.Dialog(Alert.AlertType.WARNING, "警告", "危险动作", |
| "用户名及密码不能为空"); |
| return; |
| } |
| |
| UserService userService = new UserServiceImpl(); |
| |
| User user = userService.findByUserName(username); |
| |
| |
| if (user != null) { |
| |
| if (user.getPassword().equals(Md5Util.md5(password))){ |
| |
| Session.setUser(user); |
| |
| boolean success = DialogUtil.Dialog(Alert.AlertType.INFORMATION, "提示", "友情提示", |
| "登录成功"); |
| |
| if (success) { |
| |
| this.LoginStage.close(); |
| |
| new MainApplication().initHome(); |
| }else { |
| |
| } |
| } |
| else { |
| |
| DialogUtil.Dialog(Alert.AlertType.WARNING, "警告", "危险动作", |
| "密码错误"); |
| } |
| } else { |
| |
| DialogUtil.Dialog(Alert.AlertType.WARNING, "警告", "危险动作", |
| "当前用户不存在"); |
| } |
| |
| } |
| |
| |
| @FXML |
| void register() { |
| |
| new MainApplication().initRegister(); |
| } |
| } |
| |
service层
| package com.qf.service.impl; |
| import com.qf.dao.UserDao; |
| import com.qf.dao.impl.UserDaoImpl; |
| import com.qf.domain.User; |
| import com.qf.service.UserService; |
| public class UserServiceImpl implements UserService { |
| |
| UserDao userDao = new UserDaoImpl(); |
| @Override |
| public User findByUserName(String username) { |
| |
| User user = userDao.selectByUserName(username); |
| return user; |
| } |
| } |
dao层
| package com.qf.dao.impl; |
| import com.qf.dao.UserDao; |
| import com.qf.domain.User; |
| import com.qf.utils.DbUtil; |
| import java.sql.*; |
| |
| public class UserDaoImpl implements UserDao { |
| |
| @Override |
| public User selectByUserName(String username) { |
| Connection connection = DbUtil.getConnection(); |
| |
| |
| String sql = "select * from t_user where username=?"; |
| PreparedStatement statement = DbUtil.getStatement(sql); |
| try { |
| statement.setString(1,username); |
| ResultSet resultSet = statement.executeQuery(); |
| |
| User user = null; |
| |
| while(resultSet.next()){ |
| user = new User(); |
| |
| user.setId(resultSet.getInt("id")); |
| user.setUsername(resultSet.getString("username")); |
| user.setPassword(resultSet.getString("password")); |
| user.setAvatar(resultSet.getString("avatar")); |
| user.setNickname(resultSet.getString("nickname")); |
| } |
| |
| |
| return user; |
| } catch (SQLException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| } |
MainApplication
注册功能实现
controller层
RegisterController
| package com.qf.controller; |
| |
| import com.qf.MainApplication; |
| import com.qf.domain.User; |
| import com.qf.service.UserService; |
| import com.qf.service.impl.UserServiceImpl; |
| import com.qf.utils.DialogUtil; |
| import javafx.fxml.FXML; |
| import javafx.scene.control.Alert; |
| import javafx.scene.control.PasswordField; |
| import javafx.scene.control.TextField; |
| import javafx.stage.Stage; |
| |
| import java.io.IOException; |
| |
| public class RegisterController { |
| |
| private Stage RegisterStage; |
| |
| public Stage getRegisterStage() { |
| return RegisterStage; |
| } |
| |
| public void setRegisterStage(Stage primaryStage) { |
| this.RegisterStage = RegisterStage; |
| } |
| |
| @FXML |
| private TextField usernameInput; |
| @FXML |
| private PasswordField passwordInput; |
| |
| |
| |
| @FXML |
| void login() throws IOException { |
| |
| new MainApplication().initLogin(); |
| } |
| |
| |
| |
| @FXML |
| void register() throws IOException { |
| |
| String username = usernameInput.getText(); |
| String password = passwordInput.getText(); |
| |
| if (username.trim().equals("") || password.trim().equals("")) { |
| DialogUtil.Dialog(Alert.AlertType.WARNING, "警告", "危险动作", |
| "用户名及密码不能为空"); |
| |
| } else { |
| |
| UserService userService = new UserServiceImpl(); |
| User user = userService.findByUserName(username); |
| |
| if (user != null) { |
| DialogUtil.Dialog(Alert.AlertType.INFORMATION, "提示", "提示", |
| "用户名已经存在"); |
| } else { |
| User insertUser = new User(); |
| insertUser.setUsername(username); |
| insertUser.setPassword(password); |
| |
| boolean result = userService.saveUser(insertUser); |
| |
| if (result) { |
| boolean success = DialogUtil.Dialog(Alert.AlertType.INFORMATION, "OK", "友情提示", |
| "用户添加成功,点击确定进入登录,点击取消继续注册其他账户"); |
| if (success) { |
| |
| |
| |
| new MainApplication().initLogin(); |
| } |
| } else { |
| DialogUtil.Dialog(Alert.AlertType.ERROR, "错误", "错误信息", |
| "用户添加失败"); |
| } |
| |
| } |
| |
| } |
| |
| } |
| } |
| |
service层
UserServiceImpl
其他方法已经省略
| @Override |
| public boolean saveUser(User insertUser) { |
| |
| |
| String md5Pwd = Md5Util.md5(insertUser.getPassword()); |
| insertUser.setPassword(md5Pwd); |
| |
| boolean b = userDao.addUser(insertUser); |
| return b; |
| } |
dao层
MainApplication
| package com.qf; |
| import com.qf.controller.LoginController; |
| import javafx.application.Application; |
| import javafx.fxml.FXML; |
| import javafx.fxml.FXMLLoader; |
| import javafx.scene.Parent; |
| import javafx.scene.Scene; |
| import javafx.scene.layout.AnchorPane; |
| import javafx.stage.Modality; |
| import javafx.stage.Stage; |
| import java.io.IOException; |
| |
| public class MainApplication extends Application { |
| @FXML |
| private Stage primaryStage; |
| @FXML |
| private Parent root; |
| public static void main(String[] args) { |
| launch(args); |
| } |
| @Override |
| public void start(Stage primaryStage) throws IOException { |
| this.primaryStage = primaryStage; |
| |
| this.primaryStage.setTitle("管家婆"); |
| initLogin(); |
| } |
| |
| public Scene initLogin() throws IOException { |
| |
| |
| FXMLLoader.load(getClass().getResource("views\\LoginView.fxml")); |
| |
| FXMLLoader fxmlLoader = new FXMLLoader(); |
| fxmlLoader.setLocation(getClass().getResource("views\\LoginView.fxml")); |
| |
| root = fxmlLoader.load(); |
| |
| Scene scene = new Scene(root); |
| |
| primaryStage.setScene(scene); |
| |
| LoginController controller = fxmlLoader.getController(); |
| |
| controller.setLoginStage(primaryStage); |
| |
| |
| primaryStage.show(); |
| return scene; |
| } |
| |
| public Scene initHome(){ |
| try{ |
| |
| FXMLLoader load = new FXMLLoader(); |
| |
| load.setLocation(getClass().getResource("views\\HomeView.fxml")); |
| |
| AnchorPane root = (AnchorPane)load.load(); |
| |
| Scene scene = new Scene(root); |
| |
| Stage stage = new Stage(); |
| stage.setTitle("管家婆系统"); |
| stage.setResizable(true); |
| stage.setAlwaysOnTop(false); |
| |
| stage.initModality(Modality.APPLICATION_MODAL); |
| |
| stage.initOwner(primaryStage); |
| stage.setScene(scene); |
| |
| stage.showAndWait(); |
| |
| return scene; |
| }catch (Exception e){ |
| throw new RuntimeException(e); |
| } |
| } |
| } |
| |
主页制作及渲染
HomeView.fxml
| <?xml version="1.0" encoding="UTF-8"?> |
| <?import javafx.scene.control.*?> |
| <?import javafx.scene.image.*?> |
| <?import javafx.scene.layout.*?> |
| <AnchorPane prefHeight="400.0" prefWidth="600.0" |
| xmlns="http://javafx.com/javafx/10.0.2-internal" |
| xmlns:fx="http://javafx.com/fxml/1" |
| fx:controller="com.qf.controller.HomeController"> |
| <SplitPane dividerPositions="0.29797979797979796" layoutX="-5.0" |
| prefHeight="400.0" prefWidth="611.0"> |
| <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" |
| prefWidth="100.0"> |
| <children> |
| <VBox alignment="CENTER" prefHeight="400.0" prefWidth="179.0"> |
| <children> |
| <HBox alignment="BOTTOM_CENTER" prefHeight="163.0" |
| prefWidth="179.0"> |
| <ImageView fitHeight="70" fitWidth="70" |
| fx:id="imageViewPicture"> |
| <!-- <Image url="@/com/qf/images/panda.png" />--> |
| </ImageView> |
| </HBox> |
| <HBox alignment="CENTER" prefHeight="60.0" |
| prefWidth="179.0"> |
| <Label fx:id="usernameLabel" /> |
| </HBox> |
| <HBox alignment="TOP_CENTER" prefHeight="40.0" |
| prefWidth="179.0"> |
| <Label prefHeight="15.0" prefWidth="50.0" text="总支 |
| 出:" /> |
| <TextField alignment="CENTER" prefHeight="23.0" |
| prefWidth="100" fx:id="totalExpenditure" disable="true" /> |
| </HBox> |
| <HBox alignment="TOP_CENTER" prefHeight="40.0" |
| prefWidth="179.0" fx:id=""> |
| <Label prefHeight="15.0" prefWidth="50.0" text="总收 |
| 入:" /> |
| <TextField alignment="CENTER" prefHeight="23.0" |
| prefWidth="100" fx:id="totalRevenue" disable="true"/> |
| </HBox> |
| <HBox alignment="TOP_CENTER" prefHeight="108.0" |
| prefWidth="179.0"> |
| <Label prefHeight="15.0" prefWidth="50.0" text="余 |
| 额:" /> |
| <TextField prefHeight="23.0" prefWidth="100" |
| fx:id="balance" disable="true"/> |
| </HBox> |
| </children> |
| </VBox> |
| </children> |
| </AnchorPane> |
| <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" |
| prefWidth="100.0"> |
| <MenuBar prefHeight="31.0" prefWidth="426.0"> |
| <menus> |
| <Menu mnemonicParsing="false" text="文件"> |
| <items> |
| <MenuItem mnemonicParsing="false" text="Action 1" /> |
| </items> |
| </Menu> |
| <Menu mnemonicParsing="false" text="编辑"> |
| <items> |
| <MenuItem mnemonicParsing="false" text="Action 1" /> |
| </items> |
| </Menu> |
| <Menu mnemonicParsing="false" text="查询"> |
| <items> |
| <MenuItem mnemonicParsing="false" text="Action 1" /> |
| </items> |
| </Menu> |
| <Menu mnemonicParsing="false" text="选项"> |
| <items> |
| <MenuItem mnemonicParsing="false" text="Action 1" /> |
| </items> |
| </Menu> |
| <Menu mnemonicParsing="false" text="帮助"> |
| <items> |
| <MenuItem mnemonicParsing="false" text="Action 1" /> |
| </items> |
| </Menu> |
| </menus></MenuBar> |
| <TableView layoutX="-1.0" layoutY="31.0" prefHeight="368.0" |
| prefWidth="425.0"> |
| <columns> |
| <TableColumn prefWidth="47.000001549720764" text="序号" /> |
| <TableColumn prefWidth="57.99999237060547" text="类型" /> |
| <TableColumn prefWidth="64.60002899169922" text="金额" /> |
| <TableColumn prefWidth="73.0" text="分类" /> |
| <TableColumn prefWidth="64.0" text="备注" /> |
| <TableColumn prefWidth="108.00003051757812" text="日期" /> |
| </columns> |
| </TableView> |
| </AnchorPane> |
| </SplitPane> |
| </AnchorPane> |
Controller层
HomeController
| package com.qf.controller; |
| import com.qf.dao.RecordDao; |
| import com.qf.dao.impl.RecordDaoImpl; |
| import com.qf.utils.Session; |
| import javafx.fxml.FXML; |
| import javafx.scene.control.Label; |
| import javafx.scene.control.TextField; |
| import javafx.scene.image.Image; |
| import javafx.scene.image.ImageView; |
| |
| public class HomeController { |
| @FXML |
| private ImageView imageViewPicture; |
| @FXML |
| private Label usernameLabel; |
| @FXML |
| private TextField totalExpenditure; |
| @FXML |
| private TextField totalRevenue; |
| @FXML |
| private TextField balance; |
| @FXML |
| public void initialize(){ |
| initUserRecode(); |
| } |
| private void initUserRecode(){ |
| |
| usernameLabel.setText(Session.getUser().getNickname()); |
| |
| imageViewPicture.setImage(new |
| Image("file:"+Session.getUser().getAvatar())); |
| |
| imageViewPicture.setFitWidth(50); |
| imageViewPicture.setFitWidth(50); |
| |
| imageViewPicture.setCache(true); |
| imageViewPicture.setPreserveRatio(true); |
| imageViewPicture.setSmooth(true); |
| |
| RecordDao recordDao = new RecordDaoImpl(); |
| Double Expenditure = recordDao.queryCount(Session.getUser().getId(), "支 |
| 出"); |
| totalExpenditure.setText(Expenditure.toString()); |
| Double Revenue = recordDao.queryCount(Session.getUser().getId(), "收入"); |
| totalRevenue.setText(Revenue.toString()); |
| balance.setText(new Double((Revenue-Expenditure)).toString()); |
| } |
| } |
dao层
RecordDaoImpl
| package com.qf.dao.impl; |
| import com.qf.dao.RecordDao; |
| import com.qf.domain.Record; |
| import com.qf.utils.DbUtil; |
| import java.sql.Connection; |
| import java.sql.PreparedStatement; |
| import java.sql.ResultSet; |
| import java.sql.SQLException; |
| import java.util.ArrayList; |
| import java.util.List; |
| public class RecordDaoImpl implements RecordDao { |
| @Override |
| public List<Record> selectRecordByUserId(int uid) { |
| Connection connection = DbUtil.getConnection(); |
| String sql = "select * from t_record where uid = ?"; |
| PreparedStatement statement = DbUtil.getStatement(sql); |
| ArrayList<Record> records = new ArrayList<>(); |
| try { |
| statement.setInt(1,uid); |
| ResultSet resultSet = statement.executeQuery(); |
| while(resultSet.next()){ |
| Record record = new Record( |
| resultSet.getInt(1), |
| resultSet.getString(3), |
| resultSet.getDouble(4), |
| resultSet.getString(5), |
| resultSet.getString(6), |
| resultSet.getDate(7) |
| ); |
| records.add(record); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| return records; |
| } |
| @Override |
| public double queryCount(int uid, String type) { |
| Connection connection = DbUtil.getConnection(); |
| String sql = "select SUM(rMoney) from t_record where uid=? and rType=?"; |
| PreparedStatement statement = DbUtil.getStatement(sql); |
| double total = 0; |
| try { |
| statement.setInt(1,uid); |
| statement.setString(2,type); |
| ResultSet resultSet = statement.executeQuery(); |
| while(resultSet.next()){ |
| total = resultSet.getDouble(1); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| return total; |
| } |
| } |
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15777028.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步