当前位置:网站首页>Login the system in the background, connect the database with JDBC, and do small case exercises
Login the system in the background, connect the database with JDBC, and do small case exercises
2022-07-06 14:41:00 【MSjan】
Consolidate your foundation , In the future, the learning framework can also be handy , The case is very simple , The important thing is to consolidate your study .
As the first class , Connect them
package Web.JDBClogin;
import java.util.Scanner;
public class procedure {
public void gui() throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println(" Applications ");
System.out.println("1. Login account ");
System.out.println("2. Registered account ");
System.out.println("3. sign out ");
String serialnumber = scanner.nextLine();
switch (serialnumber) {
case "1":
new Login().login();
break;
case "2":
new Enroll().enroll();
break;
case "3":
System.out.println(" Quit successfully I wish you Life is happy !~ ");
break;
}
}
}
adopt switch To implement the login method , The registration method is the same
package Web.JDBClogin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
/**
* Sign in
*/
public class Login {
public void login() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter your account number :");
String userName = sc.nextLine();
System.out.println(" Please input a password :");
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(" Congratulations " + userName + " Login successful ");
} else {
System.out.println(" The account or password is wrong or does not exist ");
}
}
}
Registration function
package Web.JDBClogin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;
/**
* register
*/
public class Enroll {
public void enroll() throws Exception {
Scanner sc1 = new Scanner(System.in);
System.out.println(" Please enter your account number ");
String Raa = sc1.nextLine();
System.out.println(" Please enter the registration password ");
String Rp = sc1.nextLine();
Connection connection = JdbcConnection.getCon();
String sql = "insert into tb_user (username,password) values ( ? ,? )";
// perform sql
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, Raa);
pst.setString(2, Rp);
int exdate = pst.executeUpdate();
if (exdate != 0) {
System.out.println(" Registered successfully ");
System.out.println(" Log back in .....");
new Login().login(); // Registered successfully Will log in again Code perfect
} else {
System.out.println(" Registration failed ");
}
}
}
Database connection
URL,username,password, Fill in by hand ,forName Reflection connection
package Web.JDBClogin;
import java.sql.*;
/**
* Database connection
*/
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;// Scope
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 Method
procedure procedure = new procedure();
procedure.gui();
The simple login system case is finished .
A great bai .
边栏推荐
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
- [pointer] counts the number of times one string appears in another string
- c语言学习总结(上)(更新中)
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- Web vulnerability - File Inclusion Vulnerability of file operation
- 内网渗透之内网信息收集(二)
- Based on authorized access, cross host, and permission allocation under sqlserver
- 内网渗透之内网信息收集(三)
- 数据库多表链接的查询方式
猜你喜欢
王爽汇编语言详细学习笔记二:寄存器
后台登录系统,JDBC连接数据库,做小案例练习
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
Captcha killer verification code identification plug-in
JVM memory model concept
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
“人生若只如初见”——RISC-V
Wu Enda's latest interview! Data centric reasons
Proceedingjoinpoint API use
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
随机推荐
《统计学》第八版贾俊平第一章课后习题及答案总结
[pointer] find the largest string
Intranet information collection of Intranet penetration (4)
Pointer -- output all characters in the string in reverse order
Lintcode logo queries the two nearest saplings
王爽汇编语言详细学习笔记二:寄存器
This article explains in detail how mockmvc is used in practical work
函数:计算字符串中大写字母的个数
《统计学》第八版贾俊平第四章总结及课后习题答案
Proceedingjoinpoint API use
The difference between layer 3 switch and router
Overview of LNMP architecture and construction of related services
New version of postman flows [introductory teaching chapter 01 send request]
【指针】使用插入排序法将n个数从小到大进行排列
《统计学》第八版贾俊平第三章课后习题及答案总结
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
Numpy Quick Start Guide
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
[pointer] find the value of the largest element in the two-dimensional array
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案