• 课堂练习1


    源代码

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    	<title>用户添加页面</title>
    <script type="text/javascript">
    function check(form) {
    	teacher = form.password.value;
    	didian = form.nickname.value;
    	if(form.username.value == ""){
    		alert("课程名称不能为空");
    		return false;
    	}
    	if(form.password.value == ""){
    		alert("任课教师不能为空");
    		return false;
    	}
    	if((teacher!= "王建民")&&(teacher!= "刘立嘉")&&(teacher!= "刘丹")&&(teacher!= "王辉")&&(teacher!= "杨子光")){
    		alert("任课教师错误");
    		return false;
    	}
    	if(form.nickname.value == ""){
    		alert("教学地点不能为空");
    		return false;
    	}
    	/*if((didian != "基教")&&(didian != "基教405")){
    		alert("教学地点错误");
    		return false;
    	}*/
    	if((didian.indexOf("基教")!=0)&&(didian.indexOf("一教")!=0)&&(didian.indexOf("二教")!=0)&&(didian.indexOf("三教")!=0)){
    		alert("教学地点错误");
    		return false;
    	}
    	return true;
    }
    
    </script>
    </head>
    <body>
    	<%request.setCharacterEncoding("UTF-8");
    	response.setCharacterEncoding("UTF-8");
    	%>
    	<jsp:include page="inc.jsp">
    	<jsp:param  name="every" value="添加"  />
    	</jsp:include>
    
    	<form name = "form1" action = "add.jsp" method = "post" onsubmit = "return check(form1)" >
    		<table align="center" border="1" width="500">
    			<tr>
    				<td>课程名称 : </td>
    				<td>
    					<input type="text" name="username" />
    				</td>
    			</tr>
    				<tr>
        			<td>任课教师:</td>
        			<td>
        				<input type="text" name="password" />
        			</td>
        		</tr>
        		<tr>
        			<td>教学地点:</td>
        			<td>
        				<input type="text" name="nickname" />
        			</td>
        			
        		</tr>
        		<tr align="center">
        			<td colspan="2">
        				<input type="submit" value="提交" />
        				<input type="reset" value="重置" />
        			</td>
        		</tr>
    		</table>
    	</form>
    </body>
    </html>
    

      连接数据库

    package com.jaovo.msg.dao;
    
    import java.sql.*;
    import java.util.*;
    
    
    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;
    
    
    
    public class UserDaoImpl implements IUserDao{
    	@Override
    	public void add(User user) {
    		//获得链接对象
    		Connection connection = DBUtil.getConnection();
    		//准备sql语句
    		String sql = "select count(*) from usertable 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 usertable(username,password,nickname) values(?,?,?)";
    			preparedStatement = connection.prepareStatement(sql);
    			preparedStatement.setString(1, user.getUsername());
    			preparedStatement.setString(2, user.getPassword());
    			preparedStatement.setString(3, user.getNickname());
    			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) {
    		// TODO Auto-generated method stub
    		Connection connection = DBUtil.getConnection();
    		String sql = "delete from usertable 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) {
    		// TODO Auto-generated method stub
    		Connection connection = DBUtil.getConnection();
    		String sql = "update usertable set password = ?,nickname = ? where id = ?";
    		PreparedStatement preparedStatement =null;
    		try {
    			preparedStatement = connection.prepareStatement(sql);
    			preparedStatement.setString(1, user.getPassword());
    			preparedStatement.setString(2, user.getNickname());
    			preparedStatement.setInt(3, user.getId());
    			preparedStatement.executeUpdate();
    		}catch (SQLException e ){
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(preparedStatement);
    			DBUtil.close(connection);
    		}
    		
    	}
    
    	@Override
    	public User load(int id) {
    		Connection connection = DBUtil.getConnection();
    		String sql = "select * from usertable 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"));
    				user.setNickname(resultSet.getString("nickname"));
    			}
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(resultSet);
    			DBUtil.close(preparedStatement);
    			DBUtil.close(connection);
    		}
    		// TODO Auto-generated method stub
    		return user;
    	}
    
    	@Override
    	public User load(String username) {
    		// TODO Auto-generated method stub
    		
    		Connection connection = DBUtil.getConnection();
    		String sql = "select * from usertable 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.setId(resultSet.getInt("id"));
    				user.setUsername(username);
    				user.setPassword(resultSet.getString("password"));
    				user.setNickname(resultSet.getString("nickname"));
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(resultSet);
    			DBUtil.close(preparedStatement);
    			DBUtil.close(connection);
    		}
    		return user;
    	}
    
    	@Override
    	public List<User> load() {
    		// TODO Auto-generated method stub
    		Connection connection = DBUtil.getConnection();
    		String sql = "select * from usertable";
    		PreparedStatement preparedStatement = null;
    		ResultSet resultSet = null;
    		List<User> users = new ArrayList<User>();
    		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"));
    				user.setNickname(resultSet.getString("nickname"));
    				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 User login(String username, String password) {
    		Connection con = DBUtil.getConnection();
    		PreparedStatement pra = null;
    		ResultSet rs = null;
    		User user = null;
    		String sql = "select * from usertable where username = ?";
    		try {
    			pra = con.prepareStatement(sql);
    			pra.setString(1,username);
    			rs = pra.executeQuery();
    			while(rs.next()) {
    				user = new User();
    				user.setId(rs.getInt("id"));
    				user.setUsername(username);
    				user.setPassword(rs.getString("password"));
    				user.setNickname(rs.getString("nickname"));
    			
    			}
    			if(user==null)throw new UserException("该用户不存在");
    			
    			if(!user.getPassword().equals(password))throw new UserException("密码错误");
    		} catch (SQLException e) {
    			System.out.println(e.getMessage());
    		}
    		return user;
    	}
    
        
    }
    

      实验截图

    数据库中表

  • 相关阅读:
    multilabel-multiclass classifier
    关于zabbix _get返回Could not attach to pid的问题
    python导出环境依赖到req,txt文件中
    inode满的解决方法
    搞定面试官:咱们从头到尾再说一次 Java 垃圾回收
    SpringBoot项目,如何优雅的把接口参数中的空白值替换为null值?
    万万没想到,JVM内存区域的面试题也可以问的这么难?
    万万没想到,面试中,连 ClassLoader类加载器 也能问出这么多问题…..
    npm私服verdaccio报sha错误的解决方案
    配置SQL Server 2016无域AlwaysOn(转)
  • 原文地址:https://www.cnblogs.com/z245894546/p/7911284.html
Copyright © 2020-2023  润新知