UI
package com.Test.view;
/*
在Motel168客房管理系统中增加客房预订表(RoomOrder)
字段包括客户姓名(customerName)、客户ID(customerID)、国家(customerCountry)、电话(customerTel)
邮政编码(customerZip)、入住时间(customerInTime)、入住天数(customerInDays)、房间号(RoomNo)和房间类型(RoomType)
“预订客房”界面(RoomOrderFrame)如下,使用JDBC实现客房预订功能,将客房预订信息插入RoomOrder表中。
*/
import javax.swing.*;
import com.Test.dao.UsernameDAO;
import com.Test.dto.UsernameDTO;
import java.awt.*;
import java.awt.event.*;
public class UsernameDisplay extends JFrame{
private JFrame jf;
private JPanel jp;
private JLabel name,id,country,tel,code,time,room,type;
private JTextField tf1,tf2,tf3,tf4,tf5,tf6;
private JComboBox roomlist,typelist;
private JButton searchbtn,okbtn,cancelbtn;
public UsernameDisplay(){
super("RoomOrder-预订客房");
Container c = this.getContentPane();
jp = new JPanel();
jp.setLayout(new GridLayout(1, 3));
this.setLayout(null);
name = new JLabel();
name.setBounds(new Rectangle(140, 50, 60, 35));
name.setText("客户姓名");
this.add(name);
tf1 = new JTextField(8);
tf1.setBounds(new Rectangle(220, 55, 250, 25));
this.add(tf1);
id = new JLabel("客户ID");
id.setBounds(new Rectangle(140,90, 60, 35));
this.add(id);
tf2 = new JTextField(8);
tf2.setBounds(new Rectangle(220, 95, 250, 25));
this.add(tf2);
country = new JLabel("国家");
country.setBounds(new Rectangle(140,130, 200, 30));
this.add(country);
tf3 = new JTextField(8);
tf3.setBounds(new Rectangle(220, 135, 250, 25));
this.add(tf3);
tel = new JLabel("电话");
tel.setBounds(new Rectangle(140, 170, 60, 35));
this.add(tel);
tf4 = new JTextField(8);
tf4.setBounds(new Rectangle(220, 175, 250, 25));
this.add(tf4);
code = new JLabel();
code.setText("邮政编码");
code.setBounds(new Rectangle(140, 215, 220, 25));
this.add(code);
tf5 = new JTextField(8);
tf5.setBounds(new Rectangle(220, 215, 250, 25));
this.add(tf5);
time = new JLabel("入住时间");
time.setBounds(new Rectangle(140, 255, 220, 25));
this.add(time);
tf6 = new JTextField(8);
tf6.setBounds(new Rectangle(220, 255, 250, 25));
this.add(tf6);
JLabel times = new JLabel("入住天数");
times.setBounds(new Rectangle(140, 297, 220, 25));
this.add(times);
JTextField tf61 = new JTextField(8);
tf61.setBounds(new Rectangle(220, 295, 250, 25));
this.add(tf61);
room = new JLabel("房间号");
room.setBounds(new Rectangle(140, 335, 220, 25));
this.add(room);
roomlist = new JComboBox();
roomlist.addItem("101");
roomlist.addItem("102");
roomlist.addItem("103");
roomlist.addItem("104");
roomlist.addItem("105");
roomlist.setBounds(new Rectangle(220,335, 250, 25));
this.add(roomlist);
type = new JLabel("房间类型");
type.setBounds(new Rectangle(140, 375, 220, 25));
this.add(type);
typelist = new JComboBox();
typelist.addItem("普通单人间");
typelist.addItem("普通双人间");
typelist.addItem("普通三人间");
typelist.addItem("总统套房");
typelist.setBounds(new Rectangle(220, 375, 250, 25));
this.add(typelist);
searchbtn = new JButton("查询房间");
searchbtn.setBounds(new Rectangle(140, 435, 90, 30));
this.add(searchbtn);
okbtn = new JButton("预订客房");
okbtn.setBounds(new Rectangle(260, 435, 90, 30));
this.add(okbtn);
cancelbtn = new JButton("重新输入");
cancelbtn.setBounds(new Rectangle(380, 435, 90, 30));
this.add(cancelbtn);
//action
// MyEventClass handler = new MyEventClass();
// okbtn.addActionListener(handler);
this.setSize(590,570);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
cancelbtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
tf61.setText("");
//roomlist.setSelectedItem("101");
//typelist.setSelectedItem("普通单人间");
roomlist.setSelectedIndex(0);
typelist.setSelectedIndex(0);
}
});
okbtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton tempButton=(JButton)e.getSource();
if(tempButton==okbtn)
{
if(tf1.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"姓名不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else if(tf2.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"ID不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else if(tf3.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"国家不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else if(tf4.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"电话不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else if(tf5.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"邮政编码不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else if(tf6.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"入住时间不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else if(tf61.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"入住天数不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
}
else
{
String u1 = tf1.getText().trim();
String u2 = tf2.getText().trim();
String u3 = tf3.getText().trim();
String u4 = tf4.getText().trim();
String u5 = tf5.getText().trim();
String u6 = tf6.getText().trim();
String u7 = tf61.getText().trim();
String u8 = roomlist.getSelectedItem().toString();
String u9 = typelist.getSelectedItem().toString();
UsernameDAO userdao = new UsernameDAO();
UsernameDTO userdto = new UsernameDTO();
userdto.setCustomerName(u1);
userdto.setCustomerID(u2);
userdto.setCustomerCountry(u3);
userdto.setCustomerTel(u4);
userdto.setCustomerZip(u5);
userdto.setCustomerInTime(u6);
userdto.setCustomerInDays(u7);
userdto.setInDaysRoomNo(u8);
userdto.setRoomType(u9);
boolean result = userdao.addUser(userdto);
if(result) {
JOptionPane.showMessageDialog(null, "预订成功!");
}
else {
JOptionPane.showMessageDialog(null, "预订失败!");
}
}
}
}
});
}
public static void main(String[] args) {
new UsernameDisplay();
}
}
DAO
/**
* 千枝竞秀有天皆丽日,万木争荣无地不春风
* Author: RoniZeng
* Time:17/12/25
*/
package com.Test.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.Test.Util.DBUtil;
import com.Test.dto.UsernameDTO;
public class UsernameDAO {
public boolean addUser(UsernameDTO user) {
Connection conn = null;
PreparedStatement statement = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into RoomOrder values(?,?,?,?,?,?,?,?,?)";
statement = conn.prepareStatement(sql);
statement.setString(1, user.getCustomerName());
statement.setString(2, user.getCustomerID());
statement.setString(3, user.getCustomerCountry());
statement.setString(4, user.getCustomerTel());
statement.setString(5, user.getCustomerZip());
statement.setString(6, user.getCustomerInTime());
statement.setString(7, user.getCustomerInDays());
statement.setString(8, user.getInDaysRoomNo());
statement.setString(9, user.getRoomType());
int result = statement.executeUpdate();
if(result>0) {
return true;
}
else {
return false;
}
}
catch(Exception e) {
e.printStackTrace();
return false;
}
finally {
try {
statement.close();
conn.close();
}
catch(Exception e1) {
e1.printStackTrace();
}
}
}
}
DTO
package com.Test.dto;
public class UsernameDTO {
private String customerName, customerID, customerCountry;
private String customerTel, customerZip, customerInTime;
private String customerInDays, InDaysRoomNo, RoomType;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerID() {
return customerID;
}
public void setCustomerID(String customerID) {
this.customerID = customerID;
}
public String getCustomerCountry() {
return customerCountry;
}
public void setCustomerCountry(String customerCountry) {
this.customerCountry = customerCountry;
}
public String getCustomerTel() {
return customerTel;
}
public void setCustomerTel(String customerTel) {
this.customerTel = customerTel;
}
public String getCustomerZip() {
return customerZip;
}
public void setCustomerZip(String customerZip) {
this.customerZip = customerZip;
}
public String getCustomerInTime() {
return customerInTime;
}
public void setCustomerInTime(String customerInTime) {
this.customerInTime = customerInTime;
}
public String getCustomerInDays() {
return customerInDays;
}
public void setCustomerInDays(String customerInDays) {
this.customerInDays = customerInDays;
}
public String getInDaysRoomNo() {
return InDaysRoomNo;
}
public void setInDaysRoomNo(String inDaysRoomNo) {
InDaysRoomNo = inDaysRoomNo;
}
public String getRoomType() {
return RoomType;
}
public void setRoomType(String roomType) {
RoomType = roomType;
}
}
DBUtil
package com.Test.Util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
public static Connection getConnection(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HappyDB;user=sa;password=123456");
return conn;
}
catch (Exception e) {
}
return null;
}
}
转载自原文链接, 如需删除请联系管理员。
原文链接:JDBC:预订房间,转载请注明来源!