简体   繁体   English

CSV,JAVA和HSQLDB

[英]CSV , JAVA & HSQLDB

im really new in java programming 我在Java编程中真的很新

i want to ask about reading a CSV in java and then upload it to HSQLDB . 我想问一下在Java中读取CSV,然后将其上传到HSQLDB的问题 i have been reading & try the example of solution through tutorial but still not really understand. 我一直在阅读并尝试通过教程解决方案的示例,但仍然不是很了解。 from my readings, most had suggest of using OpenCSV but 从我的阅读中, 大多数建议使用OpenCSV,

  1. can i read a csv with delimiter or nested delimiter with opencsv? 我可以读取带有定界符的csv或带有opencsv的嵌套定界符吗?
    • opencsv reads my *.csv file in different way with multiple line in one column. opencsv以不同的方式读取我的* .csv文件,在一列中有多行。 all i want it to be read in a single row. 我想要的所有内容都可以在一行中读取。

below is the example 下面是例子

B03BB510001T2001,,CHILDREN'S CHEWABLE MULTI VITAMINS*,,""PANTOTHENIC ACID 7.5MG," "PARA AMINO BENZOIC ACID (PABA) 250MCG," "FOLIC ACID 30MCG," "BIOTIN 50MCG," "THIAMINE MONO-NITRATE + B6 + B12 6.6MG," "CHOLINE 1MG," "VIT A + VIT E + VIT C REFER FILE," "INOSITOL 1MG"",,""TABLET, CHEWABLE"",,,,B03,REGISTERED,75MG" B03BB510001T2001,儿童可咀嚼的多种维生素*,“”泛酸7.5毫克“,” PARA氨基苯甲酸(PABA)250MCG“,”叶酸30MCG“,”生物素50MCG“,”硫胺酸+ B6 + B12 6.6“ MG,“” CHOLINE 1MG“,”维生素A +维生素E +维生素C参考文件“,” INOSITOL 1MG“”,“”平板电脑咀嚼“” ,, B03,registered,75MG“

BUT i want it to be displayed in a single row 但是我希望它显示在一行中

  1. how can i insert directly from csv file into database? 如何直接从csv文件插入数据库?
    • i am currently have to used HSQLDB [standalone server]. 我目前必须使用HSQLDB [独立服务器]。 do i have to used third party tools to do this 我是否必须使用第三方工具来做到这一点

**i only familiar reading & upload from csv into database using PHP & MYSQL not java &hsqldb **我只熟悉使用PHP和MYSQL从CSV读取并上传到数据库而不是java&hsqldb

hope you can suggest & help me. 希望您能提出建议并帮助我。

Here is a sample script I have used to load data into HSQL per fredt. 这是一个示例脚本,我已使用它在每个目录中将数据加载到HSQL中。 You will need to have a persistant DB to use a text table. 您将需要有一个持久数据库才能使用文本表。 If you run in the Database Manager just copy and execute one statement at a time. 如果您在数据库管理器中运行,则只需一次复制并执行一条语句。 You can also use sqltool (the sql script is in the same folder as the DB): 您还可以使用sqltool(sql脚本与DB在同一文件夹中):

\i demotexttable.sql

Here is the demotexttable.sql 这是demotexttable.sql

/* file.txt needs to be in the same folder as the database
   first line of file is a header, others lines are data
--start file.txt
Col1|Col2|Col3
abcdef|My Company Name|0
--end file.txt
*/

--create the schema
create schema imp;

--create the normal table
create cached table imp.normalTable (
       Col1 char(6),
       Col2 nvarchar(200),
       Col3 int
);

--create the table that table
create text table imp.textfiletable (
       Col1 char(6),
       Col2 nvarchar(200),
       Col3 int
);

--setup the text file table
set table imp.textfiletable source "file.txt;ignore_first=true;fs=|";

--insert data from text file into the `normal` table
INSERT INTO imp.normalTable (Col1, Col2, Col3) select Col1, Col2, Col3 from imp.textfiletable;

--we are done with the text file table
drop table imp.textfiletable;
commit;

I found a few errors in my earlier post, I have cleaned them up in this latest mod. 我在较早的帖子中发现了一些错误,我已在此最新版本中对其进行了清理。

You can load a CSV file directly into HSQLDB. 您可以将CSV文件直接加载到HSQLDB中。

There are two different ways: 有两种不同的方式:

  1. Use HSQLDB's TEXT TABLE feature. 使用HSQLDB的TEXT TABLE功能。 Your file shouldn't need any special setting as the delimiter is the default. 您的文件不需要任何特殊设置,因为分隔符是默认设置。
  2. Use HSQLDB's normal tables and load the CVS with SqlTool, which is an additional jar supplied with HSQLDB. 使用HSQLDB的普通表并使用SqlTool加载CVS,SqlTool是HSQLDB附带的一个jar。 SqlTool can be used not only with HSLQLDB, but also with MySQL and other databases. SqlTool不仅可以与HSLQLDB一起使用,而且还可以与MySQL和其他数据库一起使用。

See here for details: 详细信息请参见此处:

http://hsqldb.org/doc/2.0/guide/texttables-chapt.html http://hsqldb.org/doc/2.0/guide/texttables-chapt.html

http://hsqldb.org/doc/2.0/util-guide/sqltool-chapt.html#sqltool_csv-sect http://hsqldb.org/doc/2.0/util-guide/sqltool-chapt.html#sqltool_csv-sect

As you know that CSV is nothing but a "Comma-Separated-Value". 如您所知,CSV只是“逗号分隔值”。 The reason why others suggested OpenCSV is because its has a commercially friendly license. 其他人建议使用OpenCSV的原因是因为它具有商业上友好的许可证。 Though apart from that one can use the normal way to retrieve the values.Which is read the values using FileInputStream.Please find the link: 尽管除此之外,可以使用常规方法来检索值,但可以使用FileInputStream读取值,请找到链接:

http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml

After that parse the individual String using delimiter. 之后,使用定界符解析单个String。 eg: String[] tokens = wholeString.split(delimiter); 例如:String []令牌= WholeString.split(delimiter);

Now simply using the jdbc connectivity through using java.sql package one can easily store the values in hsql database.Please check out the link: 现在只需通过使用java.sql软件包使用jdbc连接就可以轻松地将值存储在hsql数据库中。请查看链接:

http://www.hsqldb.org/doc/guide/apb.html http://www.hsqldb.org/doc/guide/apb.html

Hope this helps you out; 希望这可以帮助你; Regards, 问候,

Anand 阿南德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM