From be48bdf7cbd421b0471348064bfc222435f6937d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E6=BA=90?= Date: Sat, 13 May 2017 21:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9Jdbc2Stream=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4nutz=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 - .../java/org/ansj/dic/impl/Jdbc2Stream.java | 140 ++++++++---------- 2 files changed, 65 insertions(+), 82 deletions(-) diff --git a/pom.xml b/pom.xml index 06619eec..de15773a 100644 --- a/pom.xml +++ b/pom.xml @@ -44,13 +44,6 @@ compile - - org.nutz - nutz - 1.r.58 - provided - - junit junit diff --git a/src/main/java/org/ansj/dic/impl/Jdbc2Stream.java b/src/main/java/org/ansj/dic/impl/Jdbc2Stream.java index 26063749..a2f380af 100644 --- a/src/main/java/org/ansj/dic/impl/Jdbc2Stream.java +++ b/src/main/java/org/ansj/dic/impl/Jdbc2Stream.java @@ -1,109 +1,99 @@ package org.ansj.dic.impl; +import org.ansj.dic.PathToStream; +import org.ansj.exception.LibraryException; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.InputStream; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.SQLException; - -import org.ansj.dic.PathToStream; -import org.ansj.exception.LibraryException; -import org.nutz.dao.Dao; -import org.nutz.dao.Sqls; -import org.nutz.dao.impl.NutDao; -import org.nutz.dao.impl.SimpleDataSource; -import org.nutz.dao.sql.Sql; -import org.nutz.dao.sql.SqlCallback; /** * jdbc:mysql://192.168.10.103:3306/infcn_mss?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull|username|password|select name as name,nature,freq from dic where type=1 - * - * @author ansj * + * @author ansj */ public class Jdbc2Stream extends PathToStream { - private static final byte[] TAB = "\t".getBytes(); - - private static final byte[] LINE = "\n".getBytes(); - - @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]; + private static final byte[] TAB = "\t".getBytes(); - String sqlStr = split[3]; + private static final byte[] LINE = "\n".getBytes(); - String logStr = jdbc + "|" + username + "|********|" + sqlStr; + static { + String[] drivers = {"org.h2.Driver", + "com.ibm.db2.jcc.DB2Driver", + "org.hsqldb.jdbcDriver", + "org.gjt.mm.mysql.Driver", + "oracle.jdbc.OracleDriver", + "org.postgresql.Driver", + "net.sourceforge.jtds.jdbc.Driver", + "com.microsoft.sqlserver.jdbc.SQLServerDriver", + "org.sqlite.JDBC", + "com.mysql.jdbc.Driver"}; + for (String driverClassName : drivers) { + try { + try { + Thread.currentThread().getContextClassLoader().loadClass(driverClassName); + } catch (ClassNotFoundException e) { + Class.forName(driverClassName); + } + } catch (Throwable e) { + } + } + } - SimpleDataSource ds = null; + @Override + public InputStream toStream(String path) { + path = path.substring(7); - try { - ds = new SimpleDataSource(); + String[] split = path.split("\\|"); - ds.setJdbcUrl(jdbc); - ds.setUsername(username); - ds.setPassword(password); + String jdbc = split[0]; - Dao dao = new NutDao(ds); + String username = split[1]; - Sql sql = Sqls.create(sqlStr); + String password = split[2]; - 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); + String sqlStr = split[3]; - } catch (IOException e) { - e.printStackTrace(); - } - } - return baos.toByteArray(); - } - })); + String logStr = jdbc + "|" + username + "|********|" + sqlStr; - return new ByteArrayInputStream((byte[]) execute.getResult()); - } catch (Exception e) { - throw new LibraryException("err to load by jdbc " + logStr); - } finally { - if (ds != null) { - ds.close(); - } - } + try (Connection conn = DriverManager.getConnection(jdbc, username, password); + PreparedStatement statement = conn.prepareStatement(sqlStr); + ResultSet rs = statement.executeQuery(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(100 * 1024)) { - } + int i, count; + while (rs.next()) { + for (i = 1, count = rs.getMetaData().getColumnCount(); 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); + } - public static String encryption(String path){ + return new ByteArrayInputStream(baos.toByteArray()); + } catch (Exception e) { + throw new LibraryException("err to load by jdbc " + logStr); + } + } - String[] split = path.split("\\|"); + public static String encryption(String path) { - String jdbc = split[0]; + String[] split = path.split("\\|"); - String username = split[1]; + String jdbc = split[0]; - String password = split[2]; + String username = split[1]; - String sqlStr = split[3]; + String password = split[2]; - return jdbc + "|" + username + "|********|" + sqlStr; - } + String sqlStr = split[3]; + return jdbc + "|" + username + "|********|" + sqlStr; + } }