登录界面

dzb_branch
丁昭伯 2 years ago committed by Asuna
parent 2a0744c56e
commit a263a7583e

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="libs/mysql-connector-j-8.0.31.jar"/>
<classpathentry kind="lib" path="out/production/UserLogin-master/com/weh/libs/mysql-connector-j-8.0.31.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="jdbc">
<CLASSES>
<root url="jar://D:/Java/mysql-connector-j-8.0.31/mysql-connector-j-8.0.31/mysql-connector-j-8.0.31.jar!/com/mysql/jdbc" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/UserLogin-master.iml" filepath="$PROJECT_DIR$/UserLogin-master.iml" />
</modules>
</component>
</project>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>UserLogin-master</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -0,0 +1,2 @@
Java--用户登录界面连接Mysql数据库
![输入图片说明](https://images.gitee.com/uploads/images/2022/0413/184214_a8326845_9840785.png "未标题-1.png")

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/User.iml" filepath="$PROJECT_DIR$/User.iml" />
</modules>
</component>
</project>

@ -0,0 +1,40 @@
package com.weh.User;
//登录所需要的数据转入
public class Login {
String id;
String password;
LoginDemo loginDemo;
boolean loginSuccess = false;
public LoginDemo getLoginDemo() {
return loginDemo;
}
public void setLoginDemo(LoginDemo loginDemo) {
this.loginDemo = loginDemo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isLoginSuccess() {
return loginSuccess;
}
public void setLoginSuccess(boolean loginSuccess) {
this.loginSuccess = loginSuccess;
}
}

@ -0,0 +1,166 @@
package com.weh.User;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicPanelUI;
import java.awt.*;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
*
*/
public class LoginDemo extends JFrame {
public LoginDemo() {
super("HI登录界面");
//获取显示屏的大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int sw = screenSize.width;
int sh = screenSize.height;
//窗口宽高大小
int width = 500;
int height = 340;
this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);//设置窗口的位置
this.setIconImage(Toolkit.getDefaultToolkit().getImage(LoginDemo.class.getResource("/com/weh/img/chatping.jpg")));//窗口图标
ImageIcon background = new ImageIcon(LoginDemo.class.getResource("/com/weh/img/chat.jpg")); //背景图片
JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel否则不能用方法setOpaque()来使内容窗格透明
imagePanel.setOpaque(false); // 窗口透明
this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景
JPanel panel = new JPanel();
//盒子模块
Box ubox = Box.createHorizontalBox();
Box pbox = Box.createHorizontalBox();
Box vbox = Box.createVerticalBox();
//创建界面工具类
JLabel uLabel = new JLabel(" HI :"); //文本设计
uLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
uLabel.setForeground(new Color(0xFBFCFD)); // 设置前景色
JTextField uField = new JTextField(); //输入框
uField.setFont(new Font("Arial", Font.BOLD, 15));
uField.setToolTipText("HI");// 悬停显示
uField.setColumns(12);
JLabel pLabel = new JLabel("密 码:"); //文本设计
pLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
pLabel.setForeground(new Color(0xFBFCFD)); // 设置前景色
JPasswordField pFd = new JPasswordField();
pFd.setFont(new Font("Arial", Font.BOLD, 15));
pFd.setToolTipText("密码");// 悬停显示
pFd.setColumns(12);
// 如果使用其他回显字符,可以设置大小,但是星星不可以。。。
pFd.setEchoChar('●');// 星星符号
JButton button1 = new JButton("登录"); //登录按钮
button1.setToolTipText("登录");// 悬停显示
JButton button2 = new JButton("重置"); //重置按钮
button2.setToolTipText("重置");// 悬停显示
JMenu Menubutton3 = new JMenu("注册账号"); //重置按钮
Menubutton3.setToolTipText("注册账号");// 悬停显示
// 字体设置
button1.setFont(new Font("微软雅黑", Font.BOLD, 18));
button1.setForeground(Color.white); // 设置前景色
button1.setBackground(new Color(0x08BDFD));
button1.setDefaultCapable(true);
button1.setBounds((this.getWidth() - 120 - 180) / 2, 250, 120, 30); // 设置按钮位置,及按钮大小
button1.setCursor(new Cursor(Cursor.HAND_CURSOR)); //鼠标手势的设置
button2.setFont(new Font("微软雅黑", Font.BOLD, 18));
button2.setForeground(Color.white); // 设置前景色
button2.setBackground(new Color(0x08BDFD));
button2.setDefaultCapable(true);
button2.setCursor(new Cursor(Cursor.HAND_CURSOR));//鼠标手势的设置
button2.setBounds((this.getWidth() - 120 + 180) / 2, 250, 120, 30); // 设置按钮位置,及按钮大小
Menubutton3.setFont(new Font("微软雅黑", Font.BOLD, 12));
Menubutton3.setForeground(new Color(0x02FCFC)); // 设置前景色
Menubutton3.setUI(new BasicButtonUI()); //恢复基本视觉效果
Menubutton3.setBounds(5, 280, 85, 20); // 设置按钮位置,及按钮大小
Menubutton3.setContentAreaFilled(false); // 设置按钮透明
Menubutton3.setCursor(new Cursor(Cursor.HAND_CURSOR));
/*
*/
//小盒子,设计用户名布局模块
ubox.add(uLabel);
ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5作为相邻文本的空隙
ubox.add(uField);
//小盒子,设计密码框布局模块
pbox.add(pLabel);
pbox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5作为相邻文本的空隙
pbox.add(pFd);
//大盒子
vbox.add(Box.createVerticalStrut(80));//插入中间盒子高度为80作为上下文本的空隙
vbox.add(ubox);
vbox.add(Box.createVerticalStrut(60));//插入中间盒子高度为60作为上下文本的空隙
vbox.add(pbox);
uField.setText("3037502828"); //设置默认账号
pFd.setText("123456"); //设置默认密码
panel.setUI(new BasicPanelUI()); //恢复基本视觉效果
panel.setOpaque(false); // 面板透明
panel.add(vbox, BorderLayout.CENTER);//vbox盒子居中
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(button1);
this.add(button2);
this.add(Menubutton3);
this.add(panel);
this.setVisible(true);
this.setResizable(false);
//点击按钮的监听事件
button1.addActionListener(e -> { //登录按钮监听事件
try {
//正则法则,验证输入数据的字符是否都是数字
String HI =uField.getText().trim();
String regex = "^\\d+$";
Pattern p =Pattern.compile(regex);
Matcher m = p.matcher(HI);
if(m.matches()){//匹配成功,则传入输入的数据
init(this,uField, pFd);
}else {
JOptionPane.showMessageDialog(null, "只能输入数字,请正确输入!", "警告", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
}
});
button2.addActionListener(e -> {//重置按钮监听事件
uField.setText("");
pFd.setText("");
});
Menubutton3.addActionListener(e -> {//跳转到注册窗口按钮监听事件
try {
this.dispose();
Thread.sleep(1000);
new RegisterDemo();
} catch (InterruptedException interruptedException) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
}
});
}
public void init(LoginDemo loginDemo,JTextField uField, JPasswordField pFd) {
Login login;
UserLogin UserLogin;
try {
login=new Login();
login.setLoginDemo(loginDemo);
login.setId(uField.getText());
char[] p = pFd.getPassword();
login.setPassword(new String(p));
UserLogin = new UserLogin();
UserLogin.readLogin(login);
} catch (SQLException | ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}

@ -0,0 +1,30 @@
package com.weh.User;
//注册所需要的数据转入
public class Register {
String name;
String id;
String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -0,0 +1,223 @@
package com.weh.User;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicPanelUI;
import java.awt.*;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
*
*/
public class RegisterDemo extends JFrame {
public RegisterDemo() {
//获取显示屏的大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int sw = screenSize.width;
int sh = screenSize.height;
this.setTitle("HI注册界面");
int width = 500;
int height = 600;
this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);// 设置窗口的位置
this.setIconImage(Toolkit.getDefaultToolkit().getImage(RegisterDemo.class.getResource("/com/weh/img/chatping.jpg")));// 图标
ImageIcon background = new ImageIcon(RegisterDemo.class.getResource("/com/weh/img/chat.gif")); // 背景图片
JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel否则不能用方法setOpaque()来使内容窗格透明
imagePanel.setOpaque(false); // 窗口透明
this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景
//盒子模块
Box namebox = Box.createHorizontalBox();
Box ubox = Box.createHorizontalBox();
Box pbox = Box.createHorizontalBox();
Box repbox = Box.createHorizontalBox();
Box vbox = Box.createVerticalBox();
JLabel nameLabel = new JLabel("用户名 :"); //文本设计
nameLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
nameLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
JTextField nameField = new JTextField(); //输入框
nameField.setFont(new Font("黑体", Font.BOLD, 15));
nameField.setToolTipText("用户名");
nameField.setColumns(15);
JLabel uLabel = new JLabel(" HI :"); //文本设计
uLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
uLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
JTextField uField = new JTextField(); //输入框
uField.setFont(new Font("Arial", Font.BOLD, 15));
uField.setToolTipText("HI");
uField.setColumns(15);
JLabel pLabel = new JLabel("密 码 :"); //文本设计
pLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
pLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
JPasswordField pFd = new JPasswordField();
pFd.setFont(new Font("Arial", Font.BOLD, 15));
pFd.setToolTipText("密码");// 悬停显示
pFd.setColumns(15);
// 如果使用其他回显字符,可以设置大小,但是星星不可以。。。
pFd.setEchoChar('●');// 星星符号
JLabel RepLabel = new JLabel("确认密码:"); //文本设计
RepLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
RepLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
JPasswordField RepFd = new JPasswordField();
RepFd.setFont(new Font("Arial", Font.BOLD, 15));
RepFd.setToolTipText("确认密码");// 悬停显示
RepFd.setColumns(15);
// 如果使用其他回显字符,可以设置大小,但是星星不可以。。。
RepFd.setEchoChar('●');// 星星符号
JButton button1 = new JButton("注册"); //登录按钮
button1.setToolTipText("注册");// 悬停显示
JButton button2 = new JButton("重置"); //重置按钮
button2.setToolTipText("重置");// 悬停显示
JMenu Menubutton3 = new JMenu("返回登录"); //重置按钮
Menubutton3.setToolTipText("返回登录");// 悬停显示
button1.setFont(new Font("微软雅黑", Font.BOLD, 18));
button1.setForeground(Color.white); // 设置前景色
button1.setBackground(new Color(0x08BDFD));
button1.setDefaultCapable(true);
button1.setBounds((this.getWidth() - 120 - 180) / 2, this.getHeight() - 150, 120, 30); // 设置按钮位置,及按钮大小
button1.setCursor(new Cursor(Cursor.HAND_CURSOR)); //鼠标手势的设置
button2.setFont(new Font("微软雅黑", Font.BOLD, 18));
button2.setForeground(Color.white); // 设置前景色
button2.setBackground(new Color(0x08BDFD));
button2.setDefaultCapable(true);
button2.setCursor(new Cursor(Cursor.HAND_CURSOR));//鼠标手势的设置
button2.setBounds((this.getWidth() - 120 + 180) / 2, this.getHeight() - 150, 120, 30); // 设置按钮位置,及按钮大小
Menubutton3.setFont(new Font("微软雅黑", Font.BOLD, 12));
Menubutton3.setForeground(Color.white); // 设置前景色
Menubutton3.setBackground(new Color(0x08BDFD));
Menubutton3.setUI(new BasicButtonUI()); //恢复基本视觉效果
Menubutton3.setBounds(5, this.getHeight() - 70, 85, 20); // 设置按钮位置,及按钮大小
Menubutton3.setContentAreaFilled(false); // 设置按钮透明
Menubutton3.setCursor(new Cursor(Cursor.HAND_CURSOR));
/*
*/
//小盒子,设计用户名模块
namebox.add(nameLabel);
namebox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5作为相邻文本的空隙
namebox.add(nameField);
ubox.add(uLabel);
ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5作为相邻文本的空隙
ubox.add(uField);
//小盒子,设计密码模块
pbox.add(pLabel);
pbox.add(Box.createHorizontalStrut(5));
pbox.add(pFd);
repbox.add(RepLabel);
repbox.add(Box.createHorizontalStrut(5));
repbox.add(RepFd);
vbox.add(Box.createVerticalStrut(90));//插入中间盒子高度为90作为上下文本的空隙
vbox.add(namebox);
vbox.add(Box.createVerticalStrut(65));
vbox.add(ubox);
vbox.add(Box.createVerticalStrut(65));
vbox.add(pbox);
vbox.add(Box.createVerticalStrut(65));
vbox.add(repbox);
JPanel panel = new JPanel();
panel.setUI(new BasicPanelUI()); //恢复基本视觉效果
panel.setOpaque(false); // 面板透明
panel.add(vbox, BorderLayout.CENTER);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(button1);
this.add(button2);
this.add(Menubutton3);
this.add(panel);
this.setVisible(true);
this.setResizable(false);
//注册按钮监听
button1.addActionListener(e -> {
String pField = new String(pFd.getPassword());//转换
String repField = new String(RepFd.getPassword());//转换
try {
if (nameField.getText().isEmpty() || pField.isEmpty() || repField.isEmpty()||uField.getText().isEmpty()) {
if (nameField.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "用户名不能为空", "提示", JOptionPane.WARNING_MESSAGE);
} else if(uField.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "HI号不能为空", "提示", JOptionPane.WARNING_MESSAGE);
}
else if (pField.isEmpty()) {
JOptionPane.showMessageDialog(null, "密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "确认密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
}
} else {
if (!pField.equals(repField)) {
JOptionPane.showMessageDialog(null, "两次密码不一致", "提示", JOptionPane.WARNING_MESSAGE);
} else {
String HI =uField.getText().trim();
String regex = "^\\d+$";
Pattern p =Pattern.compile(regex);
Matcher m = p.matcher(HI);
if(m.matches()){
init(nameField,uField, pFd);
}else{
JOptionPane.showMessageDialog(null, "HI号只能输入数字请正确输入", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
}
});
//重置按钮监听
button2.addActionListener(e -> {
nameField.setText(null);
uField.setText(null);
pFd.setText(null);
RepFd.setText(null);
});
//返回登录窗口按钮监听
Menubutton3.addActionListener(e -> {
try {
this.dispose();
Thread.sleep(1000);
new LoginDemo();
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
}
});
}
public void init(JTextField nameField,JTextField uField, JPasswordField pFd) {
Register register;
UserRegister userRegister;
try {
register = new Register();
register.setName(nameField.getText());
register.setId(uField.getText());
char[] p1 = pFd.getPassword();
register.setPassword(new String(p1));
userRegister = new UserRegister();
userRegister.writeRegister(register);
uField.setText(null);
nameField.setText(null);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.weh.User" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,64 @@
package com.weh.User;
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.swing.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//匹配输入的登录数据,以及对数据库驱动的开启
public class UserLogin {
Connection connection = null;
PreparedStatement prepare;
ResultSet resultSet;
boolean loginSuccess;
public UserLogin() throws SQLException, ClassNotFoundException {
//连接mysql数据库
try {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setURL("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false");//本地IP:端口/数据库名
mysqlDataSource.setUser("root");//mysql数据库的用户名
mysqlDataSource.setPassword("123456");//密码
connection = mysqlDataSource.getConnection();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "数据库连接失败", "提示", JOptionPane.WARNING_MESSAGE);
}
}
public void readLogin(Login login) {
try {
String sql = "SELECT * FROM test.User WHERE UserID = ? AND UserPassword = ?";//查询语句
prepare = connection.prepareStatement(sql);//创建执行环境
prepare.setString(1, login.getId());
prepare.setString(2, login.getPassword());
resultSet = prepare.executeQuery();//执行查询语句,如数据匹配成功则返回true
if (resultSet.next()) { //迭代查询
login.setLoginSuccess(true);
JOptionPane.showMessageDialog(null, "登录成功");
} else {
login.setLoginSuccess(false);
if (login.getId().isEmpty() || login.getPassword().isEmpty()) {
if (login.getId().isEmpty()) {
JOptionPane.showMessageDialog(null, "用户名不能为空!", "提示", JOptionPane.WARNING_MESSAGE);
}
if (login.getPassword().isEmpty()){
JOptionPane.showMessageDialog(null, "密码不能为空!","提示", JOptionPane.WARNING_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null, "用户名不存在!", "警告", JOptionPane.ERROR_MESSAGE);
}
}
//释放资源和空间
connection.close();
prepare.close();
resultSet.close();
loginSuccess = login.isLoginSuccess();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.ERROR_MESSAGE);
}
}
}

@ -0,0 +1,48 @@
package com.weh.User;
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//插入数据,以及对数据库驱动的开启
public class UserRegister { //注册数据处理者
Connection connection = null;
PreparedStatement presql; //预处理对象
public UserRegister() throws SQLException {
try {
//连接mysql数据库
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setURL("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false");
mysqlDataSource.setUser("User");
mysqlDataSource.setPassword("");
connection = mysqlDataSource.getConnection();
} catch (SQLException throwables) {
JOptionPane.showMessageDialog(null,"数据库连接失败","警告",JOptionPane.WARNING_MESSAGE);
}
}
public void writeRegister(Register register){
int flag;
try {
String sql = "INSERT INTO test.User VALUES (?,?,?)"; //插入语句
presql = connection.prepareStatement(sql);
presql.setString(1,register.getId());
presql.setString(2,register.getPassword());
presql.setString(3,register.getName());
flag = presql.executeUpdate(); //成功插入式返回1
//释放资源和空间
connection.close();
presql.close();
if (flag!=0){
JOptionPane.showMessageDialog(null,"注册成功");
}else {
JOptionPane.showMessageDialog(null,"注册失败","提示",JOptionPane.WARNING_MESSAGE);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"用户名已存在!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.weh" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/libs/mysql-connector-j-8.0.31.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="jdbc">
<CLASSES>
<root url="jar://D:/Java/mysql-connector-j-8.0.31/mysql-connector-j-8.0.31/mysql-connector-j-8.0.31.jar!/com/mysql/jdbc" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/UserLogin-master.iml" filepath="$PROJECT_DIR$/UserLogin-master.iml" />
</modules>
</component>
</project>

@ -0,0 +1,2 @@
Java--用户登录界面连接Mysql数据库
![输入图片说明](https://images.gitee.com/uploads/images/2022/0413/184214_a8326845_9840785.png "未标题-1.png")

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/User.iml" filepath="$PROJECT_DIR$/User.iml" />
</modules>
</component>
</project>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.weh.User" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.weh" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/libs/mysql-connector-j-8.0.31.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Loading…
Cancel
Save