为了账号安全,请及时绑定邮箱和手机立即绑定

jsp连接数据库,tomcat服务器启动后第一次打开正常,再刷新就报空指针异常。

jsp连接数据库,tomcat服务器启动后第一次打开正常,再刷新就报空指针异常。

慕斯713762 2017-10-11 16:41:42
 jsp页面:<%@ page language="java" contentType="text/html; charset=utf-8"%><%@ page import="dao.TDao" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <%  TDao t=new TDao();  out.print(t.getLastDate()); %></body></html>DBHelper.javapackage tools;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBHelper { private static String driver="com.mysql.jdbc.Driver"; private static String username="root"; private static String password="root"; private static String url="jdbc:mysql://localhost:3306/datamanagement"; private static Connection conn=null; static {  try {    Class.forName(driver);  } catch (ClassNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } } public static Connection getConnection() {    if(conn==null) {   try {    conn=DriverManager.getConnection(url,username,password);   } catch (SQLException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   return conn;  }else {   return null;  } }  public static void main(String[] args) {  Connection conn=DBHelper.getConnection();  if(conn==null) {   System.out.println("连接失败");  }else {   System.out.println("连接成功!!!");  } }}TDao.javapackage dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import tools.DBHelper;public class TDao {  public String getLastDate() {  String lastDate=null;  PreparedStatement stmt=null;  ResultSet rs=null;    try {     Connection conn=DBHelper.getConnection();     String sql="select riqi from daily_report order by riqi desc";     stmt=conn.prepareStatement(sql);     rs=stmt.executeQuery();     rs.next();     lastDate=rs.getString("riqi");    }catch(Exception e) {     e.printStackTrace();    }finally {     try {      rs.close();      stmt.close();     }catch(Exception e) {      e.printStackTrace();     }    }  return lastDate; }  public static void main(String[] args) {  TDao t=new TDao();  System.out.println(t.getLastDate()); }}
查看完整描述

2 回答

?
慕慕9558314

TA贡献1条经验 获得超0个赞

走Debug看哪行值没取到

查看完整回答
反对 回复 2017-10-11
  • 慕斯713762
    慕斯713762
    奇怪的是,我家里电脑没问题,单位电脑就不行。感觉像是首次运行后有什么东西一直存在内存里面,再次刷新有冲突之类的问题。TOMCAT服务器重启一下又可以正常显示了,但刷新又不行。
  • 2 回答
  • 0 关注
  • 1315 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信