How to insert specific column of Excel into Database - java

I have an Excel sheet with 200+ column names and each column has a header name. I have to fetch all the header names first from the Excel sheet using Java and pass them into Oracle DB. The DB returns only a few header names, say 150, which I need to insert into a table.
My problem is that I am unable to fetch the specific header names (150 out of 200) from the Excel sheet and their values to insert into db.
I tried using a CSV file but it cannot fetch specific column values. Please suggest any solution that I need to follow here.

1) Have you tried to read the csv file like regular text file, then extract the first line, the header row, into a String? Then separate the string by "comma" and you should have an array of string with column headers.
2) Make sure you don't have any "unusual" characters such as "dash", "comma", "quote" in your column header description.
Gook Luck

Related

CSV file column data with comma separated is displaying as number when opened in excel (JAVA)

Created csv file with name text.csv (Microsoft Excel Comma Separated Values File (.csv) type) with column data in the column as 55,666,666,7777,9999,7777,999,777 through java code. If I try to open the file by 5.56666667777999E+25. Even by changing the format of the cell, the data is displaying in the same format. But if I use Text Import Wizard and selecting the Column data format as 'Text' I am able to see the data in the way I created while writing the CSV file.
Is that something I can do through code, or is this related to Excel formatting. As when we provide the same to client, they won't change the formatter the column in excel. They just double click and open the file.

Remove special characters in CSV header row and concatenate

I am trying to read a CSV file with headercolumns containing special characters and spaces. I need to push the CSV data to MySQL which throws syntax error when i create the table with predefined column names.
I am using Apache commons.csv. I tried to give my CSV format as follows:
CSVFormat format = CSVFormat.MYSQL.withSkipHeaderRecord().withHeader(headerstring).withDelimiter(',').withQuote('"');
headerstring is-
"auto_videoId", "videoId", "join_key", "eventId", "name", "LocationShelf", "Action", "Interaction", "TablesAvailability", "timeSpent"
Instead of taking it as individual column name entries, it is read as one string and throws error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"auto_videoId", "videoId", "join_key", "eventId", "name", "LocationShelf", "Acti' at line 1
P.S- CSV already had the same column names but with special characters.

Can read from sample sheet but now from own sheet

I'm trying out Google Sheets API from a Java application. I've accessed the file mentioned in the tutorial but I cannot access any file that I have created on my own.
This is the code I'm using:
String spreadsheetId = /*omitted*/;
String range = "Class Data!A1:B";
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
I created a spreadsheet manually in Drive, filled A1:B with strings and copied the id from the URL, looking something like "1IeoY5jY3Su86x1uvgc1yJqEEU-6dd6FdUKo8Yf5J73k" (not an actual ID).
This generates an error 400 Unable to parse range: Class Data!A1:B
I'm guessing that this means that it cannot access my spreadsheet, since the sheet is filled with strings "ab" in these cells.
The sample code has a spreadsheetId that refers to some kind of public document and it is working for that document. I am guessing that I am doing something fundamentally wrong here. I have verified that the document is created with the same credentials as the ones I'm using for the Java application. Any ideas?
Class Data is the name of the sheet in the sample sheet. You should substitute that with the name of the sheet in your spreadsheet. You can see the name of the sheet on the tab near the bottom of the screen. It defaults to Sheet 1.
A1:B is the number of rows and columns to read. It is saying to read all data in columns A and B from rows 1 to the last row. You should swap that with the rows and columns you want to read in your sheet.

Is there a way to mark metadata as read only in Excel

We have a use case to generate Read-Only(such as the document Creation date or Number of characters) metadata for Msexcel file.
But whatever metadata we generate using java(apache poi) is editable.
I tried adding metadata in custom , but this also editable .
Can some one hel me out in finding a way to mark part of metadata as readonly in excel file

Stuck inserting rows from a file into Database

I want to insert rows from excel sheet into SQL database using java or JSP. Initially I have connected both the database using ODBC. But I want to take an excel file as input from user. For this I created a file browser and upload the file.
How can I connect the uploaded file with ODBC connection?
How can I query the uploaded file?
If I use Apache POI to parse Excel file, I will get column names and that also get inserted. How can I avoid this?
a) How can I insert the rows with particular column name, not from column number?
Please provide me the code snippet
1) How can i connect the uploaded file with ODBC connection?
You will have to write a service to map excel to DB if thats what you are asking?
To get the values from the excel file , you should use Apache POI.
And the solution to the problem that column names are also inserted is :
If the column names are in row 1 , then start a loop from 1 instead of 10.
int i=1;
do{
{
HSSFRow row1 = worksheet.getRow(i);
HSSFCell cellA1 = row1.getCell((short) 0);
String a1Val = cellA1.getStringCellValue().toString();
System.out.println(a1Val);
i++;
// And if you want to enter this value in sql database , write a odbc connection and insert query to sql database.
}
}while(a1val.!equals("");

Categories