JAVA简单链接Oracle数据库 注册和登陆功能的实现代码
时间:2024-01-23 13:15:12
//User 用户的基本信息,也是USERINFO表中的3个列
package 登陆判断;
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;
}
}
//用于注册和登陆的小小代码块
package 登陆判断;
import java.util.Scanner;
public class In {
public static User getUser(){
User u = new User();
Scanner sc = new Scanner(System.in);
System.out.println("请输入登陆名");
u.setLoginname(sc.nextLine());
System.out.println("请输入密码");
u.setLoginpsw(sc.nextLine());
return u;
}
public static User registerUser(){
User u = new User();
Scanner sc = new Scanner(System.in);
System.out.println("请注册用户名名");
u.setName(sc.nextLine());
System.out.println("请注册登陆名");
u.setLoginname(sc.nextLine());
System.out.println("请注册密码");
u.setLoginpsw(sc.nextLine());
return u;
}
}
//登陆时的检查,判断登陆名和密码是否正确,正确则返回 用户名
package 登陆判断;
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("登陆失败");
}else{
System.out.println("欢迎"+u.getName()+"登陆");
}
}
}
//注册界面,要判断 登陆名是否有重复,有重复则注册失败。有点小疑问,已用注释标出来
package 登陆判断;
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 registUser(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;
//判断登陆名是否已经存在
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());
//判断登陆名是否已经存在
s = conn.createStatement();
rs = s.executeQuery(sql_ck);
while(rs.next()){//为什么用 rs.getString(2)会提示 无效的索引呢
if( rs.getString("LOGINNAME").equals(u.getLoginname()) ){
System.out.println("登陆名已经存在,注册失败");
break;
}else{
pst.executeUpdate();
System.out.println("注册成功");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
pst.close();
s.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//main界面//
//数据库中表的结构式这样
//name(用户名,用于显示),
//loginname(登录名,即登陆时输入的ID)
//loginpsw(登陆时输入的密码)
package 登陆判断;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int chos = 0;
Scanner sc = new Scanner(System.in);
System.out.println("\t\t登陆请按1\t\t注册请按2");
chos = sc.nextInt();
switch(chos){
case 1: Check.checkUser(In.getUser());break;
case 2: Register.registUser(In.registerUser());break;
default: System.out.println("请输入正确的选择");break;
}
}
}