Java web开发中遇到的问题及解决方法

    技术2025-12-21  13

    目录

    1、通过数据库连接池,获得查询结果集,但是无法取出数据2、过滤器放行出错3、德鲁伊连接池获取不到连接4、过程中遇到其他问题,值得一提的是一定要注意文件所处的文件夹

    1、通过数据库连接池,获得查询结果集,但是无法取出数据

    控制台报错:java.sql.SQLException: Before start of result set 出错代码

    //获取一个连接 Connection conn = getConnection(); System.out.println("从连接池获取一个连接成功!"); PreparedStatement state = null; ResultSet rs = null; try { //预编译处理 state = conn.prepareStatement("select * from user where username =? and password =?"); //填充SQL语句 state.setString(1,username); state.setString(2,password); rs = state.executeQuery(); //生成对象 User user = new User(rs.getString("username"), rs.getString("password") , rs.getInt("count")); return user; } catch (SQLException throwables) { throwables.printStackTrace(); } finally { //释放资源 close(conn,state,rs); }

    原因:指针处于第一个结果集之前,取结果得先调用next()方法。

    2、过滤器放行出错

    出错代码:

    //放行资源 if (requestURI.endsWith("/")||requestURI.endsWith("/index.jsp")||requestURI.endsWith("/product.jsp")|| requestURI.endsWith("/login.jsp")|| requestURI.endsWith("/error.jsp")|| requestURI.endsWith("/comment")||requestURI.endsWith("/comment.jsp")|| requestURI.endsWith("/success.jsp")|| requestURI.endsWith("/sign.jsp")||requestURI.endsWith("/notlogin.jsp")|| loginName!=null){ //调取下一个servlet或filter filterChain.doFilter(servletRequest,servletResponse);

    原因:**requestURI.endsWith("/sign.jsp")和requestURI.endsWith("/sign")**不一样, .jsp和servlet弄混了,这种情况一般就是自己忘掉了加某个放行的条件,记得加上。

    3、德鲁伊连接池获取不到连接

    控制台报错:NullPointerException:The url cannot be null 网页报错: HTTP Status 500 – Internal Server Error Type Exception Report Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception java.lang.NullPointerException

    我遇到的是配置文件出错,一旦是获取连接不成功多半都是配置文件问题,我这个就可能是手残不小心按到了一个空格。。。。。。 错误示范: druid.properties文件

    url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8 username=root password=123456 driverClassName=com.mysql.jdbc.Driver initialSize=5 maxActive=10 minIdle=5 maxWait=3000

    你什么都看不出来 请看截图: 删除之后天空都变晴朗了

    4、过程中遇到其他问题,值得一提的是一定要注意文件所处的文件夹

    我遇到了如下问题 web.xml移出WEB-INF文件夹 lib文件夹目录错误,创建多留个心眼,如果还是有一直导包错误问题的可以参考如下修改过程 1、选择这个模式,就是将本地文件推送到tomcat下进行部署, 2、这个是推送到tomcat下文件的路径形式,web项目必须有WEB-INF文件夹,文件夹下面需要有class文件夹和lib文件,分别放编译后的class文件,和依赖的jar包 3、这个地方就是idea发现了你的项目是一个web项目,可以被引进来的项目 所以在部署的时候,先选择1,在选择3,最后看2的结构是不是符合要求

    一杯茶,一包烟,一个BUG改一天······

    Processed: 0.012, SQL: 9