Search in sources :

Example 1 with SimpleDataSource

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();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) LibraryException(org.ansj.exception.LibraryException) Sql(org.nutz.dao.sql.Sql) Dao(org.nutz.dao.Dao) NutDao(org.nutz.dao.impl.NutDao) SqlCallback(org.nutz.dao.sql.SqlCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) ResultSet(java.sql.ResultSet) LibraryException(org.ansj.exception.LibraryException)

Example 2 with SimpleDataSource

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);
}
Also used : SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) DataSource(javax.sql.DataSource)

Example 3 with SimpleDataSource

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();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) SQLException(java.sql.SQLException) Stopwatch(org.nutz.lang.Stopwatch) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Atom(org.nutz.trans.Atom) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) List(java.util.List) ArrayList(java.util.ArrayList) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet)

Aggregations

SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 NutDao (org.nutz.dao.impl.NutDao)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataSource (javax.sql.DataSource)1 LibraryException (org.ansj.exception.LibraryException)1 Dao (org.nutz.dao.Dao)1 Sql (org.nutz.dao.sql.Sql)1 SqlCallback (org.nutz.dao.sql.SqlCallback)1 Pet (org.nutz.dao.test.meta.Pet)1 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)1 Stopwatch (org.nutz.lang.Stopwatch)1 Atom (org.nutz.trans.Atom)1