求用java实现简单的登录和注册功能,主要是为了了解怎样对数据库进行
求用java实现简单的登录和注册功能,主要是为了了解怎样对数据库进行增删改查。和各种请求处理,大部分代码看得懂,自己对流程不是太熟悉。知道要先写bean然后写jdbc连接...
求用java实现简单的登录和注册功能,主要是为了了解怎样对数据库进行增删改查。和各种请求处理,大部分代码看得懂,自己对流程不是太熟悉。知道要先写bean然后写jdbc连接数据库的类,然后在dao里头写注册和登录的方法。jsp页面用el表达式。但是各种传参值不是太懂。还望大牛给个详细的案例。尽量简单不要复杂,可以帮助理清关系的。用java servlet mysql 邮箱:fy787476452@qq.com十分感谢
展开
1个回答
展开全部
//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;
}
}
}
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;
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询