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=你自己的密码