gpt4 book ai didi

java - 使用带引号从java读取csv文件,但我想要不带引号

转载 作者:行者123 更新时间:2023-11-29 19:26:36 26 4
gpt4 key购买 nike

我正在从java读取CSV文件并插入到数据库mysql中,在读取csv文件时,我正在获取带引号的数据,但我想读取不带引号的数据,任何人都可以帮助我......!

"03_lab3_lsi1_kw","2017年6月2日12:02:13",7243.732,1,42772501540.0694

这是我的代码:

package com.savecsvtopostgres;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SaveCsvDataToPostgres {

List<OauthResponse> responseList = new ArrayList<OauthResponse>();

public static void main(String[] args){

BufferedReader csvBuffer = null;
List<OauthResponse> csvlist = new ArrayList<OauthResponse>();

try {
String csvLine;
csvBuffer = new BufferedReader(new FileReader("C:\\Users\\DVSabareesh\\testresults.csv"));

while ((csvLine = csvBuffer.readLine()) != null) {
System.out.println("Raw CSV data: " + csvLine);
OauthResponse oauthResponse = csvtoArrayList(csvLine);
csvlist.add(oauthResponse);
}
insert(csvlist);
System.out.println("Converted ArrayList data: " + csvlist.size() + "\n");
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (csvBuffer != null) csvBuffer.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}

// Utility which converts CSV to ArrayList using Split Operation
public static OauthResponse csvtoArrayList(String csvLine) {
OauthResponse oauthResponse = new OauthResponse();

if (csvLine != null) {
String[] splitData = csvLine.split(",");
int datalength = splitData.length;
System.out.println(datalength);

oauthResponse.setId(splitData[0]);
oauthResponse.setValue2(splitData[1]);
oauthResponse.setTicketName(splitData[2]);
oauthResponse.setTicketResponse(splitData[3]);
oauthResponse.setResponseId(splitData[4]);
oauthResponse.setResponseMessage(splitData[5]);
oauthResponse.setOperations(splitData[6]);
oauthResponse.setFileType(splitData[7]);
oauthResponse.setBooleanValue(splitData[8]);
oauthResponse.setId2(splitData[9]);
oauthResponse.setId3(splitData[10]);
oauthResponse.setOauthUrl(splitData[11]);
if(datalength == 13){
oauthResponse.setValue13(splitData[12]);
}else{
oauthResponse.setValue13("");
}
}
return oauthResponse;
}

// Utility which used to insert data into postgres
public static void insert(List<OauthResponse> responseList) throws SQLException {
Connection dbConnection = null;
PreparedStatement ps = null;
try {
dbConnection = getDBConnection();
dbConnection.setAutoCommit(false);
String query = "INSERT INTO csvvalues("
+ "testgeneratorid, "
+ "ticketvalues1, "
+ "ticketname, "
+ "ticketresponsecode, "
+ "ticketresponsemessage, "
+ "testcaseresults, "
+ "resultformat, "
+ "booleanresult, "
+ "ticketvalues2, "
+ "ticketvalues3, "
+ "oauthurl, "
+ "ticketvalues4, "
+ "ticketvalues5) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = dbConnection.prepareStatement(query);

System.out.println("Records are getting inserted into TestSuiteResults.TestCase table!");

for(OauthResponse res : responseList){
ps.setString(1,res.getId());
ps.setString(2,res.getValue2());
ps.setString(3,res.getTicketName());
ps.setString(4,res.getTicketResponse());
ps.setString(5,res.getResponseId());
ps.setString(6,res.getResponseMessage());
ps.setString(7,res.getOperations());
ps.setString(8,res.getFileType());
ps.setString(9,res.getBooleanValue());
ps.setString(10,res.getId2());
ps.setString(11,res.getId3());
ps.setString(12,res.getOauthUrl());
ps.setString(13,res.getValue13());
ps.addBatch();
}
ps.executeBatch();
dbConnection.commit();
System.out.println("Record is inserted into DBUSER table!");
} catch (SQLException e) {
e.getNextException().printStackTrace();
dbConnection.rollback();
} finally {
if (ps != null) {
ps.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}

// Utility which gets connection to postgres
public static Connection getDBConnection() {
Connection dbConnection = null;
try {
//Class.forName("org.postgresql.Driver");
//dbConnection = DriverManager.getConnection(
//"jdbc:postgresql://localhost:5432/Database4TesSuitetResults", "postgres", "santhosh");
Class.forName("com.mysql.jdbc.Driver");
dbConnection = DriverManager.getConnection(
"jdbc:mysql://localhost:3309/csv","root","system");

System.out.println("DB connected");
return dbConnection;
} catch (SQLException | ClassNotFoundException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}

最佳答案

使用OpenCSV(.jar) CsvFileReader读取csv文件中的数据

    CSVReader csvfile = new CSVReader("FilePath\\.csv");

String[] csvdata = csvfile.readnext(); // To read the row one by one
// Read all row from csv
List<String[]> csvdata1 = csvfile.readAll(); //To Read all data from csv file
String[] csvdata = csvdata1.get(0);//Read first row

关于java - 使用带引号从java读取csv文件,但我想要不带引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42081550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com