第一步,eplicse导入驱动包,必须要对应版本的
写入mysql语句在数据库中
DROP TABLE IF EXISTS `table`; CREATE TABLE `table` ( `name` char(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `password` int DEFAULT NULL, PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; -- ---------------------------- -- Records of table -- ---------------------------- INSERT INTO `table` VALUES ('admin', '666'); INSERT INTO `table` VALUES ('gg', '123'); INSERT INTO `table` VALUES ('yy', '123');
二,使用了两个类dd 和 dj
dj-----mysql连接java,并且设置compare函数,用于判断传入的值与数据库里面的值是否一致
package denglu; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; public class dj { Connection con = null; Statement statement = null; ResultSet res = null; String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/denglu?useUnicode=true&characterEncoding=UTF-8&serverTimezone=CST";//denglu是自己创的mysql库名 String name = "root"; String passwd = "123456"; public dj() { try { Class.forName(driver).newInstance(); con = DriverManager.getConnection(url, name, passwd); statement = con.createStatement(); } catch (ClassNotFoundException e) { System.out.println("对不起,找不到这个Driver"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public boolean compare(String name, String password) { boolean m = false; String sql = "select password from table where name=\"" + name + "\"";//自己在denglu这个库里面创建table表并写入mysql语句 System.out.println("sql="+sql); try { res = statement.executeQuery(sql); System.out.println("res="+res); if (res.next()) { String pa = res.getString(1); System.out.println(pa + " " + password); if (pa.equals(password)) { m = true; } else { JOptionPane.showMessageDialog(null, "密码错误!"); } } else { JOptionPane.showMessageDialog(null, "用户名不存在!"); } res.close(); con.close(); statement.close(); } catch (SQLException e) { e.printStackTrace(); } return m; } }
dd--------用于布局,实现点击函数
package denglu; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class dd extends JFrame { JPanel pan = new JPanel(); JLabel namelab = new JLabel("用户名"); JLabel passlab = new JLabel("密 码"); JTextField nametext = new JTextField(); JPasswordField passtext = new JPasswordField(); public JButton denglu = new JButton("登录"); public dd() { Font font = new Font("楷体", Font.BOLD, 12); super.setTitle("欢迎登录本系统"); pan.setLayout(null); namelab.setBounds(20, 20, 60, 30); nametext.setBounds(90, 20, 140, 30); passlab.setBounds(20, 60, 60, 30); passtext.setBounds(90, 60, 140, 30); denglu.setBounds(100, 120, 90, 20);//距离x,距离y,宽,长 pan.add(namelab); pan.add(nametext); pan.add(passlab); pan.add(passtext); pan.add(denglu); passtext.setFont(font); super.add(pan); super.setSize(300, 250); super.setVisible(true); denglu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {//监听 denglu(); } }); } public static void main(String[] args) { new dd();//执行布局 } //登录按钮的事件处理函数 public void denglu() { dj dl = new dj(); String username = nametext.getText(); String password = passtext.getText(); if (dl.compare(username, password)) { JOptionPane.showMessageDialog(null, "登录成功!"); super.setVisible(false); } } }