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

📄 dbstoreimpl.java

📁 java的团队合作代码
💻 JAVA
字号:
package com.briup.impl.dbstore;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

import com.briup.BIDR;
import com.briup.Config;
import com.briup.DBStore;
import com.briup.Gather;
import com.briup.exception.DBStoreException;
import com.briup.impl.config.ConfigImpl;

public class DBStoreImpl implements DBStore {

	private String driver = "oracle.jdbc.driver.OracleDriver";
	private String url = "jdbc:oracle:thin:@192.168.40.254:1521:XE";
	private String username = "zhangzhen";
	private String password = "zhangzhen";
	private Properties prop;
	private Connection conn = null;
	private PreparedStatement pstmt = null;

	public DBStoreImpl(Properties props) {
		String pDriver = null;
		String pUrl = null;
		String pUsername = null;
		String pPassword = null;
		pDriver = props.getProperty("driver");
		pUrl = props.getProperty("url");
		pUsername = props.getProperty("username");
		pPassword = props.getProperty("password");
		if (pDriver != null)
			driver = pDriver;
		if (pUrl != null)
			url = pUrl;
		if (pUsername != null)
			username = pUsername;
		if (pPassword != null)
			password = pPassword;
		prop=props;
	}

	@SuppressWarnings("unchecked")
	public void insert(Collection c) throws DBStoreException {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url,username,password);
			String sql = "insert into t_detail values(?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			Iterator iter = c.iterator();
			while(iter.hasNext()){
				BIDR stu =(BIDR) iter.next();
				String username = stu.getUserName();
				String loginip = stu.getLoginIp();
				Date logindate = stu.getLoginDate();
				Date logoutdate = stu.getLogoutDate();
				String labip = stu.getLabIp();
				long timeduration = stu.getTimeDuration();
				pstmt.setString(1,username);
				pstmt.setString(2,loginip);
				pstmt.setTimestamp(3,new Timestamp(logindate.getTime()));
				pstmt.setTimestamp(4,new Timestamp(logoutdate.getTime()));
				pstmt.setString(5,labip);
				pstmt.setLong(6,timeduration);
				pstmt.executeUpdate();
				
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
				

		
	}

	public void insert(BIDR bidr) throws DBStoreException {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url,username,password);
			String sql = "insert into t_detail values(?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			String username = bidr.getUserName();
			String loginip = bidr.getLoginIp();
			Date logindate = bidr.getLoginDate();
			Date logoutdate = bidr.getLogoutDate();
			String labip = bidr.getLabIp();
			long timeduration = bidr.getTimeDuration();
			pstmt.setString(1,username);
			pstmt.setString(2,loginip);
			pstmt.setTimestamp(3,new Timestamp(logindate.getTime()));
			pstmt.setTimestamp(4,new Timestamp(logoutdate.getTime()));
			pstmt.setString(5,labip);
			pstmt.setLong(6,timeduration);
			pstmt.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void main(String[] agrs) throws Exception{
		Properties pro = new Properties();
		pro.setProperty("config_file_path","src/com/briup/resource/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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
}

⌨️ 快捷键说明

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