日期:2014-05-20 浏览次数:21036 次
import java.io.UnsupportedEncodingException; import java.net.URLDecoder; 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 Main { public static void main(String[] args) { try { String csvFilePath = URLDecoder.decode(Main.class.getResource( "基础数据").getPath(), "UTF-8"); Connection conn = Main.getCSVConnection(csvFilePath); if (conn != null) { Statement stmt = conn.createStatement(); String sql = "select * from 车辆"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static Connection getCSVConnection(String csvFilePath) { Connection conn = null; try { Class.forName("org.relique.jdbc.csv.CsvDriver"); Properties props = new java.util.Properties(); // props.put("separator", "|"); props.put("suppressHeaders", "true"); // props.put("fileExtension", ".txt"); // props.put("charset", "ISO-8859-2"); // props.put("maxFileSize", 10000); // props.put("create", "true"); // props.put("lineBreakEscape", "ELB"); // props.put("carriageReturnEscape", "ECR"); // props.put("useQuotes", "true"); conn = DriverManager.getConnection("jdbc:relique:csv:" + csvFilePath, props); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } }