接(java+mysql图形界面开发(上)---- ----图文并茂酒店预订系统),本篇主要介绍登陆页面注册按钮的具体实现及数据库中所用到两个表(房间信息表,用户注册信息表)的具体创建和相关代码运行中所用到的sql语句的具体实现,话不多说,开整。
if(e.getActionCommand()=="注册")
{
R1.dispose();
new zhuce();
}
class zhuce extends JFrame implements ActionListener{
public static String driver="com.mysql.cj.jdbc.Driver";
public static String URL="jdbc:mysql://localhost:3306/hotel?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
public static String username="root";
public static String password="828924";
JFrame A1;
JPanel A2,A3;
JLabel A4,A5,A6,A7;
JTextField D1,D2,D3,D4;
JButton B1,B2;
public zhuce()
{
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(Exception e) {
System.out.println(e);
}
A1=new JFrame();
A1.setLayout(new BorderLayout());
A2=new JPanel();
A2.setLayout(new GridLayout(4,2));//设置五行两列多一重复输入密码验证相等
A3=new JPanel();
A4=new JLabel("用户名");A5=new JLabel("密码");A6=new JLabel("身份证号");A7=new JLabel("手机");
D1=new JTextField(20);
D2=new JTextField(20);
D3=new JTextField(20);
D4=new JTextField(20);
B1=new JButton("确认");B2=new JButton("取消");
B1.addActionListener(this);
B2.addActionListener(this);
A2.add(A4);A2.add(D1);
A2.add(A5);A2.add(D2);
A2.add(A6);A2.add(D3);
A2.add(A7);A2.add(D4);
A3.add(B1);A3.add(B2);
A1.add(A2,BorderLayout.CENTER);
A1.add(A3,BorderLayout.SOUTH);
A1.pack();
A1.setVisible(true);
A1.setLocationRelativeTo(null);
A1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Class.forName(driver);
Connection con2=DriverManager.getConnection(URL,username,password);
if(e.getActionCommand()=="确认")
{
if(D1.getText()==null||D1.getText().equals("")||(D2.getText()==null||D2.getText().equals(""))||(D3.getText()==null||D3.getText().equals(""))||(D4.getText()==null||D4.getText().equals("")))
{
JOptionPane.showMessageDialog(null,"请正确输入","标题",JOptionPane.ERROR_MESSAGE);
}
else
{
String n1=D1.getText();
String n2=D2.getText();
String n3=D3.getText();
String n4=D4.getText();
Statement skt1=con2.createStatement();
String sql2="select count(*) from room_customer";
ResultSet r1=skt1.executeQuery(sql2);
String a = null;
while(r1.next())
{
a=r1.getString(1);
//System.out.println(a);
}
int w=Integer.parseInt(a);
String sql3="select * from room_customer";
ResultSet set3=skt1.executeQuery(sql3);
int t=0;
while(set3.next())
{
if(D1.getText().equals(set3.getString(1)))
{
JOptionPane.showMessageDialog(null,"该用户已存在","标题",JOptionPane.ERROR_MESSAGE);
}
t++;
}
if(t==w)
{
PreparedStatement pre=con2.prepareStatement("insert into room_customer values(?,?,?,?)");
pre.setString(1, n1);
pre.setString(2, n2);
pre.setString(3, n3);
pre.setString(4, n4);
pre.execute();
pre.close();
}
set3.close();
r1.close();
skt1.close();
}
}
con2.close();
if(e.getActionCommand()=="取消")
{
System.exit(0);
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
在这里输入用户名并填完信息后,根据sql查询语句得到客户表room_customer中的客户人数,再通过遍历与输入的用户名作比较,若用户名已存在,则显示如下报错界面,否则将该客户信息保存到客户表room_customer中去,并提示注册成功.
//创建表
CREATE TABLE IF NOT EXISTS room(
id INT AUTO_INCREMENT PRIMARY KEY,
type_room VARCHAR(20) NOT NULL,
price INT NOT NULL,
status_room VARCHAR(20) NOT NULL
);
//增加数据
INSERT INTO room VALUES(1,"大床房",138,"空闲"),
(2,"大床房",138,"空闲"),
(3,"大床房",138,"空闲"),
(4,"大床房",138,"空闲"),
(5,"商务房",178,"空闲"),
(6,"商务房",178,"空闲"),
(7,"商务房",178,"空闲"),
(8,"商务房",178,"空闲"),
(9,"双人间",118,"空闲"),
(10,"双人间",118,"空闲"),
(11,"双人间",118,"空闲"),
(12,"双人间",118,"空闲"),
(13,"家庭房",198,"空闲"),
(14,"家庭房",198,"空闲"),
(15,"家庭房",198,"空闲"),
(16,"家庭房",198,"空闲");
这里解释下在创建此表的过程中为何要在id属性列设置自增(AUTO_INCREMENT)和主键(PRIMARY KEY):(纯属本人理解)自增是伴随着唯一性的 设置自增的字段必须得设为主键,否则会报错.
//创建表
CREATE TABLE IF NOT EXISTS room_customer(
name_1 VARCHAR(20) NOT NULL,
mima VARCHAR(20) NOT NULL,
sfzh VARCHAR(20) NOT NULL,
phone VARCHAR(20) NOT NULL
);
//增加数据
INSERT INTO room_customer VALUES("张三","zs","311422533644","09112"),
"李四","ls","311422533645","09113");
//填入更多信息可以通过登陆界面的注册按钮来实现
SELECT * FROM room;//查看房间的全部信息
SELECT * FROM room WHERE id=num;//查看指定房间号的房间信息
SELECT * FROM room WHERE type_room =?;//选择房间类型查看相关信息
SELECT * FROM room WHERE status_room =?;//选择房间状态查看相关信息
SELECT * FROM room_customer;//得到全部客户信息
SELECT * FROM room WHERE price BETWEEN ? AND ?;//输入两个价格参数查看这个区间的房间信息
SELECT COUNT(*) FROM room_customer;//得到客户的个数
欢迎广大小伙伴来访,你们的雅正是我的荣幸,如还有问题请在下面评论,博主会一一详解,点赞就行,禁止白嫖哦