JAVA簡略鏈接Oracle數據庫 注冊和上岸功效的完成代碼。本站提示廣大學習愛好者:(JAVA簡略鏈接Oracle數據庫 注冊和上岸功效的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA簡略鏈接Oracle數據庫 注冊和上岸功效的完成代碼正文
//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;
}
}
}