⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hotel.java

📁 一个典型的数据库例子(酒店房间出租系统)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				if (except==false){
					if(roomid.equals("")||cname.equals("")||cssn.equals("")||stringreservetime.equals("")){
						JOptionPane.showMessageDialog(null, "You didn't enter the required values");
						
					}
					else{
						squery="SELECT ROOM_STATUS FROM rooms WHERE ROOM_ID LIKE "+"\'"+roomid+"\'";
						try {
							con=DriverManager.getConnection(url);
							stmt=con.createStatement();
							rs=stmt.executeQuery(squery);
							while (rs.next()){
								roomstatus=rs.getString("ROOM_STATUS");
							}
						}catch(SQLException ex) {
							System.err.println("SQLException: "+ex.getMessage());
						}
						
						if (roomstatus.equals("occupied")||roomstatus.equals("reserved")){
							JOptionPane.showMessageDialog(null, "This room is not available today");
						}
						else{
							Object[] options={"Yes","Cancel"};
							JFrame frame=new JFrame();
							int days=(int)Double.parseDouble(stringreservetime);
							calendar.add(Calendar.DAY_OF_MONTH,days);
							int nextYear=calendar.get(Calendar.YEAR);
							int nextMonth=calendar.get(Calendar.MONTH)+1;
							int nextday=calendar.get(Calendar.DAY_OF_MONTH);
							
							int durationdays=(int)Double.parseDouble(stringdays);
							calendar.add(Calendar.DAY_OF_MONTH,durationdays);
							int returnYear=calendar.get(Calendar.YEAR);
							int returnMonth=calendar.get(Calendar.MONTH)+1;
							int returnday=calendar.get(Calendar.DAY_OF_MONTH);
							calendar=new GregorianCalendar();
							
							
								
								
								
								
							int n=JOptionPane.showOptionDialog(frame,"Are you sure want to reserve?\n","Choose",
				    	     		   	                 JOptionPane.OK_CANCEL_OPTION,
				        			                     JOptionPane.QUESTION_MESSAGE,
				        	   		 	                 null,options,options[0]);
				        	if(n==JOptionPane.OK_OPTION){
					        	String update1="update rooms set ROOM_STATUS ="+"\'"+"reserved"+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
								String update2="update rooms set CUSTOMER_NAME ="+"\'"+cname+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
								String update3="update rooms set DURATION = "+"\'"+stringdays+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
								String update4="update rooms set CUSTOMER_SSN ="+"\'"+cssn+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
								String update5="update rooms set RESERVE_TIME ="+"\'"+nextday+"/"+nextMonth+"/"+nextYear+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
								String update6="update rooms set RETURN_TIME ="+"\'"+returnday+"/"+returnMonth+"/"+returnYear+"\'"+" where ROOM_ID like " +"\'"+roomid+"\'";
								
								try {
 										Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								} catch(java.lang.ClassNotFoundException e){
									System.err.print("ClassNotFoundException:");
									System.err.println(e.getMessage());
								}
								try {
									con=DriverManager.getConnection(url);
									stmt=con.createStatement();
									stmt.executeUpdate(update1);
									stmt.executeUpdate(update2);
									stmt.executeUpdate(update3);
									stmt.executeUpdate(update4);
									stmt.executeUpdate(update5);
									stmt.executeUpdate(update6);
									stmt.close();
									con.close();
									
								}catch(SQLException ex) {
									System.err.println("SQLException: "+ex.getMessage());
								}
								jp6.setVisible(false);
								jp6.remove(jsp);
								Vector dataVector = new Vector();
								Vector headVector=new Vector(4);
								String squery = "select * from rooms";
								try {
									Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
								} catch(java.lang.ClassNotFoundException e){
									System.err.print("ClassNotFoundException: ");
									System.err.println(e.getMessage());
								}
		
								try {
									con=DriverManager.getConnection(url);
									stmt=con.createStatement();
									rs=stmt.executeQuery(squery);
	
									while(rs.next()) {


										String i = rs.getString("ROOM_ID");
										String t = rs.getString("ROOM_TYPE");
										String s = rs.getString("ROOM_STATUS");
										String c = rs.getString("CUSTOMER_NAME");

										Vector rowVector = new Vector();
										rowVector.addElement(i);
										rowVector.addElement(t);
										rowVector.addElement(s);
										rowVector.addElement(c);


										dataVector.addElement(rowVector);
									}
									headVector.addElement("ROOM_ID");
									headVector.addElement("ROOM_TYPE");
									headVector.addElement("ROOM_STATUS");
									headVector.addElement("CUSTOMER_NAME");

									stmt.close();
									con.close();
								} catch(SQLException ex) {
									System.err.println("SQLException: "+ex.getMessage());
								}
								JTable table = new JTable(dataVector, headVector);
								int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
								int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
								jsp = new JScrollPane(table, v, h);
						
								jp6.add(jsp);
								jp6.setVisible(true);
												
												
								
							}   		 	                 
						}
					}
				}
				
				
			}
		});
			
			
			
			
		jbcancel=new JButton("Cancel");
		jbcancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				System.exit(0);
			}
		});
		
		
			
		jp5.add(jbmanageroom);
		jp5.add(jbmanagecustomer);
		jp5.add(jbconfirm);
		jp5.add(jbreserve);
		jp5.add(jbcancel);
		
		jp55=new JPanel();
		jp55.setLayout(new GridLayout(1,3));
		jborderr=new JButton("Order by ROOM_ID");
		jborderr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				jp6.setVisible(false);
				jp6.remove(jsp);
				Vector dataVector = new Vector();
				Vector headVector=new Vector(4);
				String squery = "select * from rooms order by ROOM_ID";
				try {
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				} catch(java.lang.ClassNotFoundException e){
					System.err.print("ClassNotFoundException: ");
					System.err.println(e.getMessage());
				}
			
				try {
					con=DriverManager.getConnection(url);
					stmt=con.createStatement();

					rs=stmt.executeQuery(squery);

					while(rs.next()) {


						String i = rs.getString("ROOM_ID");
						String t = rs.getString("ROOM_TYPE");
						String s = rs.getString("ROOM_STATUS");
						String c = rs.getString("CUSTOMER_NAME");


						Vector rowVector = new Vector();
						rowVector.addElement(i);
						rowVector.addElement(t);
						rowVector.addElement(s);
						rowVector.addElement(c);


						dataVector.addElement(rowVector);
					}

					headVector.addElement("ROOM_ID");
 					headVector.addElement("ROOM_TYPE");
					headVector.addElement("ROOM_STATUS");
					headVector.addElement("CUSTOMER_NAME");


					stmt.close();
					con.close();
				} catch(SQLException ex) {
					System.err.println("SQLException: "+ex.getMessage());
				}
				JTable table = new JTable(dataVector, headVector);
				int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
				int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
				jsp = new JScrollPane(table, v, h);
			
				jp6.add(jsp);
				jp6.setVisible(true);
			}
		});
			
			
		jborderc=new JButton("Order by CUSTOMER_NAME");
		jborderc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				jp6.setVisible(false);
				jp6.remove(jsp);
				Vector dataVector = new Vector();
				Vector headVector=new Vector(4);
				String squery = "select * from rooms order by CUSTOMER_NAME";
				try {
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				} catch(java.lang.ClassNotFoundException e){
					System.err.print("ClassNotFoundException: ");
					System.err.println(e.getMessage());
				}
		
				try {
					con=DriverManager.getConnection(url);
					stmt=con.createStatement();
					rs=stmt.executeQuery(squery);

					while(rs.next()) {

						String i = rs.getString("ROOM_ID");
						String t = rs.getString("ROOM_TYPE");
						String s = rs.getString("ROOM_STATUS");
						String c = rs.getString("CUSTOMER_NAME");

						Vector rowVector = new Vector();
						rowVector.addElement(i);
						rowVector.addElement(t);
						rowVector.addElement(s);
						rowVector.addElement(c);

						dataVector.addElement(rowVector);
					}
					headVector.addElement("ROOM_ID");
					headVector.addElement("ROOM_TYPE");
					headVector.addElement("ROOM_STATUS");
					headVector.addElement("CUSTOMER_NAME");


					stmt.close();
					con.close();
				} catch(SQLException ex) {
					System.err.println("SQLException: "+ex.getMessage());
				}
				JTable table = new JTable(dataVector, headVector);
				int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
				int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
				jsp = new JScrollPane(table, v, h);
		
				jp6.add(jsp);
				jp6.setVisible(true);
			}
		});
		
		jbshowall=new JButton("Show All information");
		jbshowall.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				showall application = new showall();	
			}
		});
			
			
		jp55.add(jborderr);
		jp55.add(jborderc);
		jp55.add(jbshowall);
		
			
		jp6=new JPanel();
		jp6.setBorder(BorderFactory.createCompoundBorder(
		              BorderFactory.createTitledBorder("Roomlist"),
		              BorderFactory.createEmptyBorder(5,5,5,5)));
		jp6.setLayout(new BoxLayout(jp6,BoxLayout.Y_AXIS));
		final String[] colHeads = {"ROOM_ID", "ROOM_TYPE", "ROOM_STATUS","CUSTOMER_NAME"};
		Vector dataVector = new Vector();
		Vector headVector=new Vector(4);
		String squery = "select * from rooms order by ROOM_ID";
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch(java.lang.ClassNotFoundException e){
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}
		
		try {
			con=DriverManager.getConnection(url);
			stmt=con.createStatement();
			rs=stmt.executeQuery(squery);

			while(rs.next()) {

				String i = rs.getString("ROOM_ID");
				String t = rs.getString("ROOM_TYPE");
				String s = rs.getString("ROOM_STATUS");
				String c = rs.getString("CUSTOMER_NAME");

				Vector rowVector = new Vector();
				rowVector.addElement(i);
				rowVector.addElement(t);
				rowVector.addElement(s);
				rowVector.addElement(c);

				dataVector.addElement(rowVector);
			}

			headVector.addElement("ROOM_ID");
			headVector.addElement("ROOM_TYPE");
			headVector.addElement("ROOM_STATUS");
			headVector.addElement("CUSTOMER_NAME");


			stmt.close();
			con.close();
		} catch(SQLException ex) {
			System.err.println("SQLException: "+ex.getMessage());
		}
		JTable table = new JTable(dataVector, headVector);
		int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
		int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		jsp = new JScrollPane(table, v, h);
		jp6.add(jp55);
		jp6.add(jsp);
		
			
			
			
			
		Container contentPane = getContentPane();
		contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
		contentPane.add(jp1);
		contentPane.add(jp4);
		contentPane.add(jp5);
		contentPane.add(jp6);
		setSize(700,550);
		setVisible(true);
		
			
	}
}
	
class rmanage extends JFrame{
	String url="jdbc:odbc:Registry";
	Connection con;
	Statement stmt;
	ResultSet rs;
	    
    JLabel jlno,jltype,jlstatus;
    JButton jbadd,jbdel,jbmod,jbreturn;
    JTextField jtfno;
    JComboBox jcbtype,jcbstatus;
    
    public rmanage() {
	    super("Manage the room information.");
	    
	    Container contentPane = getContentPane();
	    contentPane.setLayout(new GridLayout(5,2, 5, 10));
	    
	    jlno=new JLabel("Room No.:");
	    jtfno = new JTextField(15);
	    jtfno.setBorder(BorderFactory.createLineBorder(Color.blue));
	    jltype=new JLabel("Room Type:");
	    
	    jcbtype = new JComboBox();
		jcbtype.addItem("single");
		jcbtype.addItem("double");
		jcbtype.addItem("luxury");
		
		jlstatus=new JLabel("Room Status:");
		jcbstatus = new JComboBox();
		jcbstatus.addItem("empty");
		jcbstatus.addItem("reserve");
		jcbstatus.addItem("occupied");
		
		jbadd = new JButton("Add");
		jbadd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				try {
					String Price=null;
	   				con=DriverManager.getConnection(url);
	   				stmt=con.createStatement();
	   				if (jcbtype.getSelectedItem().toString().equals("single")){
		   				Price="100";
	   				}
	   				if (jcbtype.getSelectedItem().toString().equals("double")){
		   				Price="150";
	   				}
	   				if (jcbtype.getSelectedItem().toString().equals("luxury")){
		   				Price="250";
	   				}
	   	
	   				stmt.executeUpdate("insert into rooms values ('"+jtfno.getText()+"','"+jcbtype.getSelectedItem() +"','empty','"+Price+"',null,null,null,null,null)"); 
 			
					stmt.close();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -