JDBC实现登录注册(控制台操作)
本博客是对JDBC的初步学习进行练习,目前还没有连接到WEB端,之后的话会进行更新…
下面这个博客是使用JDBC连接到数据库,如果是初步入门学者,建议通过下面这个博客后在进行本博客的操作
https://blog.csdn.net/m0_63324772/article/details/129287093?spm=1001.2014.3001.5501
博客内容如下:
所在包
Main.java
package jdbc_login_registration;
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws SQLException {
String username = null;
String password = null;
String select;
Scanner scanner = new Scanner(System.in);
Login login = new Login();
Registration registration = new Registration();
do {
System.out.println("""
-----------------
欢迎来到XX
(1)登录
(2)注册
(0)退出XX
-----------------
请选择:""");
select = scanner.nextLine();
switch (select) {
case "1" -> {
System.out.println("请输入用户名:");
username = scanner.nextLine();
System.out.println("请输入密码");
password = scanner.nextLine();
login.login(username,password);
}
case "2" -> {
System.out.println("请输入用户名:");
username = scanner.nextLine();
System.out.println("请输入密码");
password = scanner.nextLine();
registration.registration(username,password);
}
}
}while (!select.equals("0"));
}
}
Registration.java
package jdbc_login_registration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Registration {
public void registration(String username , String password) throws SQLException {
int count = 0;
Statement st = null;
Connection conn=null;
//Connection接口代表Java程序和数据库的连接对象,只有获得该连接对象后,才能访问数据库,并操作数据表
String driver="com.mysql.cj.jdbc.Driver";
//数据库驱动类所对应的字符串
String URL="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT";
//URL语法格式如下
//jdbc:mysql:是固定的写法,后面跟主机名localhost,3306是默认的MySQL端口号
//serverTimezone=UTC是指定时区时间为世界统一时间
//useUnicode=true是指是否使用Unicode字符集,赋值为true
//characterEncoding=utf-8是指定字符编码格式为UTF8
try {
Class.forName(driver);//加载MySQL数据库驱动
}catch(java.lang.ClassNotFoundException e) {
//如果找不到这个类,执行下面的异常处理
System.out.println("驱动程序配置未配置成功!!!");
}
try {
conn= DriverManager.getConnection(URL,"root","123456");//建立和数据库的连接,并返回表示连接的Connection对象
//System.out.println("数据库连接成功!!!");
st = conn.createStatement();
String sql="insert into admin2(username,password) values('"+username+"','"+password+"')";
count = st.executeUpdate(sql);
}catch(Exception e) {
//未连接成功,执行下面的异常处理
System.out.println("数据库连接失败!!!");
}
finally {
if (st!=null)
st.close();
if (conn!=null)
conn.close();
}
if (count==1)
System.out.println("注册成功");
else
System.out.println("注册失败");
}
}
Login.java
package jdbc_login_registration;
import java.sql.*;
public class Login {
public void login(String username , String password) throws SQLException {
PreparedStatement ps = null;
Statement statement = null;
Connection conn=null;
ResultSet resultSet = null;
boolean flag = false;
//Connection接口代表Java程序和数据库的连接对象,只有获得该连接对象后,才能访问数据库,并操作数据表
String driver="com.mysql.cj.jdbc.Driver";
//数据库驱动类所对应的字符串
String URL="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT";
//URL语法格式如下
//jdbc:mysql:是固定的写法,后面跟主机名localhost,3306是默认的MySQL端口号
//serverTimezone=UTC是指定时区时间为世界统一时间
//useUnicode=true是指是否使用Unicode字符集,赋值为true
//characterEncoding=utf-8是指定字符编码格式为UTF8
try {
Class.forName(driver);//加载MySQL数据库驱动
}catch(java.lang.ClassNotFoundException e) {
//如果找不到这个类,执行下面的异常处理
System.out.println("驱动程序配置未配置成功!!!");
}
try {
conn= DriverManager.getConnection(URL,"root","123456");//建立和数据库的连接,并返回表示连接的Connection对象
//System.out.println("数据库连接成功!!!");
String sql="SELECT username FROM admin2 WHERE username = ? AND password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
resultSet = ps.executeQuery();
if (resultSet.next())
flag = true;
}catch(Exception e) {
//未连接成功,执行下面的异常处理
System.out.println("数据库连接失败!!!");
}
finally {
if (resultSet!=null)
resultSet.close();
if (ps!=null)
ps.close();
if (conn!=null)
conn.close();
}
if (flag)
System.out.println("登录成功");
else
System.out.println("您输入的账号密码错误");
}
}
测试如下: