⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbstoreimpl.java

📁 一个信息采集系统...对刚接触JSP和JAVA的人来说还有用
💻 JAVA
字号:
package com.briup.inf.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import com.briup.exception.DBStoreException;
import com.briup.inf.Config;
import com.briup.inf.DBStore;
import com.briup.inf.Gather;
import com.briup.pojo.BIDR;

/**
 * class DBStoreImpl
 * 
 * @author Jimmy Zhou
 * @Date 2008-2-1 下午10:23:47
 */
public class DBStoreImpl implements DBStore {

	private final String driver;

	private final String url;

	private final String username;

	private final String password;

	public DBStoreImpl(Properties pro) {
		driver = pro.getProperty("driver");
		url = pro.getProperty("url");
		username = pro.getProperty("username");
		password = pro.getProperty("password");
	}

	private Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public void insert(Collection c) throws DBStoreException {
		//得到一个connection连接
		Connection conn = getConnection();
		if(conn==null){
			throw new DBStoreException();
		}
		PreparedStatement pstmt = null;
		try {
			//取消事务的自动提交改为手动提交
			conn.setAutoCommit(false);
			//遍历集合Collection中的BIDR数据
			Iterator iter = c.iterator();
			while (iter.hasNext()) {
				BIDR bidr = (BIDR) iter.next();
				String loginName = bidr.getLoginName();
				String loginIP = bidr.getLoginIp();
				Timestamp login = new Timestamp(bidr.getLoginDate().getTime());
				Timestamp logout = new Timestamp(bidr.getLogoutDate().getTime());
				String labIp = bidr.getLabIp();
				long time = bidr.getTimeDuration();

				String sql = "insert into detail values (?,?,?,?,?,?)";
				try {
					//执行SQL语句以及传参
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, loginName);
					pstmt.setString(2, loginIP);
					pstmt.setTimestamp(3, login);
					pstmt.setTimestamp(4, logout);
					pstmt.setString(5, labIp);
					pstmt.setLong(6, time);
					pstmt.executeUpdate();
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			// 进行事务的提交和连接的关闭
			conn.commit();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	public static void main(String[] args) {
		Properties pro = new Properties();
		pro.setProperty("config_file_path", "src/com/briup/config.xml");
		Config config = ConfigImpl.newInstance(pro);
		try {
			DBStore ds = config.getDBStore();
			Gather g = config.getGather();
			Collection c = g.doGather();
			System.out.println(c.size());
			ds.insert(c);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -