package com.kneel.core.help;
import java.io.IOException;
/**
* just wrap DbUtils QueryRunner, provider simple functions to use.
*
* give chance to add action and do operator
*
* query: resolve all query problem. insert: get secuqnce to insert update:
* execute update with params. delete: execute delete with params batch: batch
* update and delete with params.
*
* NOTE: batch operator, please cause of batchSize, this will be set how many
* counts we commit, and do next batch.
*
* NOTE: below have expand of Connection PARAMS, if call please add.
*
* NOTE: if query large Datas, please set FecthSize, this will be improve
* performance.
*
* we often use Bean to setter/getter, with columnToPropertyOverrides,we use
* this to process column name not same as property. or we can do below SELECT
* event_id id, event_date date,title from events. this will be re-name column
* lable name.(care of key of Database)
*
* queryBean: query one row to Bean queryBeanList: query multiple rows to
* List queryBeanMap: query multiple rows to Map
*
* Query List application
*
* queryColumn: query one column value queryColumnSqlKey: query one column value
* with sql key queryMap: query one row to Map queryKeyMap: query
* multiple rows to Map
package com.kneel.core.help;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DatabaseHelperTest extends TestCase{
private static final Log log = LogFactory.getLog(DatabaseHelperTest.class);
public void testSinglton(){
Thread t1 = new Thread(new Runnable(){
@Override
public void run() {
DatabaseHelper helper1 = new DatabaseHelper();
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run() {
DatabaseHelper helper2 = new DatabaseHelper();
}
});
t1.start();t2.start();
System.out.println("a");
}
public void testQuerySqlRsh(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEventById");
Event event = helper.query(sql, new ResultSetHandler(){
public Event handle(ResultSet rs) throws SQLException{
if(rs.next()){
Event event = new Event();
event.setId(rs.getLong(1));
event.setDate(rs.getDate(2));
event.setTitle(rs.getString(3));
return event;
}
return null;
}
},Long.valueOf(140));
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testUpdateSql(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("updateEvent");
Object[] params = new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(140)};
int effect = helper.update(sql,params);
System.out.println(effect);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testBatch(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("updateEvent");
Object[][] params = new Object[][]{
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(140)},
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(165)}
};
int effect[] = helper.batch(sql,params);
System.out.println(Arrays.toString(effect));
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testBatchInBatchSize(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("updateEvent");
Object[][] params = new Object[][]{
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(140)},
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(165)},
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(162)},
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(163)},
new Object[]{new java.sql.Date(new Date().getTime()),"updateEvent Title",Long.valueOf(164)}
};
int effect[] = helper.batch(sql,params,2);
System.out.println(Arrays.toString(effect));
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testInsert(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("insertEvent");
String sequnceName = helper.getSql("eventsSequnce");
Object[] params = new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"};
Long id = helper.insert(sql,sequnceName,params);
System.out.println(id);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testInsertBatch(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("insertEvent");
String sequnceName = helper.getSql("eventsSequnce");
Object[][] params = new Object[][]{
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"},
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"}
};
Long effect[] = helper.insertBatch(sql,sequnceName,params);
System.out.println(Arrays.toString(effect));
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testInsertInBatchSize(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("insertEvent");
String sequnceName = helper.getSql("eventsSequnce");
Object[][] params = new Object[][]{
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"},
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"},
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"},
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"},
new Object[]{new java.sql.Date(new Date().getTime()),"insertEvent Title"}
};
Long effect[] = helper.insertBatch(sql,sequnceName,params,2);
System.out.println(Arrays.toString(effect));
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryBean(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEventById");
Event event = helper.queryBean(sql, Event.class,Long.valueOf(140));
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryBeanByRenameLable(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEventById");
sql = "SELECT event_id id,event_date,title FROM events WHERE event_id = ?";
Event event = helper.queryBean(sql, Event.class,Long.valueOf(140));
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryBeanByRenameMapping(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEventById");
Map ctp = new HashMap();
ctp.put("EVENT_ID", "id");
ctp.put("EVENT_DATE", "date");
Event event = helper.queryBean(sql, Event.class,ctp,Long.valueOf(140));
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryBeanList(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEvents");
Map ctp = new HashMap();
ctp.put("EVENT_ID", "id");
ctp.put("EVENT_DATE", "date");
List event = helper.queryBeanList(sql, Event.class,ctp);
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testBeanMap(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEvents");
Map ctp = new HashMap();
ctp.put("EVENT_ID", "id");
ctp.put("EVENT_DATE", "date");
Map event = helper.queryBeanMap(sql, Event.class,"EVENT_ID",ctp);
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryMap(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEvents");
Map event = helper.queryMap(sql);
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryKeyMap(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEvents");
Map> event = helper.queryKeyMap(sql, "EVENT_ID");
System.out.println(event);
} catch (SQLException e) {
log.error("sql error", e);
}
}
public void testQueryListMap(){
DatabaseHelper helper = new DatabaseHelper("/sql/Event.xml");
try {
String sql = helper.getSql("queryEvents");
List
什么是Hessian
The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary p
在Spark Shell上,通过创建HiveContext可以直接进行Hive操作
1. 操作Hive中已存在的表
[hadoop@hadoop bin]$ ./spark-shell
Spark assembly has been built with Hive, including Datanucleus jars on classpath
Welcom
JMS Message Delivery Reliability and Acknowledgement Patterns
http://wso2.com/library/articles/2013/01/jms-message-delivery-reliability-acknowledgement-patterns/
Transaction and redelivery in
转载请出自出处:http://eksliang.iteye.com/blog/2177567 一、游标
数据库使用游标返回find的执行结果。客户端对游标的实现通常能够对最终结果进行有效控制,从shell中定义一个游标非常简单,就是将查询结果分配给一个变量(用var声明的变量就是局部变量),便创建了一个游标,如下所示:
> var