IDEA环境下Java实现链接数据库注册登录功能

2022年05月12日 阅读数:3
这篇文章主要向大家介绍IDEA环境下Java实现链接数据库注册登录功能,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

项目的文件图在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
数据库中插入了数据在这里插入图片描述java

package dates;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class DBOperation{
	private MyDBConnection myDB=null;
	private Connection conn=null;
	private Statement stmt=null;
	private int scores;
	private int number1;
	private int number2;
	private String name;
	private String password;
	public DBOperation(MyDBConnection myDB){
		conn=myDB.getMyConnection();
		stmt=myDB.getMyStatement();
		number1=0;
		number2=0;
	}
	public void insertData(String name,String password,int scores){
		try{
			String newType1=new String(name.getBytes(),"GBK");//字节转码
			String newType2=new String(password.getBytes(),"GBK");
			String sql="INSERT INTO player(scores,name,password)VALUES("+scores+",'"+newType1+"','"+newType2+"')";
			stmt.executeUpdate(sql);//更新语句
		}catch(Exception e1){
			e1.printStackTrace();
		}
	}
	public void deleteData(int scores){
		String sql="DELETE FROM player WHERE scores="+scores+"";
		System.out.print(sql);
		try{
			stmt.executeUpdate(sql);
			//System.out.println("一条记录被删除");
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void updateData(int mscores,int scores,String name,String password){//修改
		String sql="UPDATE player SET scores="+scores+",name='"+name+"',password='"+password+"'where scores="+mscores+"&&name='"+name+"'&&password='"+password+"'";
		try{
			stmt.executeUpdate(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	public boolean  selectPassword(String mpassword){
		String sql="SELECT scores,name,password FROM player";
		try{
			ResultSet rs=stmt.executeQuery(sql);//返回结果集
			while(rs.next()){//指针向后移动
				password=rs.getString("password");
				number2++;
				//System.out.print(rs.getString("password")+"  ");
				if(password.equals(mpassword)&&(number2==number1)){
					//System.out.print("number2:"+number2);
					return true;
				}
			}

		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	public boolean selectName(String mname){
		String sql="SELECT scores,name,password FROM player";
		try{
			ResultSet rs=stmt.executeQuery(sql);//返回结果集
			while(rs.next()){//指针向后移动
				name=rs.getString("name");
				number1++;
				if(name.equals(mname)){
					//System.out.print("number1:"+number1);
					return true;
				}
			}


		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	public int selectScores(String name,String password){
		String sql="SELECT scores,name,password FROM player where name='"+name+"'&&password='"+password+"'";
		try{
			ResultSet rs=stmt.executeQuery(sql);//返回结果集
			while(rs.next()){//指针向后移动
				scores=rs.getShort("scores");
				return scores;
			}

		}catch(Exception e){
			e.printStackTrace();
		}
	return 0;
	}
	public void selectAll(){
		int i=0;
		String sql="SELECT scores,name,password FROM player";
		try{
			ResultSet rs=stmt.executeQuery(sql);//返回结果集
			while(rs.next()){//指针向后移动
				name=rs.getString("name");
				password=rs.getString("password");
				scores=rs.getShort("scores");

			}
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	public int getScores(){
		return scores;
	}
	public String getName(){
		return name;
	}
	public String getPassword(){
		return password;
	}

	public void setNumber1(){
		number1=0;
	}
	public void setNumber2(){
		number2=0;
	}
}
package dates;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class MyDBConnection{
	private String DBDriver;//链接类
	private String DBURL;
	private String DBUser;
	private String DBPass;
	private Connection conn=null;
	private Statement stmt=null;

	public MyDBConnection(String DBDriver,String DBURL,String DBUser,String DBPass){
		this.DBDriver ="com.mysql.cj.jdbc.Driver";
		this.DBURL ="jdbc:mysql://localhost:3366/info?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
		this.DBUser ="root";
		this.DBPass ="123";
		try{
			Class.forName(this.DBDriver);//加载驱动程序
			System.out.println("数据库驱动程序加载成功");
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			conn=DriverManager.getConnection(this.DBURL, this.DBUser, this.DBPass);//取得链接对象
			stmt=conn.createStatement();//取得SQL语句对象
			System.out.print("链接数据库成功");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public Connection getMyConnection(){
		return conn;
	}
	public Statement getMyStatement(){
		return stmt;
	}
	public void closeMyConnection(){
		try{
			stmt.close();
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public String toString(){
		return "数据库驱动程序"+DBDriver+",连接地址"+DBURL+",用户名"+DBUser+",密码"+DBPass;
	}
}
package ui;

public class Main {
	public static void main(String args[]){
		Window window=new Window();
		window.show();
	}

}

package ui;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

import dates.DBOperation;
import dates.MyDBConnection;

public class Window implements MouseListener {

	public JFrame frame = new JFrame("登陆窗口");
	private JLabel label1=new JLabel("游戏id:");
	private JTextField txt1=new JTextField();
	private JLabel label2=new JLabel("密   码:");
	private JTextField txt2=new JTextField();
	private JButton btn1=new JButton("登录");
	private JButton btn2=new JButton("注册");
	private JButton btn3=new JButton("退出");
	private String text1;
	private String text2;
	private int distinguish;
	String DBDriver="com.mysql.cj.jdbc.Driver";
	String DBURL="jdbc:mysql://localhost:3366/info?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
	String DBUser="root";
	String DBPass="123";
	MyDBConnection myDB=new MyDBConnection(DBDriver,DBURL,DBUser,DBPass);
	public DBOperation myOpr=new DBOperation(myDB);
	public Window(){

	}

	public void show(){
		frame.setLayout(null);
		frame.setSize(470,300);
		frame.setLocation(400, 200);

		Font font=new Font("华文行楷",Font.BOLD,20);
		label1.setFont(font);
		label1.setForeground(Color.gray);
		label2.setFont(font);
		label2.setForeground(Color.gray);
		txt1.setOpaque(false);
		txt2.setOpaque(false);

		btn1.setContentAreaFilled(false);
		btn1.setFont(font);
	    btn1.setForeground(Color.gray);
	    btn1.setBorder(BorderFactory.createRaisedBevelBorder());
	    btn2.setContentAreaFilled(false);
		btn2.setFont(font);
		btn2.setBorder(BorderFactory.createRaisedBevelBorder());
	    btn2.setForeground(Color.gray);
	    btn3.setContentAreaFilled(false);
		btn3.setFont(font);
		btn3.setBorder(BorderFactory.createRaisedBevelBorder());
	    btn3.setForeground(Color.gray);
		JPanel bj = new JPanel() {
			protected void paintComponent(Graphics g) {
				Image bg;
				try {
					bg = ImageIO.read(new File("src/image/开始背景.PNG"));
					g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};

	    label1.setBounds(100,50,100,100);
		txt1.setBounds(180,90, 150, 20);
		label2.setBounds(100,80,100,100);
		txt2.setBounds(180,120, 150, 20);
		btn1.setBounds(100,200,80,20);
		btn2.setBounds(190,200,80,20);
		btn3.setBounds(280,200,80,20);
		frame.setContentPane(bj);
		frame.setLayout(null);
		frame.add(label1);
		frame.add(txt1);
		frame.add(label2);
		frame.add(txt2);
		frame.add(btn1);
		frame.add(btn2);
		frame.add(btn3);
		btn1.addMouseListener(this);
		btn2.addMouseListener(this);
		btn3.addMouseListener(this);
		frame.setVisible(true);
	}

	public void mouseClicked(MouseEvent arg0) {
	   text1=txt1.getText();
	   text2=txt2.getText();
	   if(distinguish==1){
	   if(myOpr.selectName(text1)){
		   if(myOpr.selectPassword(text2)){
			   JOptionPane.showMessageDialog(null, "登录成功","提示",2);
				txt1.setText("");
				txt2.setText("");
				distinguish=4;
				frame.setVisible(false);
		   }else{
				JOptionPane.showMessageDialog(null, "密码错误","提示",2);
				txt2.setText("");
				myOpr.setNumber1();
				myOpr.setNumber2();
		   }
	   }else{
		   JOptionPane.showMessageDialog(null, "此id不存在,请注册","提示",2);
			txt1.setText("");
			txt2.setText("");
	   }
	   }
	   if(distinguish==2){

		   String logi=(String) JOptionPane.showInputDialog(null,"请输入你的id:\n","注册",JOptionPane.PLAIN_MESSAGE,null,null,"在这输入");
		   String pas=(String) JOptionPane.showInputDialog(null,"请输入你的密码:\n","注册",JOptionPane.PLAIN_MESSAGE,null,null,"在这输入");
			myOpr.insertData(logi,pas,0);
			JOptionPane.showMessageDialog(null, "注册成功","提示",2);
	   }
	   if(distinguish==3){
		   int n = JOptionPane.showConfirmDialog(null, "是否退出?", "游戏结束",JOptionPane.YES_NO_OPTION);
		   myDB.closeMyConnection();
			if(n==JOptionPane.YES_OPTION){
			System.exit(1);
			}
	   }

	}

	public void mouseEntered(MouseEvent arg0) {
		if (arg0.getSource() == btn1) {
			distinguish=1;
			btn1.setForeground(Color.red);
			btn1.setBorder(BorderFactory.createLoweredBevelBorder());
			btn2.setForeground(Color.gray);
			btn2.setBorder(BorderFactory.createRaisedBevelBorder());
			btn3.setForeground(Color.gray);
			btn3.setBorder(BorderFactory.createRaisedBevelBorder());
		}
		if (arg0.getSource() == btn2) {
			distinguish=2;
			btn1.setForeground(Color.gray);
			btn1.setBorder(BorderFactory.createRaisedBevelBorder());
			btn2.setForeground(Color.red);
			btn2.setBorder(BorderFactory.createLoweredBevelBorder());
			btn3.setForeground(Color.gray);
			btn3.setBorder(BorderFactory.createRaisedBevelBorder());
		}
		if (arg0.getSource() == btn3) {
			distinguish=3;
			btn1.setForeground(Color.gray);
			btn1.setBorder(BorderFactory.createRaisedBevelBorder());
			btn2.setForeground(Color.gray);
			btn2.setBorder(BorderFactory.createRaisedBevelBorder());
			btn3.setForeground(Color.red);
			btn3.setBorder(BorderFactory.createLoweredBevelBorder());
			}

	}
	public void mouseExited(MouseEvent arg0) {
		distinguish=0;
		label1.setForeground(Color.gray);
		label2.setForeground(Color.gray);
		txt1.setOpaque(false);
		txt2.setOpaque(false);
		btn1.setContentAreaFilled(false);
	    btn1.setForeground(Color.gray);
	    btn1.setBorder(BorderFactory.createRaisedBevelBorder());
	    btn2.setContentAreaFilled(false);
		btn2.setBorder(BorderFactory.createRaisedBevelBorder());
	    btn2.setForeground(Color.gray);
	    btn3.setContentAreaFilled(false);
		btn3.setBorder(BorderFactory.createRaisedBevelBorder());
	    btn3.setForeground(Color.gray);
	}

	public void mousePressed(MouseEvent arg0) {

	}
	public void mouseReleased(MouseEvent arg0) {


	}
	public String getText1(){
		return text1;
	}
	public String getText2(){
		return text2;
	}
	public int getDistinguish(){
		return distinguish;
	}

}


源码文件连接https://download.csdn.net/download/qq_44257240/11317098mysql