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

📄 logindialog.java

📁 将DB2数据库里的数据导出为excel文件的java代码
💻 JAVA
字号:
/*********************************************************************/
/*(c) Copyright IBM Corp. 2004  All rights reserved.                 */
/*                                                                   */
/*This sample program is owned by International Business Machines    */
/*Corporation or one of its subsidiaries ("IBM") and is copyrighted  */
/*and licensed, not sold.                                            */
/*                                                                   */
/*You may copy, modify, and distribute this sample program in any    */
/*form without payment to IBM,  for any purpose including developing,*/
/*using, marketing or distributing programs that include or are      */
/*derivative works of the sample program.                            */
/*                                                                   */
/*The sample program is provided to you on an "AS IS" basis, without */
/*warranty of any kind.  IBM HEREBY  EXPRESSLY DISCLAIMS ALL         */
/*WARRANTIES EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO*/
/*THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTIC-*/
/*ULAR PURPOSE. Some jurisdictions do not allow for the exclusion or */
/*limitation of implied warranties, so the above limitations or      */
/*exclusions may not apply to you.  IBM shall not be liable for any  */
/*damages you suffer as a result of using, modifying or distributing */
/*the sample program or its derivatives.                             */
/*                                                                   */
/*Each copy of any portion of this sample program or any derivative  */
/*work,  must include a the above copyright notice and disclaimer of */
/*warranty.                                                          */
/*                                                                   */
/*********************************************************************/

package com.ibm.ExcelDB2;

/*********************************************************************/
/* Authenticates a user with a database                              */
/*********************************************************************/
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;

/**
* This LoginDialog class asks for a username, password, the database name and its port number. This info is used
* to connect to the database.  The only required field is the database name. If no value is given for the port, we
* try connecting using the default port number. If no username is given, we try connecting using the default
* id/password.
*/
public class LoginDialog extends JDialog {
	
	private JPanel panel = new JPanel();
	private JPanel componentsPanel = new JPanel();
	private JPanel usernamePanel = new JPanel();
	private FlowLayout flowLayout1 = new FlowLayout();
	private JLabel logoLabel = new JLabel();
	private JPanel logoPanel = new JPanel();
	private JPanel statusPanel = new JPanel();
	private JLabel statusText = new JLabel("Ready.");
	private JPanel passwordPanel = new JPanel();
	private JTextField usernameText = new JTextField();
	private JPasswordField passwordText = new JPasswordField();
	private JLabel passwordLabel = new JLabel("Password:");
	private JLabel usernameLabel = new JLabel("Username:");
	private JPanel databasePanel = new JPanel();
	private JTextField databaseText = new JTextField();
	private JLabel databaseLabel = new JLabel("Database:");
	private JPanel portPanel = new JPanel();
	private JTextField portText = new JTextField();
	private JPanel buttonPanel = new JPanel();
	private JButton loginButton = new JButton("Login");
	private JButton cancelButton = new JButton("Cancel");
	private boolean cancelled = false;
	private boolean success = false;
	private LoginService loginService = null;
	
	/**
	* @param frame from which the dialog is displayed
	* @param title Title of the dialog box
	* @param service interface used to connect to database
	*/
	public LoginDialog(Frame frame, String title, LoginService service) {
		super(frame, title, true);
		loginService = service;
		try {
			jbInit();
			pack();
			centerDialog();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	* Method setUsername.
	* Sets 
	* @param username
	*/
	public void setUsername(String username) {
		usernameText.setText(username);
	}
	
	/**
	* Method setPassword.
	* @param password
	*/
	public void setPassword(String password) {
		this.passwordText.setText(password);
	}
	
	/**
	* Method getUsername.
	* @return String
	*/
	public String getUsername() {
		return usernameText.getText();
	}
	
	/**
	* Method getPassword.
	* @return String
	*/
	public String getPassword() {
		return String.valueOf(passwordText.getPassword());
	}
	
	/**
	* Method getDatabase.
	* @return String
	*/
	public String getDatabase() {
		return databaseText.getText();
	}
	
	/**
	* Method getPort.
	* @return String
	*/
	public String getPort() {
		return portText.getText();
	}
	
	/**
	* Method isCancelled.
	* @return boolean
	*/
	public boolean isCancelled() {
		return cancelled;
	}
	
	/**
	* Method setStatus.
	* @param message
	*/
	public void setStatus(String message) {
		this.statusText.setText(message);
		this.repaint();
	}
	
	/**
	* Method jbInit.
	* Initialize the GUI
	* @throws Exception
	*/
	private void jbInit() throws Exception {
		// Set the dialog's characteristics.
		panel.setLayout(flowLayout1);
		if (getTitle() == null || getTitle().equals("")) {
		    this.setTitle("Select DataBase");
		}
		panel.setPreferredSize(new Dimension(350, 200));
		panel.setLayout(new BorderLayout());
		// Create the dialog components
		usernamePanel.setPreferredSize(new Dimension(300, 30));
		usernameLabel.setPreferredSize(new Dimension(75, 17));
		usernameText.setPreferredSize(new Dimension(200, 21));
		passwordPanel.setPreferredSize(new Dimension(300, 30));
		passwordLabel.setPreferredSize(new Dimension(75, 17));
		passwordText.setPreferredSize(new Dimension(200, 21));
		databasePanel.setPreferredSize(new Dimension(300, 30));
		databaseText.setPreferredSize(new Dimension(200, 21));
		databaseLabel.setPreferredSize(new Dimension(75, 17));
		buttonPanel.setPreferredSize(new Dimension(300, 35));
		logoPanel.setPreferredSize(new Dimension(300, 75));
		logoPanel.setBorder(new EmptyBorder(5, 5, 25, 5));
		///////logoPanel.add(logoLabel);
		statusPanel.setPreferredSize(new Dimension(300, 21));
		statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		statusPanel.setLayout(new BorderLayout());
		statusText.setHorizontalAlignment(SwingConstants.LEFT);
		statusPanel.add(statusText, BorderLayout.WEST);
		getContentPane().add(panel);
		///////componentsPanel.add(logoPanel);
		// Add components to the login dialog
		componentsPanel.add(usernamePanel, null);
		usernamePanel.add(usernameLabel, null);
		usernamePanel.add(usernameText, null);
		componentsPanel.add(passwordPanel, null);
		passwordPanel.add(passwordLabel, null);
		passwordPanel.add(passwordText, null);
		databasePanel.add(databaseLabel, null);
		databasePanel.add(databaseText, null);
		portPanel.add(portText, null);
		componentsPanel.add(databasePanel, null);
		componentsPanel.add(buttonPanel, null);
		buttonPanel.add(cancelButton, null);
		buttonPanel.add(loginButton, null);
		panel.add(componentsPanel, BorderLayout.CENTER);
		panel.add(statusPanel, BorderLayout.SOUTH);
		// Check for the user pressing the login button
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoginDialog.this.login();
			}
		});
		// Check for the user pressing the cancel button
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoginDialog.this.cancel();
			}
		});
		// Use the enter key as a synonym for the login button
		this.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
					LoginDialog.this.login();
				}
			}
		});
		// Check for the user closing the dialog explicitly
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				LoginDialog.this.cancel();
			}
		});
	}

	/**
	* Centers the dialog box to appear in the middle of the screen
	*/
	private void centerDialog() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = ((int) screenSize.getWidth() - (int) (screenSize.getWidth() / 2)) - (int) (getWidth() / 2);
		int y = ((int) screenSize.getHeight() - (int) (screenSize.getHeight() / 2)) - (int) (getHeight() / 2);
		setLocation(x, y);
	}

	/**
	 * Method cancel.
	 */
	private void cancel() {
		this.cancelled = true;
		setVisible(false);
	}

	/**
	* Method login.
	* Connect to the database given the user id,password,database name and port #. This is done using the LoginService.
	*/
	private void login() {
        if (!this.loginButton.isEnabled())
            return;
        setStatus("Logging into Database...");
        success = false;
		boolean success = this.loginService.login(LoginDialog.this.getUsername(), LoginDialog.this.getPassword(), LoginDialog.this.getDatabase(), LoginDialog.this.getPort());
		if (success == false) {
			setStatus("Login failure... Try Again");
		} else {
			setVisible(false);
			this.loginButton.setEnabled(false);
		}
	}
}

⌨️ 快捷键说明

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