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

📄 objtab.java

📁 java数据库编程。JDBC+SQL+GUI。用java写的一个影碟租赁系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					+ " values ('" 
					+ txtCID.getText() 
					+ "', '" 
					+ txtVID.getText() 
					+ "', '" 
					+ txtDue.getText() 
					+ "')";
					 System.out.println(insert);
					 s.execute(insert);
					 txtCID.setText(""); txtVID.setText("");
					 txtDue.setText(""); 
					 lblStatus.setText("Rent saved successfully");
					
				}
			}
			}catch (SQLException e)
			{
				throw new SQLException("Error inserting record to " + strTable + e.getMessage() );
			}
			catch (Exception e)
			{
				throw new SQLException("Error inserting record to " + strTable + e.getMessage() );
			}
		}
		s.close();
	}
	
	public void updateRecord() throws SQLException
	{
		
		Statement s = databaseConnection.createStatement();
		String update;
		try
		{
			if (strTable.equalsIgnoreCase("Customer"))
			{
				if(validCustomer())
				{
					
					update = "UPDATE Customer SET FirstName ='" 
						+ txtFN.getText() 
						+ "',LastName ='" 
						+ txtLN.getText() 
						+ "', Address ='" 
						+ txtAdd.getText() 
						+ "', PhNo ='" 
						+ txtPh.getText() 
						+ "' WHERE CID ="
						+ Integer.parseInt(txtCID.getText()); 
					//test point, can be removed
					System.out.println(update);
					s.executeUpdate(update);
					txtFN.setText(""); txtLN.setText(""); txtAdd.setText("");
					txtPh.setText(""); txtFN.requestFocus();
					lblStatus.setText("Customer Updated successfully");
					
				}
				
			}
			else if (strTable.equalsIgnoreCase("Vedio"))
			{
				// code to insert vedio record
				if(validVideo())
				{
					update = "UPDATE Vedio SET Title ='" 
					+ txtTitle.getText() 
					+ "',Year ='" 
					+ txtYear.getText() 
					+ "',Genre ='" 
					+ txtGnre.getText() 
					+ "',Producer ='" 
					+ txtProducer.getText() 
					+ "' WHERE VID ="
					+ Integer.parseInt(txtVID.getText()); 
					System.out.println(update);
					s.executeUpdate(update);
					txtTitle.setText(""); txtYear.setText("");
					txtGnre.setText(""); txtProducer.setText("");
					lblStatus.setText("Video Updated successfully");
				}
			}
			else if (strTable.equalsIgnoreCase("Rental"))
			{
				// check for the validity of CID and VID
				// code to insert Rental record
				if (validRent())
				{
					update = "UPDATE Rental SET CID ='"
					+ txtCID.getText() 
					+ "',VID ='" 
					+ txtVID.getText() 
					+ "',DueDate ='" 
					+ txtDue.getText() 
					+ "' WHERE RentNo ="
					+ Integer.parseInt(txtRID.getText()); 
					System.out.println(update);
					s.executeUpdate(update);
					txtCID.setText(""); txtVID.setText("");
					txtDue.setText(""); 
					lblStatus.setText("Rent saved successfully");
				}
			}
		}catch (SQLException e)
		{
			throw new SQLException(e.getMessage() );
		}
		catch (Exception e)
		{
			throw new SQLException(e.getMessage() );
		}
		s.close();
	}
		

 	public void deleteRecord() throws SQLException
	{
		
		Statement s = databaseConnection.createStatement();
		String delete;
		try
		{
			if (strTable.equalsIgnoreCase("Customer"))
			{
					delete = "DELETE FROM Customer where CID=" + Integer.parseInt(txtCID.getText()); 
					s.executeUpdate(delete);
					txtFN.setText(""); txtLN.setText(""); txtAdd.setText("");
					txtPh.setText(""); txtFN.requestFocus();
					lblStatus.setText("Customer Deleted successfully");
			}
			else if (strTable.equalsIgnoreCase("Vedio"))
			{
					delete = "DELETE FROM Vedio where VID=" + Integer.parseInt(txtVID.getText()); 
					s.executeUpdate(delete);
					txtTitle.setText(""); txtYear.setText("");
					txtGnre.setText(""); txtProducer.setText("");
					lblStatus.setText("Video Deleted successfully");
			}
			else if (strTable.equalsIgnoreCase("Rental"))
			{
					delete = "DELETE FROM Rental WHERE RentNo ="+ Integer.parseInt(txtRID.getText()); 
					s.executeUpdate(delete);
					txtCID.setText(""); txtVID.setText("");
					txtDue.setText(""); 
					lblStatus.setText("Rent Deleted successfully");
			}
		}catch (SQLException e)
		{
			throw new SQLException(e.getMessage());
		}
		catch (Exception e)
		{
			throw new SQLException(e.getMessage());
		}
		s.close();
	}
		
	
	
	
	public boolean validCustomer() throws Exception
	{
		boolean valid= true;
		if (txtFN.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please check the First Name ");
		}
		if (txtLN.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please check the Last Name ");
		}
		if (txtAdd.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please check the Address ");
		}
		try
		{
			Integer.parseInt(txtPh.getText());
		}catch(Exception e){
			throw new NumberFormatException ("Invalid phone Number");
		}
		
		return valid;
	}
	
	public void fillEdit() throws SQLException
	{
		Statement s = databaseConnection.createStatement();
		ResultSet result;
		
		try
		{
			if (strTable.equalsIgnoreCase("Customer"))
			{
				//Construct the input fields and populate them with current value
				result = s.executeQuery("SELECT * FROM Customer WHERE CID =" + Integer.parseInt(txtCID.getText()));
				if (result.next())
				{
					cmdSearch.setActionCommand("Reset");
					cmdSearch.setText("Reset");
					txtCID.setEditable(false);
					inputCustomer();
					txtFN.setText(result.getString(2));
					txtLN.setText(result.getString(3));
					txtAdd.setText(result.getString(4));
					txtPh.setText(result.getString(5));
					lblHint.setText("Do the required changes and click Update" );
				}
				else
					lblHint.setText("No record found for CID: "+txtCID.getText()+ ". Try again" );
			}
			if (strTable.equalsIgnoreCase("Vedio"))
			{
				//Construct the input fields and populate them with current value
				result = s.executeQuery("SELECT * FROM Vedio WHERE VID =" + Integer.parseInt(txtVID.getText()));
				if (result.next())
				{
					cmdSearch.setActionCommand("Reset");
					cmdSearch.setText("Reset");
					txtVID.setEditable(false);
					inputVedio();
					txtTitle.setText(result.getString(2));
					txtYear.setText(result.getString(3));
					txtGnre.setText(result.getString(4));
					txtProducer.setText(result.getString(5));
					lblHint.setText("Do the required changes and click Update" );
				}
				else
					lblHint.setText("No record found for VID: "+txtVID.getText()+ ". Try again" );
			}
			if (strTable.equalsIgnoreCase("Rental"))
			{
				//Construct the input fields and populate them with current value
				result = s.executeQuery("SELECT * FROM Rental WHERE RentNo =" + Integer.parseInt(txtRID.getText()));
				if (result.next())
				{
					cmdSearch.setActionCommand("Reset");
					cmdSearch.setText("Reset");
					txtRID.setEditable(false);
					inputRental();
					txtCID.setText(result.getString(2));
					txtVID.setText(result.getString(3));
					txtDue.setText(result.getString(4));
					lblHint.setText("Do the required changes and click Update" );
				}
				else
					lblHint.setText("No record found with Rent No: "+txtRID.getText()+ ". Try again" );
			}
						
		}catch (SQLException ex)
		{
			throw new SQLException("No Record found: " + ex.getMessage());
		}
		s.close();
		
	}

	public void clearEdit()
	{
		
		cmdSearch.setActionCommand("Search");
		cmdSearch.setText("Search");
		lblHint.setText("Do the required changes and click Update" );
			if (strTable.equalsIgnoreCase("Customer"))
			{
					txtCID.setEditable(true);
					txtFN.setText("");
					txtLN.setText("");
					txtAdd.setText("");
					txtPh.setText("");
					this.remove(panFN);
					this.remove(panLN);
					this.remove(panAdd);
					this.remove(panPh);
					this.remove(panStatus); 
			}
			else if (strTable.equalsIgnoreCase("Vedio"))
			{
					txtVID.setEditable(true);
					txtTitle.setText("");
					txtYear.setText("");
					txtGnre.setText("");
					txtProducer.setText("");
					this.remove(panTitle);
					this.remove(panYear);
					this.remove(panGnre);
					this.remove(panProducer);
					this.remove(panStatus); 
			}
			else if (strTable.equalsIgnoreCase("Rental"))
			{
					txtRID.setEditable(true);
					txtCID.setText("");
					txtVID.setText("");
					txtDue.setText("");
					this.remove(panCID);
					this.remove(panVID);
					this.remove(panDue);
					this.remove(panStatus); 
			}
						
	}
	
	
	public boolean validVideo() throws Exception
	{
		boolean valid= true;
		
		if (txtTitle.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please Enter the Title ");
		}
		if (txtProducer.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please Enter the Producer ");
		}
		if (txtGnre.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please Enter the Address ");
		}
		try
		{
			int temp = getYear();
		}catch(NumberFormatException ex){
			valid = false;
			lblStatus.setText(ex.getMessage());
		}
		return valid;
	}
	
	public boolean validRent() throws Exception
	{
		boolean valid= true;
		Statement s=databaseConnection.createStatement();
		Statement s1=databaseConnection.createStatement();
		ResultSet cus,ved;
		if (txtCID.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please Enter the CustomerID ");
		}
		if (txtVID.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please Enter the Video ID");
		}
		if (txtDue.getText().equals(""))
		{
			valid = false;
			throw new Exception ("Error- Please Enter the Due Date");
		}
		try
		{
		cus= s.executeQuery("Select * from Customer where CID =" + Integer.parseInt(txtCID.getText()));
		System.out.println("Test");
		if (!cus.next())
		{
			valid = false;
			throw new SQLException ("CID Does not Exist.");
		}
		s.close();
		ved= s1.executeQuery("SELECT * FROM Vedio WHERE VID =" +Integer.parseInt(txtVID.getText()));
		if (!ved.next())
		{
			valid = false;
			throw new SQLException ("VID Does not Exist.");
		}
		}catch(SQLException ex)
		{
			throw new SQLException (" Invalid Input ");
		}
			
		return valid;
	}

	
	public void actionPerformed (ActionEvent e)
	{
		
		lblStatus.setText("");
		System.out.println("Action event save triggered: "+ e.getActionCommand());
		if (e.getActionCommand().equalsIgnoreCase("Save"))
		{
			try
			{
				saveRecord();
			}
			catch (SQLException ex)
			{
				lblStatus.setText("Error writing Record to " + strTable + " :" + ex.getMessage());
			}
		}
		else if (e.getActionCommand().equalsIgnoreCase("Update"))
		{
			try
			{
				updateRecord();
			}
			catch (SQLException ex)
			{
				lblStatus.setText("Error Updating:" + ex.getMessage());
			}
			
		}
		else if (e.getActionCommand().equalsIgnoreCase("Delete"))
		{
			try
			{
				deleteRecord();
			}
			catch (SQLException ex)
			{
				lblStatus.setText("Error Deleting" + ex.getMessage());
			}
			
		}
		else if (e.getActionCommand().equalsIgnoreCase("Search"))
		{
			try
			{
				fillEdit();
			}catch (SQLException ex)
			{
				lblHint.setText(ex.getMessage());
			}
			
		}
		else if (e.getActionCommand().equalsIgnoreCase("Reset"))
		{
				clearEdit();
			
		}
	}
		
	}				
	

⌨️ 快捷键说明

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