首页
学习
活动
专区
工具
TVP
发布

用数据库编写简单登录注册源代码

//创键User类存储用户登录信息

public class User {

private String name;

private String password;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

//对jdbc进行封装

public class DBUtils {

private static final String driver="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/admin";

private static final String user="root";

private static final String password="root";

static{

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public static Connection getConn() throws SQLException{

return DriverManager.getConnection(url, user, password);

}

public static void closeAll(ResultSet rs,PreparedStatement pst,Connection conn){

if(rs!=null){

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(pst!=null){

try {

pst.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(conn!=null){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

//注册登录

package com.jh.utils;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Random;

import java.util.Scanner;

import com.jh.bean.User;

public class Register {

public void showMainMenu(){

System.out.println("\t欢迎来到**********平台");

System.out.println("\t\t1.在线注册");

System.out.println("\t\t2.登录");

System.out.println("\t\t3.退出");

System.out.println("请选择:");

}

@SuppressWarnings("resource")

public void showRegister(){

Scanner sc=new Scanner(System.in);

Random r=new Random();

boolean b1=true;

do {

System.out.println("\t\t欢迎来到在线注册平台");

System.out.println("请输入用户名:");

String userName=sc.next();

System.out.println("请输入密码:");

String password=sc.next();

System.out.println("请选择验证码获取方式:1.生成到桌面文档验证码.txt 2.直接在界面显示");

int i=sc.nextInt();

String s="";

if(i==1){

File file=new File("C:\\Users\\Administrator\\Desktop\\验证码.txt");

FileOutputStream out=null;

try {

out=new FileOutputStream(file);

s=String.valueOf(r.nextInt(9000)+1000);

byte[]b=s.getBytes();

out.write(b);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(out!=null){

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}else if(i==2){

s=String.valueOf(r.nextInt(9000)+1000);

System.out.println("验证码:"+s);

}

System.out.println("请输入验证码:");

String yan=sc.next();

if(yan.equals(s)){

Connection conn=null;

PreparedStatement pst=null;

try {

conn=DBUtils.getConn();

String sql="insert into users values(?,?)";

pst=conn.prepareStatement(sql);

pst.setString(1, userName);

pst.setString(2, password);

pst.executeUpdate();

System.out.println("注册成功");

showLogin();

b1=false;

} catch (SQLException e) {

e.printStackTrace();

} finally {

DBUtils.closeAll(null, pst, conn);

}

}else{

System.out.println("注册失败");

}

} while (b1);

}

@SuppressWarnings("resource")

public void showLogin(){

Scanner sc=new Scanner(System.in);

boolean b=true;

do {

System.out.println("\t\t欢迎来到登录平台");

System.out.println("请输入用户名:");

String username=sc.next();

System.out.println("请输入密码:");

String password=sc.next();

Connection conn=null;

PreparedStatement pst=null;

try {

conn=DBUtils.getConn();

String sql="select * from users where name=? and password=?";

pst=conn.prepareStatement(sql);

pst.setString(1, username);

pst.setString(2, password);

ResultSet rs=pst.executeQuery();

User user=null;

while(rs.next()){

user=new User();

user.setName(rs.getString("name"));

user.setPassword(rs.getString("password"));

}

if(user!=null){

System.out.println("登陆成功");

b=false;

}else{

System.out.println("登录失败");

}

} catch (SQLException e) {

e.printStackTrace();

} finally{

DBUtils.closeAll(null, pst, conn);

}

} while (b);

}

}

//测试类

package com.jh.test;

import java.util.Scanner;

import com.jh.utils.Register;

public class Test {

@SuppressWarnings("resource")

public static void main(String[] args) {

Register r=new Register();

Scanner sc=new Scanner(System.in);

r.showMainMenu();

int i=sc.nextInt();

boolean b=true;

do {

switch (i) {

case 1:

r.showRegister();

b=false;

break;

case 2:

r.showLogin();

b=false;

break;

case 3:

System.out.println("谢谢使用,欢迎下次再次光临!");

b=false;

break;

default:

System.out.println("输入错误,请重新输入!");

break;

}

} while (b);

}

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181208A05KGW00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券