目录
一、需求分析
考虑这样一个需求,需要进行登录和注册的验证过程。其次我们需要图形化界面,并且提示是否登录成功,或者是否能够注册成功。
二、准备工作
1.准备开发环境
在这里我推荐使用IDEA这款强大的集成开发工具,这款软件是收费的,相信大家都懂的吧,我就不多说了。
2.准备数据库
既然是Java编写,那么肯定用MySQL啦,至于具体怎么下载和安装以及配置MySQL,大家可以参考其他大佬的文章,我这里也不多说了。
3.准备外包
因为要使用jdbc技术,那么必须得导入一个外包,用于连接MySQL,可以去网上搜索下载。下面我会细说具体的操作过程。
三、具体步骤
1.打开IDEA,新建一个空项目
2.建完后,导入新的模块
导入新模块的同时建好这三个包。
3.导入JDBC要使用的外包,jar包。
在这里导入即可,点击加号,然后找到你下载的那个连接包,里面有一个jar包。
这样各种各样的复杂环境就弄好了,接下来只需要写代码就行了。
四、代码实现
1.界面类
1.1登录界面
那么首先就是登录界面啦,考虑到登录界面,我们需要输入用户名和密码,还有一个登录按钮,以及一个注册按钮。怎么实现登录后提示等操作呢,那么就要添加鼠标监听。
package com.jdbc.ui;
import com.jdbc.connecter.ConectMysqlforlogin;
import com.jdbc.connecter.ConectMysqlforsign;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class CreateLoginUI extends JFrame implements MouseListener {
JButton btn1=new JButton("登录");//定义登录按钮
JButton btn2=new JButton("注册");//定义注册按钮
JTextField usernamefield=new JTextField();//设置输入用户名的文本框
JTextField passwordfeild=new JTextField();//设置输入密码的文本框
JLabel label1=new JLabel("用户名:");
JLabel label2=new JLabel("密码:");
/**
* 带有参数的构造方法
* @param width 设置窗体的宽
* @param height 设置窗体的高
* @param defaultOperation 设置默认的关闭方式
*/
public CreateLoginUI(String title, int width, int height, int defaultOperation){
setTitle(title);
setSize(width,height);
setDefaultCloseOperation(defaultOperation);
setLayout(null);
setLocationRelativeTo(null);
//添加登录按钮
btn1.setBackground(Color.yellow);
btn1.setBounds(0,height/8,width/10*3,height/10);
getContentPane().add(btn1);
btn1.addMouseListener(this);
//添加注册按钮
btn2.setBackground(Color.blue);
btn2.setBounds(width/10*3,height/8,width/10*3,width/10);
getContentPane().add(btn2);
btn2.addMouseListener(this);
//添加用户名、密码
label1.setBounds(0,0,width/10,height/16);
getContentPane().add(label1);
label2.setBounds(0,height/16,width/10,height/16);
getContentPane().add(label2);
//添加文本框
usernamefield.setBounds(width/10,0,width/2,height/16);
passwordfeild.setBounds(width/10,height/16,width/2,height/16);
getContentPane().add(usernamefield);
getContentPane().add(passwordfeild);
setVisible(true);
}
/**
* 设置鼠标点击的动作
* @param e
*/
@Override
public void mouseClicked(MouseEvent e) {
Object obj=e.getSource();
if(obj==btn1){
ConectMysqlforlogin conectMysqlforlogin=new ConectMysqlforlogin(usernamefield.getText(),passwordfeild.getText());
if(conectMysqlforlogin.isIn==true){
showUI showSL=new showUI("登录成功");
}
else{
showUI showFL=new showUI("登录失败");
}
}
else if(obj==btn2){
CreateSignUI signUI=new CreateSignUI("注册界面",400,400);
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
1.2注册界面
同样注册界面也要用户名和密码的输入框以及一个确定按钮。
package com.jdbc.ui;
import com.jdbc.connecter.ConectMysqlforsign;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowListener;
public class CreateSignUI extends JFrame implements MouseListener {
JTextField usernamefield=new JTextField();//设置输入用户名的文本框
JTextField passwordfeild=new JTextField();//设置输入密码的文本框
JLabel label1=new JLabel("用户名:");
JLabel label2=new JLabel("密码:");
JButton btn=new JButton("确定");
public CreateSignUI(String title,int width,int height){
setSize(width,height);
setTitle(title);
setDefaultCloseOperation(2);
setLocationRelativeTo(null);
setLayout(null);
btn.setBackground(Color.white);
btn.setBounds(0,height/8,width/10*3,height/10);
getContentPane().add(btn);
btn.addMouseListener(this);
//添加用户名、密码
label1.setBounds(0,0,width/10,height/16);
getContentPane().add(label1);
label2.setBounds(0,height/16,width/10,height/16);
getContentPane().add(label2);
//添加文本框
usernamefield.setBounds(width/10,0,width/2,height/16);
passwordfeild.setBounds(width/10,height/16,width/2,height/16);
getContentPane().add(usernamefield);
getContentPane().add(passwordfeild);
setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
ConectMysqlforsign conectMysqlforsign=new ConectMysqlforsign(usernamefield.getText(),passwordfeild.getText());
if(conectMysqlforsign.count==1){
}
else{
showUI showFS=new showUI("用户名已存在");
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
1.3提示窗口
最后就是怎么提示有没有成功。
package com.jdbc.ui;
import javax.swing.*;
public class showUI extends JFrame {
public showUI(String title){
setSize(100,100);
setDefaultCloseOperation(2);
setLocationRelativeTo(null);
setTitle(title);
JLabel label=new JLabel(title,SwingConstants.CENTER);
getContentPane().add(label);
setVisible(true);
}
}
2.连接类
2.1登录连接
登录连接MySQL,然后实现查询操作,如果用户名和密码都在数据库中,那么就能成功登录。
package com.jdbc.connecter;
import java.sql.*;
public class ConectMysqlforlogin {
private String url="jdbc:mysql://localhost:3306/inf";
private String user="root";
private String psd="zqh123";
public Boolean isIn=false;
Connection conn1 =null;
PreparedStatement ps1 =null;
ResultSet rs1 =null;
public ConectMysqlforlogin(String user, String password){
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
conn1 =DriverManager.getConnection(url,this.user,psd);
//数据库预编译操作对象获取
String sql="select * from t_user where username=? and password=?";
ps1 = conn1.prepareStatement(sql);
ps1.setString(1,user);
ps1.setString(2,password);
//执行sql语句
rs1=ps1.executeQuery();
if(rs1.next()){
isIn=true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs1 !=null){
try {
rs1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps1 !=null){
try {
ps1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn1 !=null){
try {
conn1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2.2注册连接类
注册连接数据库,要实现插入操作,如果数据库中已有账号被注册也要提示。
package com.jdbc.connecter;
import java.sql.*;
public class ConectMysqlforsign {
private String url="jdbc:mysql://localhost:3306/inf";
private String user="root";
private String psd="zqh123";
Connection conn2 =null;
PreparedStatement ps2 =null;
ResultSet rs2 =null;
Statement stmt=null;
public int count=0;
public ConectMysqlforsign(String user, String password){
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
conn2 = DriverManager.getConnection(url,this.user,psd);
//数据库预编译操作对象获取
String sql1="INSERT into t_user(username,password) values(?,?)";
String sql2="select * from t_user where username='"+user+"'";
stmt=conn2.createStatement();
rs2=stmt.executeQuery(sql2);
if(!rs2.next()){
ps2 = conn2.prepareStatement(sql1);
ps2.setString(1,user);
ps2.setString(2,password);
count=ps2.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs2 !=null){
try {
rs2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps2 !=null){
try {
ps2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn2 !=null){
try {
conn2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3.测试类
package com.jdbc.test;
import com.jdbc.ui.CreateLoginUI;
public class JDBCTest1{
public static void main(String[] args) {
CreateLoginUI ui=new CreateLoginUI("用户登录界面",400,400,3);
}
}
五、最终效果展示
另外需要注意的是,连接数据库的用户名和密码,以及所使用的数据库,每个人不一样,所以不要照抄我的代码。
粗糙的界面,哈哈哈哈哈哈
说明数据库里面没有这个账号
六、总结
总之,我写的很菜很菜,密码域其实可以改成password类型的,这样就不会显示你输入的内容了,后期其实还能完善完善,这是我第一个博客,哈哈哈哈。