use of org.nutz.dao.impl.SimpleDataSource in project ansj_seg by NLPchina.
the class Jdbc2Stream method toStream.
@Override
public InputStream toStream(String path) {
path = path.substring(7);
String[] split = path.split("\\|");
String jdbc = split[0];
String username = split[1];
String password = split[2];
String sqlStr = split[3];
String logStr = jdbc + "|" + username + "|********|" + sqlStr;
SimpleDataSource ds = null;
try {
ds = new SimpleDataSource();
ds.setJdbcUrl(jdbc);
ds.setUsername(username);
ds.setPassword(password);
Dao dao = new NutDao(ds);
Sql sql = Sqls.create(sqlStr);
Sql execute = dao.execute(sql.setCallback(new SqlCallback() {
@Override
public byte[] invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(100 * 1024);
while (rs.next()) {
try {
int count = rs.getMetaData().getColumnCount();
for (int i = 1; i < count; i++) {
baos.write(String.valueOf(rs.getObject(i)).getBytes());
baos.write(TAB);
}
baos.write(String.valueOf(rs.getObject(count)).getBytes());
baos.write(LINE);
} catch (IOException e) {
e.printStackTrace();
}
}
return baos.toByteArray();
}
}));
return new ByteArrayInputStream((byte[]) execute.getResult());
} catch (Exception e) {
throw new LibraryException("err to load by jdbc " + logStr);
} finally {
if (ds != null) {
ds.close();
}
}
}
use of org.nutz.dao.impl.SimpleDataSource in project nutz by nutzam.
the class DaoUp method buildDataSource.
/**
* 构建DataSource,子类可覆盖. 如果存在Druid,则使用之,否则使用内置的SimpleDataSource
* @param props 配置信息
* @return 目标DataSource
*/
protected DataSource buildDataSource(Properties props) {
if (druidFactoryClass != null) {
log.debug("build DruidDataSource by props");
Mirror<?> mirror = Mirror.me(druidFactoryClass);
DataSource ds = (DataSource) mirror.invoke(null, "createDataSource", props);
if (!props.containsKey("maxWait"))
Mirror.me(ds).setValue(ds, "maxWait", 15 * 1000);
return ds;
}
log.debug("build SimpleteDataSource by props");
return SimpleDataSource.createDataSource(props);
}
use of org.nutz.dao.impl.SimpleDataSource in project nutz by nutzam.
the class SimpleDaoTest method test_fastinsert_speed.
// @Test
// public void test_map_blob() throws FileNotFoundException {
// if (dao.exists("t_test_map_blob")) {
// dao.drop("t_test_map_blob");
// Lang.quiteSleep(1000);
// }
// dao.execute(Sqls.create("create table t_test_map_blob(id
// VARCHAR(60),filecontent blob)"));
//
// NutMap map = new NutMap().setv(".table", "t_test_map_blob");
// map.put("id", R.UU32());
// map.put("filecontent", new
// FileInputStream("W:\\usb3.0_intel_1.0.10.255_w7.zip"));
//
// dao.insert(map);
//
// Record re = dao.fetch("t_test_map_blob", Cnd.NEW());
// assertNotNull(re);
// System.out.println(re.get("filecontent").getClass());
// System.out.println(new String((byte[])re.get("filecontent")));
//
//// assertEquals("你好", new String((byte[])re.get("filecontent")));
// }
//@Test
public void test_fastinsert_speed() {
SimpleDataSource ds = new SimpleDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost/nutztest");
ds.setUsername("root");
ds.setPassword("root");
dao = new NutDao(ds);
// 删表重建
dao.create(Pet.class, true);
Lang.sleep(1000);
Stopwatch sw = Stopwatch.begin();
// 生成10*2000个对象
List<List<Pet>> list = new ArrayList<List<Pet>>();
for (int i = 0; i < 10; i++) {
List<Pet> pets = new ArrayList<Pet>();
for (int j = 0; j < 2000; j++) {
Pet pet = Pet.create(R.UU32());
pets.add(pet);
}
list.add(pets);
}
sw.stop();
System.out.println("生成对象的耗时: " + sw);
for (final List<Pet> tmp : list) {
sw = Stopwatch.begin();
Trans.exec(new Atom() {
public void run() {
dao.fastInsert(tmp);
}
});
sw.stop();
System.out.println("fastInsert插入2000个对象的耗时" + sw);
}
dao.create(Pet.class, false);
for (int i = 0; i < 10; i++) {
try {
final int t = i;
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
sw = Stopwatch.begin();
System.out.println(System.currentTimeMillis());
PreparedStatement ps = conn.prepareStatement("INSERT INTO t_pet(name,alias) VALUES(?,?)");
System.out.println(System.currentTimeMillis());
for (int j = 0; j < 2000; j++) {
ps.setString(1, "pet_" + t + "_" + j);
ps.setString(2, "");
// ps.setInt(3, 30);
// ps.setInt(4, 0);
// ps.setDate(5, null);
// ps.setFloat(6, 0);
ps.addBatch();
}
System.out.println(System.currentTimeMillis());
ps.executeBatch();
conn.commit();
sw.stop();
System.out.println(sw);
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Aggregations