Javafx实现登陆注册功能(数据库存储)

本文介绍了使用JavaFX构建的登录界面,包括输入验证、数据库操作(user_exist(), user_right(), detection_information())及注册功能。展示了如何通过JDBC连接数据库查询用户信息并处理登录和注册过程中的错误情况。
摘要由CSDN通过智能技术生成
package com.zht.ui;

import com.zht.JavaBigJob.jdbcUtils;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginUI extends Application{
    Label nameLabel = new Label("User Name :");
    Label passwordLabel = new Label("Password  : ");

    HBox user = new HBox();
    HBox password = new HBox();
    TextField tfUser = new TextField();
    PasswordField tfPassword = new PasswordField();
    Button btLogIn = new Button("Log in");
    Button btSignIn = new Button("Sign in");
    HBox h3 = new HBox();//装按钮
    VBox pane = new VBox();//单列面板

    @Override
    public void start(Stage stage) {
        user.getChildren().addAll(nameLabel,tfUser);
        user.setAlignment(Pos.CENTER);
        user.setSpacing(20);

        password.getChildren().addAll(passwordLabel,tfPassword);
        password.setAlignment(Pos.CENTER);
        password.setSpacing(20);

        h3.setAlignment(Pos.CENTER);
        btLogIn.setAlignment(Pos.BASELINE_RIGHT);
        btSignIn.setAlignment(Pos.BASELINE_RIGHT);
        h3.getChildren().addAll(btLogIn,btSignIn);
        h3.setSpacing(20);

        pane.setAlignment(Pos.CENTER);
        pane.setSpacing(20);
        pane.getChildren().addAll(user,password,h3);

        stage.setScene(new Scene(pane,400,250));
        stage.setTitle("Welcome!");
        stage.show();

        btLogIn.setOnAction(e->{
            if(user_exist()==false){
                System.out.println("用户不存在");
                HBox hBox = new HBox();
                Label label = new Label("用户不存在");
                Image image= null;
                try {
                    image = new Image(new FileInputStream("src/com02/No.jpg"));
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
                ImageView imageView=new ImageView();
                imageView.setImage(image);
                imageView.setFitWidth(150);
                imageView.setFitHeight(120);
                hBox.setAlignment(Pos.CENTER);
                hBox.setSpacing(10);
                hBox.getChildren().addAll(imageView);
                Stage stage1 = new Stage();
                stage1.setScene(new Scene(hBox,300,200));
                stage1.setTitle("ERROR");
                stage1.show();
            }
            else if(user_right() && detection_information()){
                stage.hide();
                //登陆成功
                System.out.println("登陆成功");
            }
        });

        //注册功能
        btSignIn.setOnAction(e->{
            if(user_exist()){
                System.out.println("用户已存在");
                HBox hBox = new HBox();
                Label label = new Label("用户已存在");
                Image image= null;
                try {
                    image = new Image(new FileInputStream("src/com02/No.jpg"));
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
                ImageView imageView=new ImageView();
                imageView.setImage(image);
                imageView.setFitWidth(150);
                imageView.setFitHeight(120);
                hBox.setAlignment(Pos.CENTER);
                hBox.setSpacing(10);
                hBox.getChildren().addAll(imageView,label);
                Stage stage1 = new Stage();
                stage1.setScene(new Scene(hBox,300,200));
                stage1.setTitle("ERROR");
                stage1.show();
            }
            //账户不存在且信息无误
            else if(detection_information())
            {
                try {
                    Connection conn=jdbcUtils.getConnection();
                    String sql = "insert into user (username,password) values (?,?)";
                    PreparedStatement ps=conn.prepareStatement(sql);
                    ps.setString(1,tfUser.getText());
                    ps.setString(2,tfPassword.getText());
                    ps.executeUpdate();
                    //释放资源
                    jdbcUtils.close(conn,ps);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

                HBox hBox = new HBox();
                Label label = new Label("注册成功!");
                Image image= null;
                try {
                    image = new Image(new FileInputStream("src/com02/yes.jpg"));
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
                ImageView imageView=new ImageView();
                imageView.setImage(image);
                imageView.setFitWidth(150);
                imageView.setFitHeight(120);
                hBox.setAlignment(Pos.CENTER);
                hBox.setSpacing(10);
                hBox.getChildren().addAll(imageView,label);
                Stage stage1 = new Stage();
                stage1.setScene(new Scene(hBox,300,200));
                stage1.show();
            }
        });
    }

    public boolean user_exist(){//判断是否已存在用户

        String sql = "select count(*) from user where username = '"+tfUser.getText()+"'";
        try {
            Connection conn=jdbcUtils.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            rs.next();
            if(rs.getInt(1)==1){
                jdbcUtils.close(conn,ps,rs);
                return true;
            }
            jdbcUtils.close(conn,ps,rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return false;
    }
    public boolean user_right(){
        String sql = "select count(*) from user where username = ? and password = ?";
        try {
            Connection conn=jdbcUtils.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,tfUser.getText());
            ps.setString(2,tfPassword.getText());
            ResultSet rs=ps.executeQuery();
            rs.next();
            if(rs.getInt(1)==1){
                jdbcUtils.close(conn,ps,rs);
                return true;
            }
            jdbcUtils.close(conn,ps,rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("用户名或密码不正确");
        HBox hBox = new HBox();
        Label label = new Label("用户名或密码不正确");
        ImageView image = new ImageView("No.jpg");
        image.setFitWidth(150);
        image.setFitHeight(120);
        hBox.setAlignment(Pos.CENTER);
        hBox.setSpacing(10);
        hBox.getChildren().addAll(image,label);
        Stage stage1 = new Stage();
        stage1.setScene(new Scene(hBox,300,200));
        stage1.setTitle("ERROR");
        stage1.show();
        return false;
    }

    public boolean detection_information(){//判断信息是否填写完全

        if (tfUser.getText().equals("")||tfPassword.getText().equals("")){
            System.out.println("信息不全!");
            HBox hBox = new HBox();
            Label label = new Label("用户信息填写不全!");
            Image image= null;
            try {
                image = new Image(new FileInputStream("src/com02/No.jpg"));
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            ImageView imageView=new ImageView();
            imageView.setImage(image);
            imageView.setFitWidth(150);
            imageView.setFitHeight(120);
            hBox.setAlignment(Pos.CENTER);
            hBox.setSpacing(10);
            hBox.getChildren().addAll(imageView,label);
            Stage stage1 = new Stage();
            stage1.setScene(new Scene(hBox,300,200));
            stage1.setTitle("ERROR");
            stage1.show();
            return false;//信息不全
        }
        return true;//信息全
    }

}


package com.zht.JavaBigJob;

import java.sql.*;
import java.util.Properties;

public class jdbcUtils {
    //获取数据连接对象时数据写死,怎么办,传参数呗,把参数放进文件里,这样就比较灵活
    private static String driver;
    private static String url;
    private static String username;
    private static String password;



    static {
        try {
            //给这些变量赋值,用到配置文件
            Properties p=new Properties();
            //将文件中的数据加载到集合里(classpath路径下)
            p.load(jdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            //这里还有另外一种写法
            driver=p.getProperty("driver");
            url = p.getProperty("url");
            username = p.getProperty("username");
            password = p.getProperty("password");
            //注册驱动
            Class.forName(driver);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        Connection conn=DriverManager.getConnection(url,username,password);
        return conn;
    }

    //增删改
    public static void close(Connection conn, PreparedStatement ps) {

        close(conn, ps, null);
    }

    //查
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        //如果不进行非空校验,很容易出现空指针异常
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

底下是jdbcutils的代码
这样写,主要是为了避免重复注册驱动浪费资源
还有jdbc.properties文件,方便对用户名代码进行修改,同时变量可以自己定义,采用键值对的方式
如:driver=driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/login
username=root
password=你自己的密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值