JDBC编程作业--登陆界面的实现(检测数据库中是否有用户名,并且可以注册用户)!

package 数据库;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class database extends JFrame
{
	private JPanel contentPane;
	private JTextField textField_name;

	private java.sql.Statement stat;
	boolean flag = false;
	boolean s = true;
	StringBuffer string = new StringBuffer();

	public static void main(String[] args)
	{
		database frame = new database();
		frame.setVisible(true);
	}

	public database()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 573, 381);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel name = new JLabel("\u7528\u6237\u540D\uFF1A");
		name.setFont(new Font("宋体", Font.PLAIN, 20));
		name.setBounds(83, 76, 86, 18);
		contentPane.add(name);

		textField_name = new JTextField();
		textField_name.setBounds(218, 71, 217, 24);
		contentPane.add(textField_name);
		textField_name.setColumns(10);

		JLabel password = new JLabel("\u5BC6 \u7801\uFF1A");
		password.setFont(new Font("宋体", Font.PLAIN, 20));
		password.setBounds(83, 136, 85, 18);
		contentPane.add(password);

		JPasswordField Password = new JPasswordField();
		Password.setBounds(220, 130, 214, 24);
		contentPane.add(Password);
		Password.setColumns(10);

		JLabel repassword = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
		repassword.setFont(new Font("宋体", Font.PLAIN, 20));
		repassword.setBounds(83, 201, 117, 18);
		contentPane.add(repassword);

		JPasswordField Repassword = new JPasswordField();
		Repassword.setBounds(221, 195, 215, 24);
		contentPane.add(Repassword);
		Repassword.setColumns(10);

		JLabel show = new JLabel("");
		show.setFont(new Font("宋体", Font.PLAIN, 12));
		show.setBounds(353, 232, 196, 81);
		contentPane.add(show);

		JButton see = new JButton("\u68C0\u6D4B");
		see.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String sqlString = textField_name.getText();
				if (sqlString.isEmpty() || sqlString.length() > 18)
				{
					JOptionPane.showMessageDialog(null, "输入不能为空或不能超过18位!");
				}
				try
				{

					ResultSet set = stat
							.executeQuery("select ID from user where ID= '"
									+ sqlString + "'");
					set.next();
					int rows = set.getRow();
					if (rows != 0)
					{
						JOptionPane.showMessageDialog(null, "用户名存在,请注册!");

					} else
					{
						JOptionPane.showMessageDialog(null, "用户名可用,请输入密码!");
					}
				} catch (SQLException e1)
				{
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "sql有异常!");
				}

			}
		});
		see.setBounds(441, 69, 103, 27);
		contentPane.add(see);

		JButton click = new JButton("\u6CE8\u518C");
		click.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String psw1 = new String(Password.getPassword());
				String psw2 = new String(Repassword.getPassword());
				if (psw1.length() > 18 || psw2.length() > 18)
				{
					JOptionPane.showMessageDialog(null, "密码不能超过18位,请重新输入!");
				}
				if (psw1.equals(psw2))
				{
					try
					{
						stat.executeUpdate("insert into user(ID,NUM) values("
								+ "'" + textField_name.getText() + "','"
								+ new String(Password.getPassword()) + "'"
								+ ")");
						JOptionPane.showMessageDialog(null, "注册成功!");
					} catch (SQLException e1)
					{
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, "有误!");
					}

				}

				else
				{
					JOptionPane.showMessageDialog(null, "两次输入密码不同,请重新输入!");
				}

			}

		});
		click.setFont(new Font("宋体", Font.PLAIN, 18));
		click.setBounds(91, 282, 113, 27);
		contentPane.add(click);

		JButton retry = new JButton("\u91CD\u586B");
		retry.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				textField_name.setText("");
				Password.setText("");
				Repassword.setText("");

			}
		});
		retry.setFont(new Font("宋体", Font.PLAIN, 18));
		retry.setBounds(226, 281, 113, 27);
		contentPane.add(retry);

		// 连接数据库

		String URL = "jdbc:mysql://localhost:3306/userdb";//mysql数据库链接!!jdbc:mysql://localhost:3306/表名
		String user = "root";//sql用户名
		String psd = "871185723";//sql密码
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(URL, user, psd);
			stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			JOptionPane.showMessageDialog(null, "SQL数据库链接成功!");
		}

		catch (SQLException | ClassNotFoundException e)
		{
			JOptionPane.showMessageDialog(null, "SQL链接不成功!");
		} finally
		{
			JOptionPane.showMessageDialog(null, "登陆开始!");
		}

	}

}

JDBC编程作业--登陆界面的实现(检测数据库中是否有用户名,并且可以注册用户)!_第1张图片


JDBC编程给我最大的感触就是,可以用sql对数据进行管理和储存,快捷便利。并且为以后的前端开发有很大的帮助。

实验总结:

1.熟悉数据库的使用。

    操作mysql的命令行:

       show databases;//显示数据

       use 表明;//使用表

       show tables;//显示表中信息

       use user;//使用表中的信息

       describe user;//选择该信息

       select *from user;//显示该user中的所有信息成员

       insert into user(‘ ’,‘ ’);//将信息插入到表中

       {除此之外,又需要傻瓜式sql操作的软件的可以找我}

2.掌握数据库如何在JFrame中使用。

     String URL = "jdbc:mysql://localhost:3306/userdb";
String user = "root";
String psd = "871185723";
Class.forName("com.mysql.jdbc.Driver");  
Connection con = DriverManager.getConnection(URL, user, psd);  //connection接口用于连接数据库和java程序。
Statement  stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);//Statement接口执行静态的SQL语句包括更新语句。其中有execute(String sqlStatement),executeQuery(String sqlQuery), exeuteUpdate(String sqlStatement),close();
JOptionPane.showMessageDialog(null, "SQL数据库链接成功!");//JOptionPane.showMessageDialog 是弹出提示框的用法!

3.事件监听器的设置

   必须考虑到所有的事件发生的可能性,并能在程序中提示出来。保持程序的健壮性是很关键的。

4.投入大量的时间去满满理解和体会编程的伟大和美丽!

                            

  不管未来怎样,就让时间说话!


你可能感兴趣的:(java)