📄 periodservlet2.java
字号:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
public class PeriodServlet2 extends HttpServlet
{
static int counter;
public void init()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:test");
Statement stmt=conn.createStatement();
String strQuery="select * from ViewNum";
ResultSet rs=stmt.executeQuery(strQuery);
boolean firstAccess=true;
while (rs.next())
{
counter=rs.getInt("num");
firstAccess=false;
}
if (firstAccess)
{
counter=0;
String strInsert="INSERT INTO ViewNum VALUES(0)";
stmt.executeUpdate(strInsert);
}
conn.close();
}
catch(Exception e){}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/HTML;charset=GB2312");
PrintWriter out=response.getWriter();
counter++;
out.println("<HTML>");
out.println("<head>");
out.println("<title> 保持计数器连续</title></head>");
out.println("<body>");
out.println("该Servlet已经被访问 <b>"+String.valueOf(counter)+" </b>次!"+"\n");
out.println("</body>");
out.println("</HTML>");
}
public void destroy()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:test");
Statement stmt=conn.createStatement();
String strUpdate="UPDATE ViewNum set num="+counter;
stmt.executeUpdate(strUpdate);
conn.close();
}
catch(Exception e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -