使用javafx建立简单的酒店客房管理系统(二):简单登陆,连接数据库,修改数据

 

一个简单的登陆页面

具体程序可以在https://github.com/MasterEE/javafx-/tree/master下载

javafx中页面是用 .fxml 设计的 可以在 .class 文件中控制

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane alignment="center" hgap="10" prefHeight="236.0" prefWidth="352.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.LoginController">
    <Label text="用户名:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
    <TextField fx:id="loginname" GridPane.columnIndex="1" GridPane.rowIndex="1" />
    <Label text="密码:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
    <PasswordField fx:id="loginpassword" GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <Button onAction="#handleSubmitButtonAction" text="登陆" GridPane.columnIndex="1" GridPane.rowIndex="3" />
     <ComboBox promptText="选择身份" GridPane.rowIndex="3" fx:id="choose">
         <items>
            <FXCollections fx:factory="observableArrayList">
             <String fx:value="客户" />
             <String fx:value="管理员" />
            </FXCollections>
         </items>
     </ComboBox>
    <columnConstraints>
        <ColumnConstraints />
        <ColumnConstraints />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

</GridPane>
 fx:controller="sample.LoginController"

上面这行就是.fxml中控制的文件

下面是LoginController文件

package sample;

import javafx.collections.FXCollections;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class LoginController {
    public boolean useridentity = false;
    @FXML
    private TextField loginname;
    @FXML
    private PasswordField loginpassword;
    @FXML
    private ComboBox choose;

    public void handleSubmitButtonAction(ActionEvent event) {
        String username = loginname.getText();
        String password = loginpassword.getText();
        if (choose.getValue().equals("管理员")) {
            if (DatabsaeOperate.Connect_adminslogin(username, password)) {
                useridentity = true;
                Stage primaryStage = new Stage();
                try {
                    adminlogin_next(primaryStage);
                } catch (Exception e) {

                }
                ((Node) (event.getSource())).getScene().getWindow().hide();//关闭窗口
            }
        } else if (choose.getValue().equals("客户")) {
            if (DatabsaeOperate.Connect_userslogin(username, password)) {
                useridentity = true;
                Stage primaryStage = new Stage();
                try {
                    userslogin_next(primaryStage);
                } catch (Exception e) {

                }
                ((Node) (event.getSource())).getScene().getWindow().hide();//关闭窗口
            }
        }
    }

    //登陆后下一个窗口
    public void adminlogin_next(Stage primaryStage) throws Exception {
        //choose.setItems(FXCollections.observableArrayList("One","Two","Three"));
        Parent root1 = FXMLLoader.load(getClass().getResource("systemoperate.fxml"));
        primaryStage.setTitle("酒店客房管理系统");
        primaryStage.setScene(new Scene(root1, 900, 500));
        primaryStage.show();
    }

    public void userslogin_next(Stage primaryStage) throws Exception {
        //choose.setItems(FXCollections.observableArrayList("One","Two","Three"));
        Parent root1 = FXMLLoader.load(getClass().getResource("Customerview.fxml"));
        primaryStage.setTitle("选择客房");
        primaryStage.setScene(new Scene(root1, 900, 500));
        primaryStage.show();
    }
}

登陆后切换页面到SystemController.fxml 页面

需要注意的是这里面有连接数据库中用户名密码表,并进行判断的函数

DatabsaeOperate.Connect_adminslogin(username, password)

 

连接数据库和进行操作的函数

package sample;

import java.sql.*;

public class DatabaseConnect {
    private static String DRIVER="com.mysql.jdbc.Driver";
    private static String URL = "jdbc:mysql://localhost:3306/hotelsystem";
    private static final String USER = "root";
    private static final String PASSWORD = "xiaoxiao";
    public  Connection dbConnection() {
        Connection con = null;
        try {
            // 加载驱动类
            Class.forName(DRIVER);
            long start = System.currentTimeMillis();

            // 建立连接
            con = DriverManager.getConnection(URL, USER, PASSWORD);
            long end = System.currentTimeMillis();
            System.out.println(con);
            System.out.println("建立连接耗时: " + (end - start) + "ms 毫秒");

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return con;
    }
}
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DatabsaeOperate {

    Connection conn =null;
    public static boolean Connect_adminslogin(String loginname1, String loginpassword1) {
        ResultSet rs = null;
        Statement stmt = null;

        DatabaseConnect databaseConnect = new DatabaseConnect();
        Connection con = databaseConnect.dbConnection();

        try {
            // 创建Statement对象
            stmt = con.createStatement();

            // 执行SQL语句
            rs = stmt.executeQuery("select * from 管理员");
            // System.out.println("账号\t密码");
            while (rs.next()) {
                if (loginname1.equals(rs.getString(1))&&loginpassword1.equals(rs.getString(2)))
                    return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static boolean Connect_userslogin(String loginname1, String loginpassword1) {
        ResultSet rs = null;
        Statement stmt = null;

        DatabaseConnect databaseConnect = new DatabaseConnect();
        Connection con = databaseConnect.dbConnection();

        try {
            // 创建Statement对象
            stmt = con.createStatement();

            // 执行SQL语句
            rs = stmt.executeQuery("select * from 客户");
            // System.out.println("账号\t密码");
            while (rs.next()) {
                if (loginname1.equals(rs.getString(1))&&loginpassword1.equals(rs.getString(2)))
                    return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static ObservableList Connect_searchroom(){
            //Connection conn = null;
            ResultSet rs = null;
            Statement stmt = null;

            DatabaseConnect databaseConnect = new DatabaseConnect();
            Connection con = databaseConnect.dbConnection();

            ObservableList<Room> Roomslist = FXCollections.observableArrayList();

            try {
                // 创建Statement对象
                stmt = con.createStatement();

                // 执行SQL语句
                rs = stmt.executeQuery("select * from 客房信息");
                // System.out.println("账号\t密码");
                while (rs.next()) {
                    Room room =new Room();
                    System.out.println(rs.getInt(1)+' '+rs.getString(3)+'\t');//+rs.getInt(3));
                    room.setRoomnumber(rs.getInt(1));
                    room.setRommstate(rs.getString(2));
                    room.setRoomprice(rs.getInt(3));

                    Roomslist.add(room);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
         return Roomslist;
    }
    public static void Connect_add(Customer customer) {
        Statement stmt = null;

        DatabaseConnect databaseConnect = new DatabaseConnect();
        Connection con = databaseConnect.dbConnection();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String date1=temp.format(customer.getIndata());
//        String date2=temp.format(customer.getOutdata());
    //    System.out.println(date1);
        try {
            // 创建Statement对象
            String sql1="insert into 客户登记信息 value(?,?,?,?,?)";
            PreparedStatement ps1 = con.prepareStatement(sql1);
            //stmt = con.createStatement();
            ps1.setString(1,customer.getName());
            ps1.setString(2,customer.getIdnumber());
            ps1.setString(3,customer.getRoomnumber());
            String t1 = df.format(customer.getIndata());
            String t2 =df.format(customer.getOutdata());
            System.out.println(t1);
            System.out.println(t2);
            ps1.setString(4, t1);
            ps1.setString(5, t2);
            // 执行SQL语句
            System.out.println(customer.getIndata());
            ps1.executeUpdate();
            String sql2="update 客房信息 set 状态=? where 房间号=?";
            PreparedStatement ps2 = con.prepareStatement(sql2);
            String str="full";
            ps2.setString(1,str);
            ps2.setString(2,customer.getRoomnumber());
            ps2.executeUpdate();
        } catch (SQLException e) {

            e.printStackTrace();
        }
    }
    public static ObservableList Connect_searchcustomer(){
        //Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        DatabaseConnect databaseConnect = new DatabaseConnect();
        Connection con = databaseConnect.dbConnection();

        ObservableList<Customer> customerslist = FXCollections.observableArrayList();

        try {
            // 创建Statement对象
            stmt = con.createStatement();

            // 执行SQL语句
            rs = stmt.executeQuery("select * from 客户登记信息");
            while (rs.next()) {
                Customer customer =new Customer();
                System.out.println(rs.getString(1)+' '+rs.getString(3)+'\t');//+rs.getInt(3));
                customer.setName(rs.getString(1));
                customer.setIdnumber(rs.getString(2));
                customer.setRoomnumber(rs.getString(3));
                customer.setIndata(rs.getDate(4));
                customer.setOutdata(rs.getDate(5));
                customerslist.add(customer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return customerslist;
    }

    public static void Connect_back(Customer customer){
        Statement stmt = null;

        DatabaseConnect databaseConnect = new DatabaseConnect();
        Connection con = databaseConnect.dbConnection();

        try {
            String sql1="delete from 客户登记信息 "+" where 身份证= "+customer.getIdnumber()+";";
            PreparedStatement ps1 = con.prepareStatement(sql1);
            ps1.executeUpdate();
            // 执行SQL语句
            String sql2="update 客房信息 set 状态=? where 房间号=?";
            PreparedStatement ps2 = con.prepareStatement(sql2);
            String str="empty";
            ps2.setString(1,str);
            ps2.setString(2,customer.getRoomnumber());
            ps2.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public static ObservableList Connect_searchgoods(){
        //Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        DatabaseConnect databaseConnect = new DatabaseConnect();
        Connection con = databaseConnect.dbConnection();

        ObservableList<Goods> Goodslist = FXCollections.observableArrayList();

        try {
            // 创建Statement对象
            stmt = con.createStatement();

            // 执行SQL语句
            rs = stmt.executeQuery("select * from 商品");
            // System.out.println("账号\t密码");
            while (rs.next()) {
                Goods goods =new Goods();
               // System.out.println(rs.getInt(1)+' '+rs.getString(3)+'\t');//+rs.getInt(3));
                goods.setGoodsname(rs.getString(1));
                goods.setPrice(rs.getInt(2));
                goods.setNumber(rs.getInt(3));

                Goodslist.add(goods);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Goodslist;
    }

}

 

 

  • 4
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值