【发布时间】:2017-05-11 09:06:44
【问题描述】:
我正在开发一个 Spring Boot Web 应用程序(我的第一个应用程序),当我将它部署在嵌入式 tomcat 服务器中时它工作正常。但是当我将它部署在独立 Tomcat 服务器中时,它无法访问数据库。我正在使用 Rest WebService 将数据传递到前端,我的 url 看起来像
http://localhost:8080/day_demand?day=3
但是当我访问时在我的独立服务器中
http://localhost:8080/WebApp/day_demand?day=3(WebApp是我的项目名)
通过以下代码连接数据库:
private Connection connectToDatabaseOrDie()
{
Connection conn = null;
try
{
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/data_base";
conn = DriverManager.getConnection(url,"user", "password");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
return conn;
}
private void populateListOfTopics(Connection conn, List<State> listOfBlogs,Timestamp start_time,Timestamp end_time,int zone_id)
{
try
{
String sql= "SELECT * FROM public.table where time >= ? and time <= ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setTimestamp(1,start_time);
pstmt.setTimestamp(2,end_time);
ResultSet rs = pstmt.executeQuery();
while ( rs.next() )
{
State blog = new State();
blog.year = rs.getInt ("year");
blog.month=rs.getInt ("month");
blog.day = rs.getInt ("day");
blog.hour = rs.getInt ("hour");
listOfBlogs.add(blog);
}
rs.close();
pstmt.close();
conn.close();
}
catch (SQLException se) {
System.err.println("Threw a SQLException creating the list of state.");
System.err.println(se.getMessage());
} catch (Exception e) {
System.out.println("Err");
e.printStackTrace();
}
}
我无法访问数据。感谢任何帮助。
【问题讨论】:
-
你检查日志了吗?是否给出任何具体错误。
标签: javascript jquery web-services tomcat spring-boot