Copy the code code as follows:
//User user's basic information is also three columns in the USERINFO table
package login judgment;
public class User {
private String name;
private String loginname;
private String loginpsw;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getLoginpsw() {
return loginpsw;
}
public void setLoginpsw(String loginpsw) {
this.loginpsw = loginpsw;
}
}
//Small code block for registration and login
package login judgment;
import java.util.Scanner;
public class In {
public static User getUser(){
User u = new User();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your login name");
u.setLoginname(sc.nextLine());
System.out.println("Please enter your password");
u.setLoginpsw(sc.nextLine());
return u;
}
public static User registerUser(){
User u = new User();
Scanner sc = new Scanner(System.in);
System.out.println("Please register user name");
u.setName(sc.nextLine());
System.out.println("Please register your login name");
u.setLoginname(sc.nextLine());
System.out.println("Please register a password");
u.setLoginpsw(sc.nextLine());
return u;
}
}
//Check when logging in to determine whether the login name and password are correct. If correct, return the user name.
package login judgment;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Check {
public static void checkUser(User u)
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
String sql = "Select name from USERINFO where loginname = ? and loginpsw = ? ";
try {
conn = DriverManager.getConnection(url, user, password);
pst = conn.prepareStatement(sql);
pst.setString(1, u.getLoginname());
pst.setString(2,u.getLoginpsw());
rs = pst.executeQuery();
while(rs.next()){
u.setName(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(u.getName() == null || u.getName().isEmpty()){
System.out.println("Login failed");
}else{
System.out.println("Welcome"+u.getName()+"Login");
}
}
}
//In the registration interface, it is necessary to determine whether the login name is duplicated. If there are duplicates, the registration will fail. I have a little question, I have marked it with comments.
package login judgment;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Register {
public static void registerUser(User u){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
Connection conn = null;
String sql = "Insert Into USERINFO values(?,?,?)";
PreparedStatement pst = null;
//Determine whether the login name already exists
Statement s = null;
ResultSet rs = null;
String sql_ck = "Select LOGINNAME FROM USERINFO";
try {
conn = DriverManager.getConnection(url, user, password);
pst = conn.prepareStatement(sql);
pst.setString(1, u.getName());
pst.setString(2, u.getLoginname());
pst.setString(3, u.getLoginpsw());
//Determine whether the login name already exists
s = conn.createStatement();
rs = s.executeQuery(sql_ck);
while(rs.next()){//Why does it prompt an invalid index when using rs.getString(2)?
if( rs.getString("LOGINNAME").equals(u.getLoginname()) ){
System.out.println("Login name already exists, registration failed");
break;
}else{
pst.executeUpdate();
System.out.println("Registration successful");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
pst.close();
s.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//main interface//
//The structure of the table in the database is like this
//name (user name, used for display),
//loginname (login name, the ID entered when logging in)
//loginpsw (password entered when logging in)
package login judgment;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
intchos = 0;
Scanner sc = new Scanner(System.in);
System.out.println("/t/tLogin please press 1/t/tRegistration please press 2");
chos = sc.nextInt();
switch(chos){
case 1: Check.checkUser(In.getUser());break;
case 2: Register.registUser(In.registerUser());break;
default: System.out.println("Please enter the correct choice");break;
}
}
}