I want to write the result of an sql query to a csv or excel file and save it in a particular folder.I would like to know if this can be achieved using a java program which can be reused for any sql query result.I would also like to know if this can be used for different types of databases(oracle,mysql,MS sql server etc).I plan to attach the saved file to an email(is this possible directly exporting a sql query result to an email).Please help.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- How to maintain order of key-value in DataFrame sa
For this to work you need to work write a small code that can take up any query and any driver . The first input should be the driver name as the input to the software that you are writing. Then the software you are writing should be in a position to execute any SQL given to it and give out only rows and columns.
The next task comes to parse the ResultSet that comes from the JDBC of java application. Either you want to write the results into CSV file or EXCEL is based on how good you have the java api to do that.
Writing the output into the CVS is easy and not trival. I have not worked on exporting the data into Excel. I am sure you find jars for that.
Yes!
You can connect to the different database types using jdbc and then create an Excel with the results (Seen here).
This is my solution. Code to insert in the main class:
The core class:
This is the class DBConn for Connection to database
This is the class for retrieves data from table to resultset and writes to csv file
This is the excel data sheet
input excel file containing records Hi this is the solution you need 3 files 1.input thread 2.output thread 3.data structure 4.main 1.input thread to read the excel and output thread to write the sql out put 2.data structure is to hold and transfer the data
(InputThread.java)
(OutputThread.java)
(Main.java)
(DataStructure.java)
//This class represents the data structure to hold and transform input //data as a linked list of sql statements
output result
You can use JDBC to fetch the records from DB in java and then use Apache POI for exporting the data to CSV/Excel.
Additionally, you can use the desktop API of java to send email using your default email client.
It's difficult to export result set data from any tool.
ex: while exporting result set data to .csv file it does not export properly when data contains (,)
please refer below java code :
it works perfectly with any any query input and all type of data in result set