?? 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 + -