图形用户界面(Graphics User Interface,GUI)是用户与程序交互的窗口,比命令行的界面更加直观并且更好操作
class into 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 R1;
JPanel P1,P2;
JLabel L1,L2;
JTextField F1,F2;
JButton B1,B2,B3;
public into()
{
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);
}
R1=new JFrame();
R1.setLayout(new BorderLayout());
P1=new JPanel();
P1.setLayout(new GridLayout(2,2,-10,5));
P2=new JPanel();
L1=new JLabel("用户名");
L2=new JLabel("密码");
F1=new JTextField(10);
F2=new JTextField(10);
B1=new JButton("登录");
B1.addActionListener(this);
B2=new JButton("注册");
B2.addActionListener(this);
B3=new JButton("退出");
B3.addActionListener(this);
P1.add(L1);
P1.add(F1);
P1.add(L2);
P1.add(F2);
P2.add(B1);
P2.add(B2);
P2.add(B3);
R1.add(P1,BorderLayout.CENTER);
R1.add(P2,BorderLayout.SOUTH);
R1.pack();
R1.setVisible(true);
R1.setLocationRelativeTo(null);
R1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Class.forName(driver);
Connection con1=DriverManager.getConnection(URL,username,password);
//System.out.println("连接成功");
Statement stat1=con1.createStatement();
String sql2="select * from room_customer";
ResultSet set1=stat1.executeQuery(sql2);
if(e.getActionCommand()=="登录")
{
// R1.dispose();
// new P2();
if(F1.getText()==null || F1.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入正确的用户名","标题",JOptionPane.ERROR_MESSAGE);
}
else
{
while(set1.next())
{
if(F1.getText().equals(set1.getString(1)))
{
//System.out.println(set1.getString(1));
if(F2.getText().equals(set1.getString(2)))
{
//System.out.println(set1.getString(2));
String stri=set1.getString(1);
R1.dispose();
new P2(stri);
}
else
JOptionPane.showMessageDialog(null, "密码错误","标题",JOptionPane.ERROR_MESSAGE);
}
}
}
}
if(e.getActionCommand()=="注册")
{
R1.dispose();
new zhuce();
}
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();
}
}
这里是引用 实现监听登录按钮时通过与数据库hotel中的表room_customer连接实现数据互通,通过输入行F1,F2与表中对应数据作比较来显示下一界面
以下为登录后相关功能的实现及数据库的相关操作
if(e.getActionCommand()=="查询全部房间信息")
{
JFrame Y1=new JFrame();
JScrollPane S1=new JScrollPane();
JTable T1;
Object a1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object a2[][]=new Object[17][4];
try {
Class.forName(driver);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Connection con=DriverManager.getConnection(URL,username,password);
Statement stat=con.createStatement();
String sql1="select * from room";
ResultSet set=stat.executeQuery(sql1);
int i=0;
while(set.next())
{
for(int j=0;j<4;j++)
{
a2[i][j]=set.getString(j+1);
}
i++;
}
set.close();
stat.close();
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
T1=new JTable(a2,a1);
//S1.setBounds(200, 100, 550, 650);
S1.setViewportView(T1);
T1.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
T1.setDefaultRenderer(Object.class,r);
Y1.add(S1);
Y1.pack();
Y1.setLocationRelativeTo(null);
Y1.setDefaultCloseOperation(EXIT_ON_CLOSE);
Y1.setVisible(true);
}
由于博主在编写代码时将其代码全部归与一个主类,所以有的代码前面并没有导入包,相关全部代码在下面将给出链接。
通过JTable将数据库中的数据以表格形式显示清晰明了
if(e.getActionCommand()=="按房间号查询房间")
{
String number=JOptionPane.showInputDialog(null,"请输入房间号:\n","title",JOptionPane.PLAIN_MESSAGE);
//System.out.println(number);//检测输入
if(number.length()==0)
{
JOptionPane.showMessageDialog(null, "请正确输入房间号!╮(╯▽╰)╭", "标题",JOptionPane.ERROR_MESSAGE);
}
else
{
int n = JOptionPane.showConfirmDialog(null, "是否确定?", "标题",JOptionPane.YES_NO_OPTION); //返回值为0或1
if(n==0)
{
//System.out.println("是");
try {
int num=Integer.parseInt(number);
System.out.println(num);
getClass().forName(driver);
Connection con1=DriverManager.getConnection(URL,username,password);
Statement stat1=con1.createStatement();
String sql2="select * from room where id="+num;
ResultSet set1=stat1.executeQuery(sql2);
JFrame X1=new JFrame();
JScrollPane X2=new JScrollPane();
JTable X3;
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[2][4];
int i=0;
while(set1.next())
{
for(int j=0;j<4;j++)
{
x2[i][j]=set1.getString(j+1);
}
i++;
}
X3=new JTable(x2,x1);
X2.setViewportView(X3);
X3.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
X3.setDefaultRenderer(Object.class,r);
X1.add(X2);
X1.pack();
X1.setLocationRelativeTo(null);
X1.setDefaultCloseOperation(EXIT_ON_CLOSE);
X1.setVisible(true);
set1.close();
stat1.close();
con1.close();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
{
System.out.println(1);
}
}
}
if(e.getActionCommand()=="按房间类型以查询")
{
Object[] obj2 ={
"大床房", "双人间", "商务房","家庭房" };
String s = (String) JOptionPane.showInputDialog(null,"请选择房间类型:\n", "身份", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "足球");
System.out.println(s);
//System.out.println("是");
try {
getClass().forName(driver);
Connection con1=DriverManager.getConnection(URL,username,password);
PreparedStatement stat1=con1.prepareStatement(" select * from room where type_room =?");
stat1.setString(1, s);
ResultSet stat=stat1.executeQuery();
JFrame X1=new JFrame();
JScrollPane X2=new JScrollPane();
JTable X3;
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[5][4];
int i=0;
while(stat.next())
{
for(int j=0;j<4;j++)
{
x2[i][j]=stat.getString(j+1);
}
i++;
}
X3=new JTable(x2,x1);
X2.setViewportView(X3);
X3.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
X3.setDefaultRenderer(Object.class,r);
X1.add(X2);
X1.pack();
X1.setLocationRelativeTo(null);
X1.setDefaultCloseOperation(EXIT_ON_CLOSE);
X1.setVisible(true);
stat.close();
stat1.close();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
房间类型有四个选项
if(e.getActionCommand()=="按房间价格查询")
{
price x1=new price();
}
实现单一价格查询
class price 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 M1;
JPanel M2,M3;
JLabel M4,M5;
JTextField M6,M7;
JButton B1,B2,B3;
public price()
{
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);
}
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[17][4];
M1=new JFrame();
M1.setLayout(new BorderLayout());
M2=new JPanel();
M2.setLayout(new GridLayout(2,2));
M3=new JPanel();
M4=new JLabel("最低价");
M5=new JLabel("最高价");
M6=new JTextField(10);
M7=new JTextField(10);
B1=new JButton("确定");
B1.addActionListener(this);
B2=new JButton("返回");
B2.addActionListener(this);
B3=new JButton("退出");
B3.addActionListener(this);
M2.add(M4);M2.add(M6);
M2.add(M5);M2.add(M7);
M3.add(B1);M3.add(B2);M3.add(B3);
M1.add(M2,BorderLayout.CENTER);
M1.add(M3,BorderLayout.SOUTH);
M1.pack();
M1.setLocationRelativeTo(null);
M1.setDefaultCloseOperation(EXIT_ON_CLOSE);
M1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="确定")
{
try {
getClass().forName(driver);
Connection con1=DriverManager.getConnection(URL,username,password);
PreparedStatement stat1=con1.prepareStatement(" select * from room where price between ? and ?");
M1.dispose();
//System.out.println(M6.getText());
if((M6.getText()==null||M6.getText().equals(""))||(M7.getText()==null||M7.getText().equals("")))
{
JOptionPane.showMessageDialog(null, "请重新输入","标题",JOptionPane.ERROR_MESSAGE);
}
else
{
String t1=M6.getText();
String t2=M7.getText();
// int a1=Integer.parseInt(M6.getText());
// int a2=Integer.parseInt(M7.getText());
stat1.setString(1, t1);
stat1.setString(2, t2);
try {
ResultSet set1=stat1.executeQuery();
JFrame X1=new JFrame();
JScrollPane X2=new JScrollPane();
JTable X3;
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[17][4];
int i=0;
while(set1.next())
{
for(int j=0;j<4;j++)
{
x2[i][j]=set1.getString(j+1);
}
i++;
}
X3=new JTable(x2,x1);
X2.setViewportView(X3);
X3.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
X3.setDefaultRenderer(Object.class,r);
X1.add(X2);
X1.pack();
X1.setLocationRelativeTo(null);
X1.setDefaultCloseOperation(EXIT_ON_CLOSE);
X1.setVisible(true);
set1.close();
stat1.close();
con1.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
实现单一价格查询
if(e.getActionCommand()=="按房间状态查询")
{
Object[] obj2 ={
"空闲","非空闲"};
String s = (String) JOptionPane.showInputDialog(null,"请选择房间状态:\n", "身份", JOptionPane.PLAIN_MESSAGE, null, obj2, "空闲");
//System.out.println(s);
//System.out.println("是");
try {
getClass().forName(driver);
Connection con1=DriverManager.getConnection(URL,username,password);
PreparedStatement stat1=con1.prepareStatement(" select * from room where status_room =?");
stat1.setString(1, s);
ResultSet stat=stat1.executeQuery();
JFrame X1=new JFrame();
JScrollPane X2=new JScrollPane();
JTable X3;
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[17][4];
int i=0;
while(stat.next())
{
for(int j=0;j<4;j++)
{
x2[i][j]=stat.getString(j+1);
}
i++;
}
X3=new JTable(x2,x1);
X2.setViewportView(X3);
X3.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
X3.setDefaultRenderer(Object.class,r);
X1.add(X2);
X1.pack();
X1.setLocationRelativeTo(null);
X1.setDefaultCloseOperation(EXIT_ON_CLOSE);
X1.setVisible(true);
stat.close();
stat1.close();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
class ding 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";
public static String rr;
JFrame W1;
JPanel X1,G1;
JLabel S1;
JButton B1,B2;
JScrollPane X2;
public ding(String ee) throws Exception
{
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);
}
String s=JOptionPane.showInputDialog(null,"请输入预订房间号","标题",JOptionPane.PLAIN_MESSAGE);
if(s==null || s.equals(""))
{
JOptionPane.showMessageDialog(null,"请重新输入","标题",JOptionPane.ERROR_MESSAGE);
}
else
{
Class.forName(driver);
Connection cor=DriverManager.getConnection(URL,username,password);
PreparedStatement ssr=cor.prepareStatement("select status_room from room where id=?");
ssr.setString(1, s);
ResultSet ddl=ssr.executeQuery();
String ssy=null;
while(ddl.next())
{
ssy=ddl.getString(1);
}
if(ssy==null || ssy.equals("非空闲"))
{
JOptionPane.showMessageDialog(null,"房间不存在或他人预订","标题",JOptionPane.ERROR_MESSAGE);
}
else
{
Class.forName(driver);
Connection con=DriverManager.getConnection(URL,username,password);
PreparedStatement pre=con.prepareStatement("update room set status_room =? where id=?");
pre.setString(1,"非空闲");
pre.setString(2, s);
pre.executeUpdate();
pre.close();
///将房间状态改为非空闲
PreparedStatement pre1=con.prepareStatement("select * from room where id=?");
pre1.setString(1,s);
ResultSet set1=pre1.executeQuery();
Object a2[][]=new Object[1][4];
while(set1.next())
{
for(int i=0;i<4;i++)
{
a2[0][i]=set1.getString(i+1);
}
}
set1.close(); pre1.close();
//将数组中内容添加到新表中,判断存在与否,操作后将表删除,达到查看客户已订房订单
rr=ee; //将登陆用户名传入
//System.out.println(rr);
String sss=rr;
PreparedStatement st=con.prepareStatement("create table if not exists "+sss+"(id int,type_r varchar(20),price_r int,status_r varchar(20))");
//st.setString(1, sss);
st.execute();
st.close();
/创建名为用户名的订单生成表
mysql不支持表名参数传入
PreparedStatement stt=con.prepareStatement("insert into "+sss+" values(?,?,?,?)");
String a1=(String) a2[0][0];
String a3=(String) a2[0][1];
String a4=(String) a2[0][2];
String a5=(String) a2[0][3];
stt.setString(1, a1);
stt.setString(2, a3);
stt.setString(3, a4);
stt.setString(4, a5);
stt.execute();
stt.close();
//将选择订单存入特定表中//成功
int cc=JOptionPane.showConfirmDialog(null,"是否查看你的订单","标题",JOptionPane.YES_NO_OPTION);
//是为0,否为1
if(cc==1)
System.exit(0);
else
{
PreparedStatement sst=con.prepareStatement("select * from "+sss);
ResultSet ssw=sst.executeQuery();
W1=new JFrame();
W1.setLayout(new BorderLayout());
X1=new JPanel();
G1=new JPanel();
S1=new JLabel("你的订单:");
X2=new JScrollPane();
JTable X3;
B1=new JButton("返回");
B1.addActionListener(this);
B2=new JButton("退出");
B2.addActionListener(this);
G1.add(B1);
G1.add(B2);
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[17][4]; //一个人只能订一间房,可改进
int i=0;
while(ssw.next())
{
for(int j=0;j<4;j++)
{
x2[i][j]=ssw.getString(j+1);
}
i++;
}
X3=new JTable(x2,x1);
X2.setViewportView(X3);
X3.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
X3.setDefaultRenderer(Object.class,r);
X1.add(S1);
X1.add(X2);
W1.add(X1,BorderLayout.CENTER);
W1.add(G1,BorderLayout.SOUTH);
W1.pack();
W1.setLocationRelativeTo(null);
W1.setDefaultCloseOperation(EXIT_ON_CLOSE);
W1.setVisible(true);
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="退出")
{
System.exit(0);
}
if(e.getActionCommand()=="返回")
{
W1.setVisible(false);
new P2(null); //传入参数为null
}
}
}
if(e.getActionCommand()=="订房")
{
///
try {
new ding(ww);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
订房类实现细节(数据库相关表列中值的改变等相关增删改查操作)这里就不作过多解释,有问题的小伙伴可以评论出来我一一解答。
if(e.getActionCommand()=="退房")
{
///
try {
new tuichu(ww);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
class tuichu 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";
public static String names;
JFrame W1;
JPanel X1,G1;
JLabel S1,S2;
JButton B1,B2;
JScrollPane X2;
JTextField T1;
public tuichu(String gg) throws Exception
{
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);}
names=gg;
Class.forName(driver);
Connection con=DriverManager.getConnection(URL,username,password);
PreparedStatement sst=con.prepareStatement("select * from "+gg);
ResultSet ssw=sst.executeQuery();
W1=new JFrame();
W1.setLayout(new BorderLayout());
X1=new JPanel();
G1=new JPanel();
S1=new JLabel("你的订单:");
S2=new JLabel("输入房号退房:");
T1=new JTextField(7);
X2=new JScrollPane();
JTable X3;
B1=new JButton("确定");
B1.addActionListener(this);
B2=new JButton("退出");
B2.addActionListener(this);
G1.add(S2);
G1.add(T1);
G1.add(B1);
G1.add(B2);
Object x1[]= {
"房间号","房间类型","房间价格","房间状态"};
Object x2[][]=new Object[17][4];
int i=0;
while(ssw.next())
{
for(int j=0;j<4;j++)
{
x2[i][j]=ssw.getString(j+1);
}
i++;
}
X3=new JTable(x2,x1);
X2.setViewportView(X3);
X3.setRowHeight(35);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
X3.setDefaultRenderer(Object.class,r);
X1.add(S1);
X1.add(X2);
W1.add(X1,BorderLayout.CENTER);
W1.add(G1,BorderLayout.SOUTH);
W1.pack();
W1.setLocationRelativeTo(null);
W1.setDefaultCloseOperation(EXIT_ON_CLOSE);
W1.setVisible(true);
ssw.close();
sst.close();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(URL,username,password);
String SSG=T1.getText();
if(e.getActionCommand()=="退出")
System.exit(0);
if(e.getActionCommand()=="确定")
{
if(T1.getText()==null || T1.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "请正确输入房间号","标题",JOptionPane.ERROR_MESSAGE);
}
else
{
Statement st=con.createStatement();
String STR="delete from "+names+" where id="+SSG;
st.executeUpdate(STR);
st.close();
///删除个人订单表中的房间信息
Statement stt=con.createStatement();
String sqla="update room set status_room='空闲' where id="+SSG;
stt.executeUpdate(sqla);
stt.close();
con.close();
JOptionPane.showMessageDialog(null, "退房成功!", "哈哈",JOptionPane.INFORMATION_MESSAGE);
}
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
关于退房细节中,通过将登录界面中输入的用户名作为参数不断传入为每个人创作一张表
有关本系统中表的建立和相关sql语言实现细节见下一篇博客,
欢迎小伙伴们积极评论,本期将会随机从评论中的小伙伴抽
取一位幸运小伙伴送出本系统的全部源码