Skip to content

Commit

Permalink
修改Jdbc2Stream,移除nutz依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yuan committed May 13, 2017
1 parent 5b826ef commit be48bdf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 82 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.58</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
140 changes: 65 additions & 75 deletions src/main/java/org/ansj/dic/impl/Jdbc2Stream.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit be48bdf

Please sign in to comment.