本篇博客是java对mysql的简单操作的综合运用——登录系统、java对mysql的简单操作的综合运用——注册系统、java对mysql的简单操作的综合运用——修改密码系统的整合。
因为使用的是数据库中的数据,故代码中会有很多代码是有关数据库的,如有疑问,最下方传送门。
整合后的代码共四个界面,分别是登录界面,注册界面,菜单界面,修改密码界面。
登录界面
登录界面在原来的基础上设置了用户密码的隐私保护,并且给按钮设置了页面跳转。
代码如下(注:dispose()的作用是关闭当前的界面)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class login extends JFrame implements ActionListener{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/message";
static final String USER = "root";
static final String PASS = "123456";
JButton jb1=new JButton("登录");
JButton jb2=new JButton("注册");
JLabel jl1=new JLabel("账号:");
JLabel jl2=new JLabel("密码:");
JTextField jt1=new JTextField();
JPasswordField jt2=new JPasswordField ();
public login() {
//jt2.setEchoChar((char)0);//密码可视
jt1.setBounds(90, 48, 180, 25);
jt2.setBounds(90, 98, 180, 25);
jl1.setBounds(44, 42, 70, 35);
jl2.setBounds(44, 92, 70, 35);
jb1.setBounds(60, 150, 70, 35);
jb2.setBounds(190, 150, 70, 35);
this.add(jb1);this.add(jl1);this.add(jl2);this.add(jt1);this.add(jt2);this.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setTitle("login");
this.setSize(350,250);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new login();
}
@Override
public void actionPerformed(ActionEvent e1) {
// TODO Auto-generated method stub
if(e1.getSource()==jb1) {
if(jt1.getText().equals("")||jt2.getText().equals("")) {
JOptionPane.showMessageDialog(null, "账号或密码不能为空", "警告",JOptionPane.ERROR_MESSAGE);
}
else {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM login";
ResultSet rs = stmt.executeQuery(sql);
int pd=0;
while(rs.next()){
// 通过字段检索
String id = rs.getString("id");
String password = rs.getString("password");
if(id.equals(jt1.getText())&&password.equals(jt2.getText())) {
JOptionPane.showMessageDialog(null, "账号密码正确,登陆成功!", "提示",JOptionPane.INFORMATION_MESSAGE );
pd=1;
new menu(id);dispose();//页面跳转
break;
}
}
if(pd==0) {JOptionPane.showMessageDialog(null, "账号或密码错误", "警告",JOptionPane.ERROR_MESSAGE);}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
else { new zhuce();dispose();}
}
}
界面截图
注册界面
注册界面与之前的代码相比没有做太大的改变。
代码如下(注:dispose()的作用是关闭当前的界面)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
public class zhuce extends JFrame implements ActionListener{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/message";
static final String USER = "root";
static final String PASS = "123456";
JButton jb1=new JButton("确定");
JButton jb2=new JButton("返回");
JLabel jl1=new JLabel("账号:");
JLabel jl2=new JLabel("密码:");
JLabel jl3=new JLabel("确认密码:");
JTextField jt1=new JTextField();
JTextField jt2=new JTextField();
JTextField jt3=new JTextField();
public zhuce() {
this.add(jb1);this.add(jb2);
this.add(jl1);this.add(jl2);this.add(jl3);
this.add(jt1);this.add(jt2);this.add(jt3);
jt1.setBounds(90, 18, 180, 25);
jt2.setBounds(90, 68, 180, 25);
jt3.setBounds(90, 118, 180, 25);
jl1.setBounds(44, 12, 70, 35);
jl2.setBounds(44, 62, 70, 35);
jl3.setBounds(30, 112, 70, 35);
jb1.setBounds(60, 160, 70, 35);
jb2.setBounds(190, 160, 70, 35);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setTitle("账号注册");
this.setSize(350,250);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e1) {
// TODO Auto-generated method stub
if(e1.getSource()==jb1) {
if(jt1.getText().length()==0) {JOptionPane.showMessageDialog(null, "账号不能为空", "警告",JOptionPane.ERROR_MESSAGE);}
else if(jt2.getText().length()==0||jt3.getText().length()==0) {
JOptionPane.showMessageDialog(null, "密码不能为空!", "警告",JOptionPane.ERROR_MESSAGE);
}
else if(!jt2.getText().equals(jt3.getText())) {
JOptionPane.showMessageDialog(null, "两次密码不相同,请重新输入!", "警告",JOptionPane.ERROR_MESSAGE);
}
else {
Connection conn = null;
Statement stmt = null;
PreparedStatement ps=null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM login";
ResultSet rs = stmt.executeQuery(sql);
int pd=0;
while(rs.next()){
// 通过字段检索
String id = rs.getString("id");
if(id.equals(jt1.getText())) {
JOptionPane.showMessageDialog(null, "账号已被注册,请重新输入!", "警告",JOptionPane.ERROR_MESSAGE);
pd=1;
break;
}
}
if(pd==0) {
sql="INSERT INTO login VALUES(?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1, jt1.getText());
ps.setString(2, jt2.getText());
if(ps.executeUpdate()!=0) {
JOptionPane.showMessageDialog(null, "账号注册成功!", "提示",JOptionPane.INFORMATION_MESSAGE );
new login();
dispose();
}
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
else {
new login();
dispose();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new zhuce();
}
}
界面截图
菜单界面
菜单界面做了传参处理,登录成功后会将用户名传到当前界面,并在Label中显示。
代码如下(注:dispose()的作用是关闭当前的界面)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class menu extends JFrame implements ActionListener{
JButton jb1=new JButton("修改密码");
JButton jb2=new JButton("切换用户");
JLabel jl1=new JLabel();
String user_id;
public menu(String id) {
user_id=id;
jl1.setText("用户:"+id);
this.add(jl1);
this.add(jb1);
this.add(jb2);
jl1.setBounds(120,20,100,30);
jb1.setBounds(110, 60, 100, 50);
jb2.setBounds(110, 130, 100, 50);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setTitle("菜单");
this.setSize(350,250);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb1) {new password(user_id);dispose();}
else {new login();dispose();}
}
}
界面截图
修改密码界面
修改密码界面做了传参处理,将用户名传到修改密码界面,并在TextField 里面显示,且那个TextField 设置为不可编辑。
代码如下(注:dispose()的作用是关闭当前的界面)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class password extends JFrame implements ActionListener{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/message";
static final String USER = "root";
static final String PASS = "123456";
JButton jb1=new JButton("确定");
JButton jb2=new JButton("返回");
JLabel jl1=new JLabel("账号:");
JLabel jl2=new JLabel("原密码:");
JLabel jl3=new JLabel("新密码:");
JLabel jl4=new JLabel("确认密码:");
JTextField jt1=new JTextField();
JTextField jt2=new JTextField();
JTextField jt3=new JTextField();
JTextField jt4=new JTextField();
String user_id;
public password(String id) {
user_id=id;
//添加组件
this.add(jb1);this.add(jb2);
this.add(jt1);this.add(jt2);this.add(jt3);this.add(jt4);
this.add(jl1);this.add(jl2);this.add(jl3);this.add(jl4);
//设置组件位置和大小
jb1.setBounds(80, 250, 70, 35);
jb2.setBounds(180, 250, 70, 35);
jt1.setBounds(105, 48, 180, 25);
jt2.setBounds(105, 98, 180, 25);
jt3.setBounds(105, 148, 180, 25);
jt4.setBounds(105, 198, 180, 25);
jl1.setBounds(70, 42, 70, 35);
jl2.setBounds(57, 92, 70, 35);
jl3.setBounds(57, 142, 70, 35);
jl4.setBounds(44, 192, 70, 35);
jt1.setText(user_id);
jt1.setEditable(false);
jb1.addActionListener(this);
jb2.addActionListener(this);
//设置窗口属性
this.setTitle("修改密码");
this.setSize(350,350);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e1) {
// TODO Auto-generated method stub
if(e1.getSource()==jb1) {
if(jt1.getText().equals("")||jt2.getText().equals("")) {
JOptionPane.showMessageDialog(null, "账号或原密码不能为空", "警告",JOptionPane.ERROR_MESSAGE);
}
else if(jt3.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请输入新的密码", "警告",JOptionPane.ERROR_MESSAGE);
}
else if(jt4.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请确认新的密码", "警告",JOptionPane.ERROR_MESSAGE);
}
else if(!jt3.getText().equals(jt4.getText())) {
JOptionPane.showMessageDialog(null, "两次密码不相同", "警告",JOptionPane.ERROR_MESSAGE);
}
else {
Connection conn = null;
Statement stmt = null;
PreparedStatement ps=null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM login";//搜索login表
ResultSet rs = stmt.executeQuery(sql);
int pd=0;
while(rs.next()){
int id = rs.getInt("id");//得到“id”列的值
String password = rs.getString("password");//得到“password”列的值
if(jt1.getText().equals(String.valueOf(id))) {
pd=1;
if(jt2.getText().equals(password)) {
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql1;
sql1="UPDATE login SET password=? WHERE id=?";//向login表里修改数据
ps=conn.prepareStatement(sql1);//修改数据预处理
ps.setString(1, jt3.getText());
ps.setString(2, String.valueOf(id));
ps.executeUpdate();//执行修改数据
ps.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "密码修改成功!", "提示",JOptionPane.INFORMATION_MESSAGE );
new menu(user_id);dispose();
}
else {
JOptionPane.showMessageDialog(null, "原密码输入错误,请重输", "警告",JOptionPane.ERROR_MESSAGE);
}
break;
}
}
if(pd==0) {JOptionPane.showMessageDialog(null, "账号不存在,请重输", "警告",JOptionPane.ERROR_MESSAGE);}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
else {new menu(user_id);dispose();}
}
}
界面截图
如有错误