界面展示
具体代码
//DBUtil.java
package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","20214063");
return connection;
}
}
//LoginForm.java
package org.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class LoginForm extends JFrame {
//定义系统的各个组件
private static final long serialVersionUID = 5475179439752076273L;
private Container container = getContentPane();
private JLabel userLabel = new JLabel("用 户:");
private JTextField usernameField = new JTextField();
private JLabel passLabel = new JLabel("密 码:");
private JPasswordField passwordField = new JPasswordField();
private JButton okBtn = new JButton("确定");
private JButton cancelBtn = new JButton("清空");
public void init(){
setTitle("系统登录功能");
setBounds(600,200,350,180);
container.setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置让窗口可见
setVisible(true);
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
userLabel.setBounds(50, 20, 50, 20);
passLabel.setBounds(50, 60, 50, 20);
fieldPanel.add(userLabel);
fieldPanel.add(passLabel);
usernameField.setBounds(110, 20, 160, 20);
passwordField.setBounds(110, 60, 160, 20);
fieldPanel.add(usernameField);
fieldPanel.add(passwordField);
container.add(fieldPanel, "Center");
//按钮组件部分
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(okBtn);
buttonPanel.add(cancelBtn);
container.add(buttonPanel, "South");
listerner();
}
//登录功能的实现
public void listerner() {
okBtn.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
//判断是否在数据库中存在这样一条信息
UserDAO dao=new UserDAO();
boolean a=dao.findUser(username.toString(),password);
if(a){
JOptionPane.showMessageDialog(null, "登录成功");
}else{
JOptionPane.showMessageDialog(null, "登录失败");
}
} catch (ClassNotFoundException | SQLException ex) {
throw new RuntimeException(ex);
}
}
});
//清空输入信息
cancelBtn.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
usernameField.setText("");
passwordField.setText("");
}
});
}
}
//UserDAO.java
package org.example;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDAO {
public boolean findUser(String userName,String password) throws SQLException, ClassNotFoundException {
Connection connection=DBUtil.getConnection();
PreparedStatement psmt=connection.prepareStatement("select * from design where id=?");
psmt.setString(1,userName);
ResultSet rs=psmt.executeQuery();
String pa="";
int count=0;
while(rs.next()){
pa=rs.getString(2);
if(pa.equals(password)){
count=1;
}else{
count=0;
}
}
rs.close();
psmt.close();
connection.close();
if(count!=0){
return true;
}else{
return false;
}
}
}
//Main.java
package org.example;
public class Main {
public static void main(String[] args) {
LoginForm loginForm=new LoginForm();
loginForm.init();
}
}
标签:java,登录,软件设计,数据库,private,add,import,new,public
From: https://www.cnblogs.com/liuzijin/p/17782147.html