package com.vefan.csv;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 本文讲的是在java环境下对CSV文件进行读取操作,前几天在网上找到一个驱动可以把这种格式的文件当作数据库一样读取。操作步骤如下:
* 第一、下载驱动程序http://sourceforge.net/projects/csvjdbc
* 第二、把csvjdbc.jar文件放到classpath路径中去
* */
public class JdbcCsv
{
public static void main(String args[]){
JdbcCsv jdbcCsv = new JdbcCsv();
jdbcCsv.queryCsv();
}
public void queryCsv(){
Connection conn = null;
Statement stmt = null;
ResultSet results = null;
try
{
//load the driver into memory
Class.forName("org.relique.jdbc.csv.CsvDriver");
// create a connection. The first command line parameter is assumed
// to be the directory in which the .csv files are held
conn = DriverManager.getConnection("jdbc:relique:csv:E:\\workspace\\JavaApp\\csv");
// create a Statement object to execute the query
stmt = conn.createStatement();
String sql = "select * from user";
// String sql = "select * from user where rownum<3"; //不支持此种查询
// String sql = "select top 10 * from user"; //不支持此种查询
results = stmt.executeQuery(sql);
// results.setFetchSize(3); //不支持
ResultSetMetaData metaData = results.getMetaData();
int columnCount = metaData.getColumnCount(); //一共有多少列
for(int i=1; i<=columnCount; i++){
String name = metaData.getColumnName(i);
if(i!=columnCount)
System.out.print(name+"\t\t");
else
System.out.println(name+"\t\t");
}
while(results.next()){
// int rowNo = results.getRow(); 不支持获取当前行号
// System.out.println("第"+rowNo+"行");
for(int i=1; i<=columnCount; i++){
String value = results.getString(i);
if(i!=columnCount)
System.out.print(value+"\t\t");
else
System.out.println(value+"\t\t");
}
}
} catch (Exception e)
{
e.printStackTrace();
}
finally{
try
{
if(null != results) results.close();
if(null != stmt) stmt.close();
if(null != conn) conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
} //end of finally
}
}
/*
A read-only JDBC driver for Java that uses Comma Separated Value (CSV) files as database tables.
Ideal for writing data import programs and conversion programs.
The latest file releases:
Package Release Date
CsvJdbc 0.10 August 9, 2004
当时下载的csvjdbc.jar文件版本是0.10, 有很多JDBC的功能都不支持,
它只能把csv文件的第一行作为字段名,从第二行作为数据行,不能指定其它行作为字段名;
在创建链接时,csvjdbc是把存放csv文件的文件夹作为数据库,文件夹中的每一个csv文件作为数据库中的表;
*/
/*
* Advanced Options
--------------------------------------------------------------------------------
The driver also supports a number of parameters that change the default behaviour of the driver.
These properties are:
separator
Used to specify a different column separator (Default is ',').
suppressHeaders
Used to specify if the first line contains column header information (Default is false; column headers are on first line).
fileExtension
Used to specify a different file extension (Default is ".csv")
charset
Used to specify a different than default charset encoding of input file (default is same VM default charset)
This following example code shows how these properties are used.
...
Properties props = new java.util.Properties();
props.put("separator","|"); // separator is a bar
props.put("suppressHeaders","true"); // first line contains data
props.put("fileExtension",".txt"); // file extension is .txt
props.put("charset","ISO-8859-2"); // file encoding is "ISO-8859-2"
Connection conn = Drivermanager.getConnection("jdbc:relique:csv:" + args[0],props)
Scrollable ResultSets
--------------------------------------------------------------------------------
The driver also now supports scrollable result sets.
This following example code shows how these are used.
...
Connection conn = Drivermanager.getConnection("jdbc:relique:csv:" + args[0],props)
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 0);
ResultSet results = stmt.executeQuery("SELECT ID,NAME FROM sample");
resulst.next();
...
results.first();
...
results.previous();
...
results.relative(2);
...
results.absolute(2);
...
*/
下面是user.csv文件内容:
ID CODE NAME MOBILE EMAIL VALID ORGANID LOGO |
33157 jia 用户甲 13625845145 0 24072 <BLOB> |
20600 1 高利强 13064878451 0 41 <BLOB> |
33163 yi 用户乙 13436963236 0 24072 <BLOB> |
33691 312 用户甲 13636363626 0 24072 <BLOB> |
78874 fsdf_gg 大幅度 15865895236 0 999 <BLOB> |
81322 1 中心 13533333333 1 45702 <BLOB> |
33879 YI 用户乙 13526262512 0 24072 <BLOB> |
33966 6 小李 13523075568 0 24072 <BLOB> |
81330 12234 中心 13533333333 1 45702 <BLOB> |
41637 12 12 15648551210 0 20583 <BLOB> |
41665 12 12 15648551210 0 20583 <BLOB> |
45829 hi hello 13589898545 0 24082 <BLOB> |
|
分享到:
相关推荐
Java读取excel,Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作。
使用java对csv文件进行读写操作的源代码,包含javacsv.jar
该文档主要介绍java如何操作csv文件,在这里介绍了一个工具包的使用,也黏贴了java对于读取csv文件和写入csv文件时,对于大数据量的处理和安排,有效的解决了处理大数据是内存溢出问题
文件中包括读取excel、csv文件,同时可以将读取出的文件内容保存在另外一个excel或csv中。注意excel2003和excel2007调用的jar包也不一样,需要将代码进行修改。2007版的将HSSF改成XSSF,2003相反。csv文件用XSSF
一个非常好用的csv文件操作工具
在我们需求中需要读取一个csv文件的数据,但是只有旧的csv的几列数据再新写入一个csv文件,而且在旧的数据有空的数据,需要判断旧的csv的空值。可能写的存在不完善!但是应用的需求里面还是能用的
在做项目的时候,发现使用POI无法解析以csv文件结尾的文件,虽然csv文件能用Excel打开,但是csv文件没有像Excel一样有规定的电子表格形式,故使用POI无法解析csv文件,在网上找了一下,发现java有提供java csv文件来...
利用Java swing 对csv和excel文件进行读写;并且可以同时读取5个文件的数量;通过关键字配置来读文件,还有就是通过POI CSVWriter等类的使用来对excel和csv等文件进行操作
下载javacsv-2.0.jar ,根据例子进行读写CSV操作。
读取与创建CSV文件,根据第三方jar包自动解析文件编码方式,相关jar包与使用说明
可读取复杂的csv文件,根据引号和逗号等分割读取所需要的数据信息。
VC++6.0编译通过的读写CSV文件的代码及实例 在VC++6.0中编译通过,实测可用 通用性很不错 包含CSV读取的实例
NULL 博文链接:https://qq-24665727.iteye.com/blog/2339850
matlab读取csv文件例程,读取指定路径下的所有csv文件
对CSV文件的读取和写入,简单操作,利用新手阅读和学习
本人以JAVA来实现以支付宝的账单表为例,实现JAVA读取CSV..csv是一种文件格式(如.txt、.doc等),也可理解.csv文件就是一种特殊格式的纯文本文件。即是一组字符序列,字符之间已英文字符的逗号或制表符(Tab)分隔。
java 解析csv文件例子,csv文件 中文乱码问题
通过引用【opevcsv】依赖,逐行读取数据 并转为Map,Object>格式,后续可以通过BeanUtil.MaptoBean()方法...csv文件必须带表头,bean对象命名符合驼峰格式; 通过此方法可以快速的将文件转换为程序可以操作的bean对象;
java动态实现生成CSV文件,java动态实现生成CSV文件,java动态实现生成CSV文件,java动态实现生成CSV文件,java动态实现生成CSV文件,java动态实现生成CSV文件,java动态实现生成CSV文件
NULL 博文链接:https://thinktothings.iteye.com/blog/1536044