J2EE零碎笔记

前段时间一直在赶代码,没有时间整理笔记了。这两天趁还没忘光赶紧记录一下,有些零碎,想起什么写什么吧。

1.java.sql.PreparedStatement的批量处理数据.

  之前都是一条一条的插入,觉得太2了,于是查到PreparedStatement可以批量处理,直接贴DEMO代码好了

PreparedStatement ps = conn.prepareStatement( 
   "INSERT into employees values (?, ?, ?)"); 
for (n = 0; n < 100; n++) { 
  ps.setString(name[n]); 
  ps.setLong(id[n]); 
  ps.setInt(salary[n]); 
  ps.addBatch(); 
} 
ps.executeBatch(); 

如果数据量特别大的话,可以限制一下batch的size,比如每1000条记录就提交一次

PreparedStatement ps = conn.prepareStatement( 
   "INSERT into employees values (?, ?, ?)"); 
int[] result;
int affectedLine=0;
for (n = 0; n < 2345; n++) { 
ps.setString(name[n]); 
ps.setLong(id[n]); 
ps.setInt(salary[n]); 
ps.addBatch();
if((i+1)%BATCH_LIMITE==0){
	result=pstmt.executeBatch();
	affectedLine+=calculateSum(result);
	conn.commit();
	ps.clearBatch();
}
result=ps.executeBatch();
affectedLine+=calculateSum(result);
}

2.获取WEB当前根目录:

public static String getWebClassesPath() throws URISyntaxException {
		String path =GlobalParameter.class.getResource("/").toURI().getPath();
		return path;
	}
	public static String getWebInfPath() throws IllegalAccessException, URISyntaxException {
		String path = getWebClassesPath();
		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF") + 8);
		} else {
			throw new IllegalAccessException("路径获取错误");
		}
		return path;
	}
	public static String getWebRoot() throws IllegalAccessException, URISyntaxException {
		String path = getWebClassesPath();
		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF/classes"));
		} else {
			throw new IllegalAccessException("路径获取错误");
		}
		return path;
	}
	@Test
	public void testPath() throws IllegalAccessException, URISyntaxException {
		System.out.println("Web Class Path = " + getWebClassesPath());
		System.out.println("WEB-INF Path = " + getWebInfPath());
		System.out.println("WebRoot Path = " + getWebRoot());
	}

这里有一点要说明一下:在开发环境下(我是Eclipse J2EE+Tomcat 7),eclipse用一个server的插件模拟Tomcat的服务,所以通过以上方法获得的WEBROOT PATH通常会是D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mystruts\这种,如果有动态生成的文件要放到WEB目录下就在这里面找吧,貌似是个临时的文件夹,没有仔细研究。(一开始放在项目工程WEBROOT下的文件会被复制到这个临时目录里面的,所以都找得到)

3.双向MAP,BiMap.支持通过value查找key

使用的是Google Collection,这里有介绍。

使用方法

BiMap<String,String> map = HashBiMap.create();   
map.put("kafka0102","1");   
System.out.println(map.get("kafka0102"));   
System.out.println(map.inverse().get("1");//输出反向数据没有提供单独的函数,而是需要调用inverse().get() 

 

 

4.目前想起这么多,回头再补

posted on 2011-04-29 15:36  Jersey  阅读(609)  评论(0编辑  收藏  举报