做这次实验,主要用到了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 hererequest.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登陆成功
实验结果截图:
对自己空闲时间计划的太少,平时不能利用空闲时间来学习java web的内容,以后打算每天用4个小时的时间来学习,编程,周六日时间再加强!