複製代碼代碼如下:
//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;
//判斷登陸名是否已經存在
州 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;
}
}
}