-
登陆界面
- 代码
GUI界面
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import javax.imageio.ImageIO;
import javax.security.auth.login.LoginContext;
import javax.swing.*;
import com.mysql.jdbc.PreparedStatement;
import UserMysql.connectLog_Mysql;
public class userLog_Window extends connectLog_Mysql { //此类中只调用WindowLog里面的方法
public static void main(String args[]){
//调用WindowLogin里面的方法
WindowLogin win = new WindowLogin();
}
}
class WindowLogin extends JFrame implements ActionListener{ //JFrame的子类
//声明组件
JLabel jlpic = new JLabel();
JFrame jf ;
JLabel Title = new JLabel("用户管理系统");
JLabel Name = new JLabel("账号:");
JTextField textName = new JTextField(10);
JLabel Passwd = new JLabel("密码:");
JTextField textPasswd = new JTextField(10);
JButton btnLog = new JButton("登录");
public WindowLogin(){ //主要设置窗口属性,调用函数实现组件
//基本设置:
//1.窗口声明
jf = new JFrame("用户登录");
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setSize(1000,800);
jf.setLocation(500,200);
//2.面板声明
JPanel panel = new JPanel();
//设置面板透明
panel.setOpaque(false);
//3.窗口中添加面板
jf.add(panel);
//4.添加组件到面板
placeComponents(panel);
//调用函数
initFrame();
}
private void placeComponents(JPanel panel) { //对组件的具体操作
//设置为布局为null,这样才可以给他绝对定位组件
panel.setLayout(null);
//add添加功能组件进去
Title.setBounds(250, 130, 300, 35);
Title.setForeground(Color.pink);
Title.setFont(new Font("楷体",1,30));
panel.add(Title);
Name.setBounds(200,200,120,25); //一定要先设置他的位置,否则一直都不会显示噢
Name.setFont(new Font("楷体",NORMAL,25));
panel.add(Name);
textName.setBounds(280,200,165,30);
panel.add(textName);
Passwd.setBounds(200,250,80,25);
Passwd.setFont(new Font("楷体",NORMAL,25));
panel.add(Passwd);
textPasswd.setBounds(280,250,165,30);
panel.add(textPasswd);
btnLog.setBounds(320,300,70,30);
btnLog.setBackground(Color.pink);
btnLog.setFont(new Font("楷体",NORMAL,18));
panel.add(btnLog);
//给按钮添加监听器
btnLog.addActionListener(this);
}
public void initFrame() {
ImageIcon icon = new ImageIcon("src\\pink.jpg");
icon.setImage(icon.getImage().getScaledInstance(900,
500, Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jf.add(jlpic);
jf.pack();
}
@Override
public void actionPerformed(ActionEvent e) {
//点击登录后,调用login方法将用户输入的内容与mysql中存储的内容
if(e.getSource() == btnLog) {
login();
}
}
//和Mysql数据库中的数据进行联系
private void login() {
connectLog_Mysql connect = new connectLog_Mysql();
//获取用户输入的用户名和密码
String username = textName.getText();
String password = textPasswd.getText();
System.out.println("用户输入的:"+username + ","+ password);
//在mysql中搜索这个用户名,不存在则输出用户不存在
//若用户名存在,对照密码,密码正确则登录ok,不正确则输出密码不对
if (connect.compare(username, password)) {
JOptionPane.showMessageDialog(null, "登录成功!");
// super.setVisible(false);
jf.dispose();
new WindowChoose();
}
}
}
- 连接MySQL
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import com.mysql.jdbc.PreparedStatement;
public class connectLog_Mysql {
//对应Window窗口中调用的数据库操作方法
public Boolean compare(String username, String password) {
boolean m = false;
try {
//要去掉mysql和jdbc之间的cj!!!
Class.forName("com.mysql.jdbc.Driver");//根据报错信息修改
String url = "jdbc:mysql://localhost:3307/users?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";//链接的mysql
String user = "root";
String passwd = "123456";
Connection con = DriverManager.getConnection(url,user,passwd);//处理异常
System.out.println("Mysql连接成功" + con.toString());//测试是否连接成功
//执行sql语句
Statement sql;
ResultSet rs;
try {
//此方法创建用于执行静态SQL 语句并返回它所生成结果的对象
sql = con.createStatement();
//定义所有查询的语句
String searchALL = "select * from admininfo";
//执行要查询的语句
rs = sql.executeQuery(searchALL);
while(rs.next()) {
String username2 = rs.getString(2);
String password3 = rs.getString(3);
System.out.println("数据库中"+username2 + ":" + password3);
if (password.equals(password3) && username.equals(username2)) {
m = true;
break;
} else if(rs.isLast()){
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
break;
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("error:数据库连接出错");
}
return m;
}
public static void main(String args[]){
connectSearchInfo_Mysql con = new connectSearchInfo_Mysql();
}
}