我想第一次启动页面时调用数据库,并且生成静态页面,下次刷新时不用在去数据库中读取。写了一个代码如下:<%@ page contentType="text/html;charset=gb2312"%>
<%@page import="bean.ReadTemplateException"%>
<%@page import="bean.WriteFileException"%>
<%@ page import="bean.ReadTemplates"%>
<%@ page import="bean.WriteHtml"%>
<%@ include file="conn1.jsp"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Calendar,java.util.Date,java.text.SimpleDateFormat"%>
<%
String[] flag = {"###title###","###date###","###pic###","###content###"};
String sqlUser = "select * from news order by id desc";
rs = stmt.executeQuery(sqlUser);
String newtitle;
String pic;
//String newsdate;
String newbody;
//String filename;
newtitle=rs.getString("News_Title");
pic=rs.getString("pic1");
//newsdate=rs.getDate("News_Date");
newbody=rs.getString("News_Body");
//filename=rs.getString("File_Name");
//String title=request.getParameter("title");
//String content=request.getParameter("content");
//String editer="admin";
//int classid = Integer.parseInt(request.getParameter("class"));
String filePath = "";
filePath = application.getRealPath("tpl.html");
String templateContent;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
//out.print(templateContent);
//out.flush();
}catch(ReadTemplateException e){
throw new Exception("模板信息读取失败。请联系系统管理员。");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(System.currentTimeMillis());
String nowdate = sdf.format(d);
templateContent = templateContent.replaceAll(flag[0],newtitle);
templateContent = templateContent.replaceAll(flag[1],nowdate);
templateContent = templateContent.replaceAll(flag[2],pic);
templateContent = templateContent.replaceAll(flag[3],newbody);
//根据时间得文件名与路径名
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) + ".html";
String pathName = application.getRealPath("./docs") + "\\" + calendar.get(Calendar.YEAR) +
"\\"+ (calendar.get(Calendar.MONTH)+1) + "\\" + calendar.get(Calendar.DAY_OF_MONTH)+"\\";
try{
WriteHtml.save(templateContent,pathName,fileName);
}catch(WriteFileException we){
throw new Exception("操作失败!");
}%>但是出现如下错误:javax.servlet.ServletException: java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 无效的游标状态。
请大家指正!!!
<%@page import="bean.ReadTemplateException"%>
<%@page import="bean.WriteFileException"%>
<%@ page import="bean.ReadTemplates"%>
<%@ page import="bean.WriteHtml"%>
<%@ include file="conn1.jsp"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Calendar,java.util.Date,java.text.SimpleDateFormat"%>
<%
String[] flag = {"###title###","###date###","###pic###","###content###"};
String sqlUser = "select * from news order by id desc";
rs = stmt.executeQuery(sqlUser);
String newtitle;
String pic;
//String newsdate;
String newbody;
//String filename;
newtitle=rs.getString("News_Title");
pic=rs.getString("pic1");
//newsdate=rs.getDate("News_Date");
newbody=rs.getString("News_Body");
//filename=rs.getString("File_Name");
//String title=request.getParameter("title");
//String content=request.getParameter("content");
//String editer="admin";
//int classid = Integer.parseInt(request.getParameter("class"));
String filePath = "";
filePath = application.getRealPath("tpl.html");
String templateContent;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
//out.print(templateContent);
//out.flush();
}catch(ReadTemplateException e){
throw new Exception("模板信息读取失败。请联系系统管理员。");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(System.currentTimeMillis());
String nowdate = sdf.format(d);
templateContent = templateContent.replaceAll(flag[0],newtitle);
templateContent = templateContent.replaceAll(flag[1],nowdate);
templateContent = templateContent.replaceAll(flag[2],pic);
templateContent = templateContent.replaceAll(flag[3],newbody);
//根据时间得文件名与路径名
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) + ".html";
String pathName = application.getRealPath("./docs") + "\\" + calendar.get(Calendar.YEAR) +
"\\"+ (calendar.get(Calendar.MONTH)+1) + "\\" + calendar.get(Calendar.DAY_OF_MONTH)+"\\";
try{
WriteHtml.save(templateContent,pathName,fileName);
}catch(WriteFileException we){
throw new Exception("操作失败!");
}%>但是出现如下错误:javax.servlet.ServletException: java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 无效的游标状态。
请大家指正!!!
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:wysp";
String username="";
String password="";
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
conn=DriverManager.getConnection(url,username,password);
}
catch(SQLException sqlex) {
System.err.println("无法连接数据库");
sqlex.printStackTrace();
System.exit(1);
}
try {
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch (SQLException sqlex){
sqlex.printStackTrace();
}
%>
先改成
stmt=conn.createStatement();
试试