java用户登录界面_java web用户登录界面

做这次实验,主要用到了mysql  java web 的 内容

实验代码:

IUserDao.java

package com.jaovo.msg.dao;

import java.util.List;

import com.jaovo.msg.model.User;

public interface IUserDao {

public void add(User user);

public void delete(int id);

public void update(User user);

public User load(int id);

public User load(String username);

public List load();

}

UserDaoImpl.java

package com.jaovo.msg.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import com.jaovo.msg.Util.DBUtil;

import com.jaovo.msg.Util.UserException;

import com.jaovo.msg.model.User;

import sun.net.www.content.text.plain;

public class UserDaoImpl implements IUserDao{

@Override

public void add(User user) {

//获得链接对象

Connection connection = DBUtil.getConnection();

//准备sql语句

String sql = "select * from example where username = ?";

//创建语句传输对象

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, user.getUsername());

//接收结果集

resultSet = preparedStatement.executeQuery();

//遍历结果集

while(resultSet.next()) {

if (resultSet.getInt(1) > 0) {

throw new UserException("用户已存在") ;

}

}

sql = "insert into example(username,password) value (?,?)";

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, user.getUsername());

preparedStatement.setString(2, user.getPassword());

preparedStatement.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//关闭资源

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

}

@Override

public void delete(int id){

Connection connection = DBUtil.getConnection();

String sql = "delete from example where id = ?";

PreparedStatement preparedStatement = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setInt(1, id);

preparedStatement.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

}

@Override

public void update(User user) {

Connection connection = DBUtil.getConnection();

//准备sql语句

String sql = "update example set password = ? , nickname=? where id = ?";

//创建语句传输对象

PreparedStatement preparedStatement = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, user.getPassword());

preparedStatement.setInt(3, user.getId());

preparedStatement.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

}

public User load1(int id) {

Connection connection = DBUtil.getConnection();

//准备sql语句

String sql = "select * from example where id = ?";

//创建语句传输对象

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

User user = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setInt(1, id);

resultSet = preparedStatement.executeQuery();

while(resultSet.next()) {

user = new User();

user.setId(id);

user.setUsername(resultSet.getString("username"));

user.setPassword(resultSet.getString("password"));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

return user;

}

public User load1(String username){

// TODO Auto-generated method stub

Connection connection = DBUtil.getConnection();

//准备sql语句

String sql = "select * from example where username = ?";

//创建语句传输对象

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

User user = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, username);

resultSet = preparedStatement.executeQuery();

while(resultSet.next()) {

user = new User();

user.setUsername(username);

user.setId(resultSet.getInt("username"));

user.setPassword(resultSet.getString("password"));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

return user;

}

public List load(){

Connection connection = DBUtil.getConnection();

//准备sql语句

String sql = "select * from example ";

//创建语句传输对象

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

//集合中只能放入user对象

List users = new ArrayList();

User user = null;

try {

preparedStatement = connection.prepareStatement(sql);

resultSet = preparedStatement.executeQuery();

while(resultSet.next()) {

user = new User();

user.setId(resultSet.getInt("id"));

user.setUsername(resultSet.getString("username"));

user.setPassword(resultSet.getString("password"));

users.add(user);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

return users;

}

@Override

public void add(User user) {

// TODO Auto-generated method stub

}

@Override

public void update(User user) {

// TODO Auto-generated method stub

}

@Override

public User load(int id) {

// TODO Auto-generated method stub

return null;

}

@Override

public User load(String username) {

// TODO Auto-generated method stub

return null;

}

}

User.java

package com.jaovo.msg.model;

public class User {

private int id;

private String username;

private String password;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

DBUtil.java

package com.jaovo.msg.Util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class DBUtil {

public static Connection getConnection() {

try {

//1 加载驱动

Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String user = "root";

String password = "582607kou";

String url = "jdbc:mysql://localhost:33060/mysql";

Connection connection = null;

try {

//2 创建链接对象connection

connection = DriverManager.getConnection(url,user,password);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return connection;

}

//关闭资源的方法

public static void close(Connection connection ) {

try {

if (connection != null) {

connection.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void close(PreparedStatement preparedStatement ) {

try {

if (preparedStatement != null) {

preparedStatement.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void close(ResultSet resultSet ) {

try {

if (resultSet != null) {

resultSet.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

UserException.java

package com.jaovo.msg.Util;

public class UserException extends RuntimeException{

public UserException() {

super();

// TODO Auto-generated constructor stub

}

public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {

super(message, cause, enableSuppression, writableStackTrace);

// TODO Auto-generated constructor stub

}

public UserException(String message, Throwable cause) {

super(message, cause);

// TODO Auto-generated constructor stub

}

public UserException(String message) {

super(message);

// TODO Auto-generated constructor stub

}

public UserException(Throwable cause) {

super(cause);

// TODO Auto-generated constructor stub

}

}

indexcheck.java

pageEncoding="utf-8"%>

>

>

Insert title here

request.setCharacterEncoding("UTF-8");

String username=request.getParameter("username");

String password=request.getParameter("password");//取出login.jsp的值

UserDaoImpl a=new UserDaoImpl();

User user=a.load(username);

String password1=user.getPassword();

if(password.equals(password1)){

response.sendRedirect("loginsuccess.jsp");

}

else{

out.print("");

response.setHeader("refresh", "0;url=login.jsp");

}

%>

login.java

pageEncoding="utf-8"%>

登录界面

登录

账号:
密码:

loginsuccess.java

pageEncoding="utf-8"%>

Insert title here

登陆成功

实验结果截图:

bd13a73956260f4907c63e5d87828ff7.png

56cd4fbd7853e16e6696e269a7424f4c.png

对自己空闲时间计划的太少,平时不能利用空闲时间来学习java web的内容,以后打算每天用4个小时的时间来学习,编程,周六日时间再加强!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值