📄 stathandle.java
字号:
package myitem.renshu;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class StatHandle {
/**
* 往数据库插入新的访问信息
* @param ip String 用户IP地址
* @param oper String
* @param browser String
* @throws Exception
*/
public static void insert(String ip, String os, String browser) throws
Exception {
Connection cnn = null;
PreparedStatement ps = null;
try {
cnn = DBHandle.getConn(); //获取连接句柄
ps = cnn.prepareStatement( //创建语句句柄
"insert into history(ip, os, browser, accessdate, accesstime) values(?,?,?,?,?)");
//填充参数
ps.setString(1, ip);
ps.setString(2, os.toUpperCase()); //存大写
ps.setString(3, browser.toUpperCase());
ps.setDate(4, new java.sql.Date(System.currentTimeMillis()));
ps.setTime(5, new Time(System.currentTimeMillis()));
ps.executeUpdate(); //执行语句
}
catch (Exception es) {
throw es;
}
finally
{
try
{
DBHandle.closeResource(ps, null, cnn); //释放语句句柄和连接句柄
}
catch(Exception e)
{
}
}
return;
}
/**
* 查询访问站点次数
*/
public static int select() throws
Exception {
Connection cnn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int count = 0;
try {
cnn = DBHandle.getConn();
//获取连接句柄
ps = cnn.prepareStatement(
"select count(*) from history");
//创建语句句柄
rs = ps.executeQuery();
//执行查询语句,获取结果集
rs.next();
//游标指向第一条记录
count = rs.getInt(1);
//记录访问次数
}
catch (Exception es) {
throw es;
//直接将异常抛出,由外部程序处理
}
finally {
DBHandle.closeResource(ps, rs, cnn);
//释放语句句柄、连接句柄、结果集
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -