当前位置:网站首页>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 .
边栏推荐
- 函数:用牛顿迭代法求方程的根
- Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
- Binary search tree concept
- 《统计学》第八版贾俊平第二章课后习题及答案总结
- [pointer] counts the number of times one string appears in another string
- Es full text index
- 【指针】删除字符串s中的所有空格
- [pointer] find the length of the string
- 链队实现(C语言)
- Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
猜你喜欢

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

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

1. Payment system

Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class

JDBC read this article is enough

Lintcode logo queries the two nearest saplings

ES全文索引

“Hello IC World”

Constants, variables, and operators of SystemVerilog usage

Intranet information collection of Intranet penetration (4)
随机推荐
How does SQLite count the data that meets another condition under the data that has been classified once
王爽汇编语言详细学习笔记二:寄存器
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
《统计学》第八版贾俊平第三章课后习题及答案总结
Intranet information collection of Intranet penetration (2)
Keil5-MDK的格式化代码工具及添加快捷方式
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
Sentinel overall workflow
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Hcip -- MPLS experiment
【指针】八进制转换为十进制
Always of SystemVerilog usage_ comb 、always_ iff
Intranet information collection of Intranet penetration (4)
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
[pointer] octal to decimal
[issue 18] share a Netease go experience
flask实现强制登陆
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
[pointer] find the length of the string
Captcha killer verification code identification plug-in