用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java使用JDBC连接SQL数据库,适合于初学者学习(一款小型的图书信息管理)

2015-10-06 作者: YunmR举报

[java]代码库

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 591378743488239713L;
	public static String DB_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public static String DB_URL = "jdbc:sqlserver://localhost:1433; DatabaseName=BookManager";

	JPanel contentPane;
	Statement ps;
	ResultSet rs;
	Connection con;
	JMenuBar jMenuBar = new JMenuBar();
	JMenu Menu_UserManager = new JMenu();
	JMenu jMenuHelp = new JMenu();
	JMenuItem jMenuHelpAbout = new JMenuItem();
	JMenuItem adduser = new JMenuItem();
	JMenuItem deleteuser = new JMenuItem();
	JMenu reader_management = new JMenu(); // 读者管理
	JMenuItem queryReader = new JMenuItem();
	JMenuItem change_readerInfo = new JMenuItem();
	JMenuItem add_readerInfo = new JMenuItem();
	JMenu borrowAndreturn = new JMenu(); // 借阅管理
	JMenuItem returnBook = new JMenuItem();
	JMenuItem borrowBook = new JMenuItem();
	JMenu book_management = new JMenu(); // 图书管理
	JMenuItem AddBook = new JMenuItem();
	JMenuItem ChangeBookInfo = new JMenuItem();
	JMenuItem queryBookInfo = new JMenuItem();
	JMenuItem exit = new JMenuItem();
	JMenuItem changePasswd = new JMenuItem();
	JMenuItem scanningUser = new JMenuItem();
	JMenuItem login = new JMenuItem();

	public MainFrame() {
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		try {
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Component initialization
	private void jbInit() throws Exception {

		this.setResizable(false);
		this.setTitle("图书信息管理系统");
		contentPane = new MyPanelk();
		contentPane.setOpaque(false);
		contentPane.add(jMenuBar);
		this.add(contentPane);

		Menu_UserManager.setFont(new java.awt.Font("Dialog", 0, 15));
		Menu_UserManager.setForeground(Color.black);
		Menu_UserManager.setText("用户管理");

		adduser.setFont(new java.awt.Font("Dialog", 0, 15));
		adduser.setText("添加用户");
		adduser.addActionListener(new mainFrame_adduser_actionAdapter(this));

		deleteuser.setFont(new java.awt.Font("Dialog", 0, 15));
		deleteuser.setText("删除用户");
		deleteuser.addActionListener(new mainFrame_deleteuser_actionAdapter(this));

		exit.setFont(new java.awt.Font("Dialog", 0, 15));
		exit.setText("退出系统");
		exit.addActionListener(new mainFrame_exit_actionAdapter(this));

		changePasswd.setFont(new java.awt.Font("Dialog", 0, 15));
		changePasswd.setText("修改密码");
		changePasswd.addActionListener(new mainFrame_changePasswd_actionAdapter(this));

		scanningUser.setFont(new java.awt.Font("Dialog", 0, 15));
		scanningUser.setText("浏览用户");
		scanningUser.addActionListener(new mainFrame_scanningUser_actionAdapter(this));

		login.setFont(new java.awt.Font("Dialog", 0, 15));
		login.setText("用户登录");
		login.addActionListener(new mainFrame_login_actionAdapter(this));

		jMenuHelp.setFont(new java.awt.Font("Dialog", 0, 15));
		jMenuHelp.setText("帮助信息");
		jMenuHelpAbout.setFont(new java.awt.Font("Dialog", 0, 15));
		jMenuHelpAbout.setText("关于");
		jMenuHelpAbout.addActionListener(new mainFrame_HelpAbout_ActionAdapter(this));

		reader_management.setFont(new java.awt.Font("Dialog", 0, 15));
		reader_management.setText("读者管理");

		add_readerInfo.setFont(new java.awt.Font("Dialog", 0, 15));
		add_readerInfo.setForeground(Color.black);
		add_readerInfo.setText("添加读者信息");
		add_readerInfo.addActionListener(new mainFrame_addReaderInfo_actionAdapter(this));

		change_readerInfo.setFont(new java.awt.Font("Dialog", 0, 15));
		change_readerInfo.setText("查改删读者信息");
		change_readerInfo.addActionListener(new mainFrame_changeReaderInfo_actionAdapter(this));

		queryReader.setFont(new java.awt.Font("Dialog", 0, 15));
		queryReader.setText("查询读者信息");
		queryReader.addActionListener(new mainFrame_scanningUserInfo_actionAdapter(this));

		borrowAndreturn.setFont(new java.awt.Font("Dialog", 0, 15));
		borrowAndreturn.setText("借阅管理");

		book_management.setFont(new java.awt.Font("Dialog", 0, 15));
		book_management.setText("图书管理");

		AddBook.setFont(new java.awt.Font("Dialog", 0, 15));
		AddBook.setText("添加图书");
		AddBook.addActionListener(new mainFrame_AddBook_actionAdapter(this));

		borrowBook.setFont(new java.awt.Font("Dialog", 0, 15));
		borrowBook.setText("借阅图书");
		borrowBook.addActionListener(new mainFrame_borrowBook_actionAdapter(this));

		returnBook.setFont(new java.awt.Font("Dialog", 0, 15));
		returnBook.setText("归还图书");
		returnBook.addActionListener(new mainFrame_returnBook_actionAdapter(this));

		ChangeBookInfo.setFont(new java.awt.Font("Dialog", 0, 15));
		ChangeBookInfo.setText("改删图书信息");
		ChangeBookInfo.addActionListener(new mainFrame_ChangeBookInfo_actionAdapter(this));

		queryBookInfo.setFont(new java.awt.Font("Dialog", 0, 15));
		queryBookInfo.setText("查询图书信息");
		queryBookInfo.addActionListener(new mainFrame_scanningBookInfo_actionAdapter(this));

		Menu_UserManager.add(login);
		Menu_UserManager.add(scanningUser);
		Menu_UserManager.add(changePasswd);
		Menu_UserManager.add(adduser);
		Menu_UserManager.add(deleteuser);
		Menu_UserManager.add(exit);

		jMenuHelp.add(jMenuHelpAbout);

		jMenuBar.add(Menu_UserManager);
		jMenuBar.add(reader_management);
		jMenuBar.add(borrowAndreturn);
		jMenuBar.add(book_management);
		jMenuBar.add(jMenuHelp);

		reader_management.add(add_readerInfo);
		reader_management.add(change_readerInfo);
		reader_management.add(queryReader);

		borrowAndreturn.add(borrowBook);
		borrowAndreturn.add(returnBook);

		book_management.add(AddBook);
		book_management.add(ChangeBookInfo);
		book_management.add(queryBookInfo);

		reader_management.setEnabled(false);
		borrowAndreturn.setEnabled(false);
		book_management.setEnabled(false);
		Menu_UserManager.setEnabled(true);
		scanningUser.setEnabled(false);
		adduser.setEnabled(false);
		deleteuser.setEnabled(false);

		this.setBounds(400, 100, 600, 400);
		this.setVisible(true);
	}

	public static void main(String args[]) {

		/* 设置系统默认布局样式 */
		String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
		try {
			// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}

		SetPersonalFont setfont = new SetPersonalFont();
		setfont.InitGlobalFont(new Font("monspaced", Font.PLAIN, 13));
		MainFrame main = new MainFrame();
		main.setVisible(true);
	}

	public void jMenuFileExit_actionPerformed(ActionEvent e) {
		System.exit(0);
	}

	// Help | About action performed
	public void HelpAbout_actionPerformed(ActionEvent e) {
		new About();
	}

	// Overridden so we can exit when window is closed
	@Override
	protected void processWindowEvent(WindowEvent e) {
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			jMenuFileExit_actionPerformed(null);
		}
	}

	void adduser_actionPerformed(ActionEvent e) {
		new AddUser();
	}

	void deleteuser_actionPerformed(ActionEvent e) {
		new DeleteUser();
	}

	void xgmm_actionPerformed(ActionEvent e) {
		new ChangePassword();
	}

	void borrowBook_actionPerformed(ActionEvent e) {
		new BorrowBook();
	}

	void returnBook_actionPerformed(ActionEvent e) {
		new ReturnBook();
	}

	void exit_actionPerformed(ActionEvent e) {
		System.exit(0);
	}

	void login_actionPerformed(ActionEvent e) {
		final JTextField user_ID = new JTextField(); // 用户ID
		final JPasswordField pwd = new JPasswordField();// 密码
		final JDialog dialog = new JDialog();
		JPanel panel = new JPanel();
		panel.setLayout(null);
		// this.getContentPane().setLayout(null);
		dialog.setTitle("登录");
		dialog.setBounds(500, 200, 300, 250);

		JLabel label_ID = new JLabel(); // 用户ID
		JLabel label_pwd = new JLabel(); // 用户密码
		JLabel label_power = new JLabel(); // 登录身份

		final JComboBox<String> login_power = new JComboBox<String>(); // 登录权限
		login_power.addItem("管理员");
		login_power.addItem("读者");

		label_ID.setText("请输入用户ID:");
		label_ID.setBounds(20, 20, 100, 29);
		user_ID.setBounds(120, 20, 130, 29);
		user_ID.setText("1000920303");

		label_pwd.setText("请输入密码:");
		label_pwd.setBounds(20, 60, 100, 29);
		pwd.setBounds(120, 60, 130, 29);
		pwd.setText("123");

		label_power.setText("身份验证:");
		label_power.setBounds(20, 100, 100, 29);
		login_power.setBounds(120, 100, 130, 29);

		JButton jbSure = new JButton("确定(Y)");
		jbSure.setMnemonic(KeyEvent.VK_Y);
		jbSure.setBounds(40, 150, 80, 29);

		JButton cancel = new JButton("取消(N)");
		cancel.setMnemonic(KeyEvent.VK_N);
		cancel.setBounds(150, 150, 85, 29);
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});

		// 居中显示
		// Toolkit kit = Toolkit.getDefaultToolkit();
		// Dimension screenSize = kit.getScreenSize();
		// dialog.setLocation((screenSize.width - 200) / 2,
		// (screenSize.height - 150) / 2);

		dialog.getContentPane().add(panel, null);
		panel.add(label_ID);
		panel.add(user_ID);
		panel.add(label_pwd);
		panel.add(pwd);
		panel.add(jbSure);
		panel.add(label_power);
		panel.add(login_power);
		panel.add(cancel);

		jbSure.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if ((!(user_ID.getText().equals("")) && !(new String(pwd.getPassword()).equals("")))) {
					try {// 连接数据库,验证用户
						Class.forName(DB_NAME);
						Connection con = DriverManager.getConnection(DB_URL, "sa", "server");
						ps = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
					} catch (ClassNotFoundException | SQLException e1) {
						e1.printStackTrace();
					}

					switch (login_power.getSelectedItem().toString()) {
					case "管理员":

						try {
							rs = ps.executeQuery(
									"select * from UserInfo where UserID='" + user_ID.getText().trim() + "' "); // 读取数据库
							if (!rs.next()) {
								JOptionPane.showMessageDialog(null, "不存在此用户!");
							} else if (!(rs.getString("Password").trim().equals(new String(pwd.getPassword())))) {
								JOptionPane.showMessageDialog(null, "密码错误!");
							} else {
								dialog.dispose();
								reader_management.setEnabled(true);
								borrowAndreturn.setEnabled(true);
								book_management.setEnabled(true);
								Menu_UserManager.setEnabled(true);
								scanningUser.setEnabled(true);
								adduser.setEnabled(true);
								deleteuser.setEnabled(true);
								add_readerInfo.setEnabled(true);
							}
						} catch (SQLException sqle) {
							String error = sqle.getMessage();
							JOptionPane.showMessageDialog(null, error);
							sqle.printStackTrace();
						}
						break;

					case "读者":
						try {// 读取数据库
							rs = ps.executeQuery(
									"select * from ReadersInfo where ReaderID='" + user_ID.getText().trim() + "' ");
							if (!rs.next()) {
								JOptionPane.showMessageDialog(null, "不存在此用户!");
							} else if (!(rs.getString("Password").trim().equals(new String(pwd.getPassword())))) {
								JOptionPane.showMessageDialog(null, "密码错误!");
							} else {
								dialog.dispose();
								book_management.setEnabled(true);
								reader_management.setEnabled(false);
								ChangeBookInfo.setEnabled(false);
								AddBook.setEnabled(false);
								// change_readerInfo.setEnabled(true);
								borrowAndreturn.setEnabled(true);
								add_readerInfo.setEnabled(false);
								queryBookInfo.setEnabled(true);
								Menu_UserManager.setEnabled(true);
								scanningUser.setEnabled(false);
								adduser.setEnabled(false);
								deleteuser.setEnabled(false);
								add_readerInfo.setEnabled(false);
							}
						} catch (SQLException e1) {
							JOptionPane.showMessageDialog(null, e1.toString());
						}

						break;
					}

				} else {
					JOptionPane.showMessageDialog(null, "用户ID或密码不能为空");
				}
			}
		});
		dialog.setModal(true);
		dialog.setVisible(true);
	}

	void addReaderInfo_actionPerformed(ActionEvent e) {
		new AddReaderInfo();
	}

	void changeReaderInfo_actionPerformed(ActionEvent e) {
		new ChangeReadersInfo();
	}

	void scanningUserInfo_actionPerformed(ActionEvent e) {
		new ScanningReadersInfo();
	}

	void scanningUser_actionPerformed(ActionEvent e) {
		new ScanningUser();
	}

	void changePasswd_actionPerformed(ActionEvent e) {
		new ChangePassword();
	}

	void AddBook_actionPerformed(ActionEvent e) {
		new AddBook();
	}

	void ChangeBookInfo_actionPerformed(ActionEvent e) {
		new ChangeBookInfo();
	}

	void scanningBook_actionPerformed(ActionEvent e) {
		new ScanBookInfo();
	}

}

[代码运行效果截图]


java使用JDBC连接SQL数据库,适合于初学者学习(一款小型的图书信息管理)

[源代码打包下载]




网友评论    (发表评论)

共3 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...