当前位置:网站首页>后台登录系统,JDBC连接数据库,做小案例练习
后台登录系统,JDBC连接数据库,做小案例练习
2022-07-06 09:24:00 【MSjan】
巩固自己的基础,以后学习框架学起来也可以称心应手一些,案例很简单,重要的是巩固自己的学习。
作为第一个类,把他们联系起来
package Web.JDBClogin;
import java.util.Scanner;
public class procedure {
public void gui() throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("应用程序");
System.out.println("1.登录账号");
System.out.println("2.注册账号");
System.out.println("3.退出");
String serialnumber = scanner.nextLine();
switch (serialnumber) {
case "1":
new Login().login();
break;
case "2":
new Enroll().enroll();
break;
case "3":
System.out.println("退出成功 祝你 生活愉快!~ ");
break;
}
}
}
通过switch来实现登录方法,注册方法也同路理
package Web.JDBClogin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
/**
* 登录
*/
public class Login {
public void login() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入账号:");
String userName = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
Connection connection1 = JdbcConnection.getCon();
String sql = "select * from tb_user where username=? and password=?";
PreparedStatement pst = connection1.prepareStatement(sql);
pst.setString(1, userName);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
System.out.println("恭喜" + userName + "登录成功");
} else {
System.out.println("账号或密码错误或不存在");
}
}
}
注册功能实现
package Web.JDBClogin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;
/**
* 注册
*/
public class Enroll {
public void enroll() throws Exception {
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入注册账号");
String Raa = sc1.nextLine();
System.out.println("请输入注册密码");
String Rp = sc1.nextLine();
Connection connection = JdbcConnection.getCon();
String sql = "insert into tb_user (username,password) values ( ? ,? )";
//执行sql
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, Raa);
pst.setString(2, Rp);
int exdate = pst.executeUpdate();
if (exdate != 0) {
System.out.println("注册成功");
System.out.println("重新登录.....");
new Login().login(); //注册成功 会再一次进行登录 代码完善
} else {
System.out.println("注册失败");
}
}
}
数据库连接
URL,username,password,手动填写,forName反射连接
package Web.JDBClogin;
import java.sql.*;
/**
* 数据库连接
*/
public class JdbcConnection {
private static final String url =
private static final String username = ;
private static final String password = ;
private static final String jdbcName = "com.mysql.jdbc.Driver";
static {
try {
Class.forName(jdbcName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon() {
Connection con;//作用域
try {
con = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return con;
}
public static void close(ResultSet rs, Statement st, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
main方法
procedure procedure = new procedure();
procedure.gui();

简单的登录系统案例就做完了。
点个赞呗。
边栏推荐
- 【指针】求字符串的长度
- Numpy Quick Start Guide
- Feature extraction and detection 14 plane object recognition
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- The most popular colloquial system explains the base of numbers
- Internet Management (Information Collection)
- 链队实现(C语言)
- 内网渗透之内网信息收集(四)
- Fundamentals of digital circuit (V) arithmetic operation circuit
- . Net6: develop modern 3D industrial software based on WPF (2)
猜你喜欢

Hackmyvm target series (2) -warrior

关于超星脚本出现乱码问题

内网渗透之内网信息收集(一)

MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?

《统计学》第八版贾俊平第三章课后习题及答案总结

High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters

图书管理系统

Only 40% of the articles are original? Here comes the modification method

《统计学》第八版贾俊平第二章课后习题及答案总结

Build domain environment (win)
随机推荐
Interview Essentials: what is the mysterious framework asking?
Markdown font color editing teaching
[issue 18] share a Netease go experience
【指针】数组逆序重新存放后并输出
flask实现强制登陆
JDBC transactions, batch processing, and connection pooling (super detailed)
How does SQLite count the data that meets another condition under the data that has been classified once
2022华中杯数学建模思路
“Hello IC World”
Always of SystemVerilog usage_ comb 、always_ iff
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
Intranet information collection of Intranet penetration (4)
Matplotlib绘图快速入门
Hcip -- MPLS experiment
XSS之冷门事件
【指针】使用插入排序法将n个数从小到大进行排列
数字电路基础(一)数制与码制
【指针】删除字符串s中的所有空格
内网渗透之内网信息收集(一)
Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques