当前位置:网站首页>后台登录系统,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();

简单的登录系统案例就做完了。
点个赞呗。
边栏推荐
- Realize applet payment function with applet cloud development (including source code)
- Record an edu, SQL injection practice
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- 链队实现(C语言)
- Overview of LNMP architecture and construction of related services
- 《统计学》第八版贾俊平第五章概率与概率分布
- Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
- Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
猜你喜欢

Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution

Detailed explanation of network foundation

《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案

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

《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案

JDBC事务、批处理以及连接池(超详细)

图书管理系统

数字电路基础(五)算术运算电路

Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator

SystemVerilog discusses loop loop structure and built-in loop variable I
随机推荐
[issue 18] share a Netease go experience
指针:最大值、最小值和平均值
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Lintcode logo queries the two nearest saplings
What language should I learn from zero foundation. Suggestions
Attack and defense world misc practice area (GIF lift table ext3)
Always of SystemVerilog usage_ comb 、always_ iff
《统计学》第八版贾俊平第一章课后习题及答案总结
线程的实现方式总结
Numpy快速上手指南
Feature extraction and detection 14 plane object recognition
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
List and data frame of R language experiment III
Matplotlib绘图快速入门
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。