//User用户的基本信息,也是USERINFO表中的3个列
package
登陆判断;
publicclassUser{
privateStringname;
privateStringloginname;
privateString
loginpsw;
publicStringgetName(){
returnname;
}
public
voidsetName(Stringname){
this.name=name;
}
publicString
getLoginname(){
returnloginname;
}
publicvoid
setLoginname(Stringloginname){
this.loginname=
loginname;
}
publicStringgetLoginpsw(){
return
loginpsw;
}
publicvoidsetLoginpsw(Stringloginpsw)
{
this.loginpsw=loginpsw;
}
}
//用于注册和登陆的小小代码块
package
登陆判断;
importjava.util.Scanner;
publicclassIn{
publicstaticUsergetUser(){
Useru=newUser();
Scannersc=
new
Scanner(System.in);
System.out.println("请输入登陆名");
u.setLoginname(sc.nextLine());
System.out.println("请输入密码");
u.setLoginpsw(sc.nextLine());
return
u;
}
publicstaticUserregisterUser(){
Useru=new
User();
Scannersc=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登陆判断;
importjava.sql.Connection;
importjava.sql.DriverManager;
import
java.sql.PreparedStatement;
importjava.sql.ResultSet;
import
java.sql.SQLException;
publicclassCheck{
publicstaticvoidcheckUser(Useru)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch
(ClassNotFoundExceptione){
e.printStackTrace();
}
String
url="jdbc:oracle:thin:@localhost:1521:orcl";
Stringuser=
"scott";
Stringpassword="tiger";
Connectionconn=
null;
ResultSetrs=null;
PreparedStatementpst=
null;
Stringsql="SelectnamefromUSERINFOwhereloginname=?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(SQLExceptione){
e.printStackTrace();
}finally{
try
{
rs.close();
pst.close();
conn.close();
}catch
(SQLExceptione)
{
e.printStackTrace();
}
}
if(u.getName()==null||
u.getName().isEmpty()){
System.out.println("登陆失败");
}else{
System.out.println("欢迎"+u.getName()+"登陆");
}
}
}
//注册界面,要判断登陆名是否有重复,有重复则注册失败。有点小疑问,已用注释标出来
package登陆判断;
importjava.sql.Connection;
importjava.sql.DriverManager;
import
java.sql.PreparedStatement;
importjava.sql.ResultSet;
import
java.sql.SQLException;
importjava.sql.Statement;
publicclassRegister{
publicstaticvoidregistUser(Useru){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch
(ClassNotFoundExceptione){
e.printStackTrace();
}
String
url="jdbc:oracle:thin:@localhost:1521:orcl";
Stringuser=
"scott";
Stringpassword="tiger";
Connectionconn=
null;
Stringsql="InsertIntoUSERINFO
values(?,?,?)";
PreparedStatementpst=
null;
//判断登陆名是否已经存在
Statements=null;
ResultSetrs=
null;
Stringsql_ck="SelectLOGINNAMEFROMUSERINFO";
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(SQLExceptione){
e.printStackTrace();
}finally{
try
{
rs.close();
pst.close();
s.close();
conn.close();
}
catch(SQLExceptione)
{
e.printStackTrace();
}
}
}
}
//main界面//
//数据库中表的结构式这样
//name(用户名,用于显示),
//loginname(登录名,即登陆时输入的ID)
//loginpsw(登陆时输入的密码)
package
登陆判断;
importjava.util.Scanner;
publicclassTest{
publicstaticvoidmain(String[]args){
intchos=0;
Scannersc=new
Scanner(System.in);
System.out.println("\t\t登陆请按1\t\t注册请按2");
chos=
sc.nextInt();
switch(chos){
case1:
Check.checkUser(In.getUser());break;
case2:
Register.registUser(In.registerUser());break;
default:
System.out.println("请输入正确的选择");break;
}
}
}