当前位置:网站首页>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 .
边栏推荐
- How does SQLite count the data that meets another condition under the data that has been classified once
- 数字电路基础(四) 数据分配器、数据选择器和数值比较器
- [issue 18] share a Netease go experience
- 函数:求两个正数的最大公约数和最小公倍
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- 【指针】求二维数组中最大元素的值
- 《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
- [pointer] the array is stored in reverse order and output
- 指針:最大值、最小值和平均值
- Intel oneapi - opening a new era of heterogeneity
猜你喜欢
![Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)](/img/85/d6b196f22b60ad5003f73eb8d8a908.png)
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)

循环队列(C语言)

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

Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class

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

Data mining - a discussion on sample imbalance in classification problems

《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案

Intranet information collection of Intranet penetration (4)

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

Windows platform mongodb database installation
随机推荐
MySQL learning notes (stage 1)
【指针】求解最后留下的人
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Numpy快速上手指南
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
《统计学》第八版贾俊平第三章课后习题及答案总结
MySQL interview questions (4)
Sentinel overall workflow
Markdown font color editing teaching
Es full text index
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
【指针】查找最大的字符串
Function: string storage in reverse order
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
The difference between layer 3 switch and router
How to earn the first pot of gold in CSDN (we are all creators)
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
循环队列(C语言)
5分钟掌握机器学习鸢尾花逻辑回归分类
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。