1.首先需要下载csvjdbc.jar文件,可以从附件下载
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtil {
/**
* CSV JDBC驱动
*/
private static final String CSV_JDBC_DRIVER = "org.relique.jdbc.csv.CsvDriver";
/**
* jdbc连接csv的 Header
*/
private static final String CSV_JDBC_HEADER = "jdbc:relique:csv:";
/**
* separator 参数设置: CSV 文件中数据分割符
*/
private static final String CSV_PROP_SEPARATOR = "separator";
/**
* separator 参数设置: 首行包含数据否
*/
private static final String CSV_PROP_SUPHEADER = "suppressHeaders";
/**
* fileExtension 参数设置: 文件类型
*/
private static final String CSV_PROP_FILEEXTEN = "fileExtension";
/**
* charset 参数设置: 字符集
*/
private static final String CSV_PROP_CHARSET = "charset";
public static Connection getConn(String csvDirectory){
// 加载CSV-JDBC驱动
try {
Class.forName(CSV_JDBC_DRIVER);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 解析CSV前的一些准备工作:解析参数设置
final Properties props = new java.util.Properties();
// 该CSV的数据是由','分隔
props.put(CSV_PROP_SEPARATOR, ",");
// 首行(去掉上面头行后的第一行)包含数据
props.put(CSV_PROP_SUPHEADER, "false");
// 要解析的文件类型
props.put(CSV_PROP_FILEEXTEN, ".csv");
// 字符集
props.put(CSV_PROP_CHARSET, "UTF-8");
// 创建一个connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
Connection conn = null;
try {
conn = DriverManager.getConnection(CSV_JDBC_HEADER
+ csvDirectory, props);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement stmt,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
public class DemoDriver {
public static Map<String,String> parseUser(Connection conn,final String csvName){
Statement stmt;
Map<String,String> users = new HashMap<String, String>();
try {
stmt = conn.createStatement();
// Select the ID and NAME columns from sample.csv
final ResultSet rs = stmt.executeQuery("SELECT "
+ " objectClass,cn,description,member " + " FROM " + csvName +" where objectClass = 'user'");
while (rs.next()) {
System.out.println("objectClass:"+rs.getString("objectClass")+";cn:"+rs.getString("cn")+";description:"+rs.getString("description")+";member:"+rs.getString("member"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
public static void parse(final String csvDirectory,final String csvName){
Connection conn = JdbcUtil.getConn(csvDirectory);
parseUser(conn,csvName);
}
public static void main(String[] args) {
parse("F:\\unis\\test\\", "output");
}
}