Generating CSV file with column names and data from the MySQL with JAVA

Hi all,
Give small example on ...
How can I add column names and data to a CSV from from MySQL.
like
example
sequence_no, time_date, col_name, col_name
123, 27-apr-2004, data, data
234, 27-apr-2004, data, data
Pls give small exeample on this.
Thanks & Regards
Rama Krishna

Hello Rama Krishna,
Check this code:
Example below exports data from MySQL Select query to CSV file.
testtable structure
CREATE TABLE testtable
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
text varchar(45) NOT NULL,
price integer not null);
Application takes path of output file as an argument.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class automateExport {
    public static void main(String[] args) {
        DBase db = new DBase();
        Connection conn = db.connect(
                "jdbc:mysql://localhost:3306/test","root","caspian");
        if (args.length != 1) {
            System.out.println(
                    "Usage: java automateExport [outputfile path] ");
            return;
        db.exportData(conn,args[0]);
class DBase {
    public DBase() {
    public Connection connect(String db_connect_str,
            String db_userid, String db_password) {
        Connection conn;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(db_connect_str,
                    db_userid, db_password);
        } catch(Exception e) {
            e.printStackTrace();
            conn = null;
        return conn;
    public void exportData(Connection conn,String filename) {
        Statement stmt;
        String query;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            //For comma separated file
            query = "SELECT id,text,price into OUTFILE  '"+filename+
                    "' FIELDS TERMINATED BY ',' FROM testtable t";
            stmt.executeQuery(query);
        } catch(Exception e) {
            e.printStackTrace();
            stmt = null;
Greetings,
Praveen Gudapati

Similar Messages

  • Reading MS Project column names and data on the fly from a selected View

    Hi guys,
    I have several views on my project file (MSPROJECT 2010) and I want to build a macro so that;
    1. User can select any view ( Views can have diffrent columns and the user may add new columns as well)
    2. User runs the Macro and all the coulmns along with the tasks displayed in the view will be written to a excel file. ( I don't want to build several macro's for each view, I'm thinking of a common method which would work for any selected view)
    The problem I'm facing is that how will i read the column names and data for a particular view on the fly without hard coding them inside the vba code ?
    The solution needs to work on a master schedule as well.
    Appreciate your feedback.

    Just to get you started the following code writes the field name and data for the active task to the Immediate window.
    Sub CopyData()
    Dim fld As TableField
    For Each fld In ActiveProject.TaskTables(ActiveProject.CurrentTable).TableFields
    If fld.Field >= 0 Then
    Debug.Print Application.FieldConstantToFieldName(fld.Field), ActiveCell.Task.GetField(fld.Field)
    End If
    Next fld
    End Sub
    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

  • How to list column names and data types for a given table using SQL

    I remember that it is possible to use a select statement to list the column names and data types of databaase tables but forgot how its done. Please help.

    You can select what you need from DBA_TAB_COLUMNS (or ALL_TAB_COLUMNS or USER_TAB_COLUMNS).

  • I am trying to edit track names and genres from the library in iTunes, but the Get Info window has everything in grey, so I cannot edit anything. How do I edit?

    I am trying to edit track names and genres from the library in iTunes, but the Get Info window has everything in grey, so I cannot edit anything. How do I edit? Any ideas?

    I have the same problem since I installed iTunes 11.1.0.126.

  • My moms computer died we are putting the old drive into the new computer as a slave drive. how do I transfer all the bookmarks, user names and passwords from the old drive to the new drive?

    <blockquote>Locking duplicate thread.<br>
    Please continue here: [[/questions/889318]]</blockquote>
    Hello,
    Sorry if this posted twice I am NEW. I thought I could just go back to what I typed and add to it. Mom had a computer it died they are putting in the old drive on the new computer as a slave. What are the steps I need to do to transfer ALL the bookmarks, user names and passwords onto the new computer. We will have 3 desktops do we have to do the same steps on each desktop? I do not want to re enter all the user names and passwords on the new hard drive. Old computer was Windows XP the new computer is Windows 7 Kathryn Schwartz

    Duplicate - https://support.mozilla.com/en-US/questions/889318

  • How to get the document type, name and revision from the search page cv04n

    Hello,
    After performing cv04n and getting a list of documents satisfying the search criteria, how can I then get the document type, name and revision of the selected document using ABAP?
    Thanks

    HI,
    IN table DRAW... u have Document type, Version and document number.
    In Table DRAT also u get Document Type, Version, Number and Description of Doucment.
    Regards
    SAB

  • Extract image and Features from the Catalog via JAVA API

    Hello,
    I would like to Extract image from the Catalog via JAVA API, Can anybody help on that? I also tried to extract the Features field form the Catalog but results in the error "Features field not found" Any ideas what could have wrong?
    Many thanks,
    Dharmi

    Hello,
    Can anybody tell me where i can find the latest JAVAAPI reference guide? I found the one for MDM 5.5 SP 1 but that also refers to the last parameter of the CatalogCache.Init as int and not string.
    I looked up in service.sap.com/instguides -> SAP Netweaver -> Release 4 -> Installation and there only following 3 files are there for MDM 5.5 SP2
    MDM 5.5 SP02 - Configuration Guide  SAP MDM
    MDM 5.5 SP02 - Installation Guide   SAP MDM
    MDM 5.5 SP02 – ERP-MDM Field Mapping and Check Tables
    Regards,
    Dharmi
    Message was edited by: Dharmi Tanna
    Message was edited by: Dharmi Tanna

  • Generating .csv file with the datas from the database

    Hi Java experts,
    Ihave a current assignment to generate a .CSV file using java getting values from database. The output CSV file may have more than one row and each row should have a end of line delimiter. Any help in giving a template for generating a CSV file accessing from database is greatly appreciated.

    Ihave a current assignment to generate a .CSV file
    using java getting values from database.well, I hope you learn something doing it yourself...
    The output
    CSV file may have more than one row and each row
    should have a end of line delimiter. Any help in
    giving a template for generating a CSV file accessing
    from database is greatly appreciated.How about looking for a JDBC and a file/print stream tutorial?

  • Detecting column name and value from a xml

    Dear all,
    I need to create a procedure to get a xml file. The xml file may contain a table data or procedure parameter value. Off course it will also contain a flag if the data is of a procedure or table nnd then it's table or procedure name, column name(in case of table), and value. The procedure will receive the xml file and it does not know the column name in case of table or parameter name in case of procedure and then its value. i.e i have to parse the xml file without knowing the column name for table or parameter for procedure. Then i may populate data to my need. How can i do that? Please help me.
    BR.
    Tarik
    Edited by: Tarik_kuet on Sep 23, 2008 2:32 PM
    Edited by: Tarik_kuet on Sep 23, 2008 2:32 PM

    You need to cast your data into an xmltype and look for the existence of nodes for tables, proceduresm etc
    with xmldata
    as
      (select  xmltype('<?xml version="1.0" encoding="UTF-8"?>
    <Sample UniqueID="KWID_AP" >
         <Description>Sample Application</Description>
         <Table Name="SampleTable" />
         <Procedure Name="SampleProc" />
    </Sample>') xml
    from dual)
    select * from xmldata
    where existsnode(xml,'/Sample/Procedure')=1

  • How to capture user name and date in the database

    How to capture the person name who edits the application and the date of edit in the database...
    Pallavi

    Hi
    There are substitution strings you can use for this purpose.
    1.APP_USER ------is the current user running the application
    2.SYSDATE --------represents the current date on the database server
    APP_USER Syntax
    Bind variable------ :APP_USER
    PL/SQL------- V('APP_USER')
    Substitution string---------- &APP_USER.
    SYSDATE_YYYYMMDD Syntax
    Bind variable------- :SYSDATE_YYYYMMDD
    Direct PL/SQL------- APEX_APPLICATION.G_SYSDATE (DATE DATATYPE)
    PL/SQL-------- V('SYSDATE_YYYYMMDD')
    Your application will be based on a table with primary key column. You create a 'before update trigger' on that table and add columns 'UPDATED_ON' and 'UPDATED_BY' to that table. Add the following to that trigger:
    :NEW.UPDATED_ON := SYSDATE;
    SELECT V('APP_USER') INTO :NEW.UPDATED_BY FROM DUAL;
    -Priyanka

  • How can I batch process files to change name and extention in the latest photoshop cc ?

    Hi,
    The batch process function has changed design recently and I cannot get it to change file names in photoshop cc 2014.
    I keep getting new layers in the first file to open instead of a renamed batch.
    I would also like to convert files to jpeg and resize ( at the same time ? )
    thank you for help in advance,
    christos

    Can't help you with CC, but you cannot change files types by merely changing the extension, regardless of program or version.

  • How can I hide the user name and password from the url address?

    Good afternoon every body,
    I have a form running with Oracle9i Developer Suite Release 2 and when I run the form on the web it shows the user name and password of my data base. Can anyone of you please help me to hide the user name and password, if there's any way of course?.
    Thanks a lot!!.

    Luis,
    Then, as inolau's notice, create logon screen (or use the default one) and force the users to logon at runtime. Do not pass username/password as parameters.
    inolau,
    True that if the connection is specified in the config it will be the same for everyone. However, every case is different. For example one of our apps gets S3 credentials (from non-Oracle S3) as session parameters. It uses this common db connection to validate some stuff with the database, read security definitions and then it re-connects the forms using the credentials.

  • Webservice​s remove service name and port from the URL

    Hi community,
    so I have a server PC called "superlab"(*) and I also have a webservice called "superlab"(*). Now the users need to enter superlab:8001/superlab to the browser to access the service. I'd like them simply enter superlab.
    So my questions:
    1. how can I remove the service name from the URL
    2. how can I remove the port number from the URL
    3. (not related but I have difficulty to understand what it the point in "publishing" a webservice while simply starting it does the job as well)
    thanks!
    *Not the real name

    Luis,
    Then, as inolau's notice, create logon screen (or use the default one) and force the users to logon at runtime. Do not pass username/password as parameters.
    inolau,
    True that if the connection is specified in the config it will be the same for everyone. However, every case is different. For example one of our apps gets S3 credentials (from non-Oracle S3) as session parameters. It uses this common db connection to validate some stuff with the database, read security definitions and then it re-connects the forms using the credentials.

  • Fetch host name and url from the payload

    Hi,
    I want to configure my HTTP reciever channel, but i need the details to be fetched from payload.
    The details are like Target Host ,URL etc.
    Any help is appreciated.
    Regards,
    Bhanu.

    Hello
    I want to configure my HTTP reciever channel, but i need the details to be fetched from payload.
    Since your URL is dynamic and there is a chance that the protocol is https, it is better to use SOAP Receiver Adapter instead.
    Make sure that you check Do not use SOAP Envelope to make it behave like a Plain HTTP Receiver.
    http://help.sap.com/saphelp_nwpi71/helpdata/EN/29/5bd93f130f9215e10000000a155106/frameset.htm
    From the link, URL is TServerLocation, etc... Use dynamic configuration via UDF to fulfill your requirements.
    Hope this helps,
    Mark

  • How can I get the Organizer in Elements 12to display the file name and date of the thumbnail images?

    The View menu option to display the File Name is grayed out and thus I cannot display the file name.

    Hi,
    You need to go to View menu and check Details as well.
    You may even have to make the thumbnails larger by using the zoom slider at the bottom
    Good luck
    Brian

Maybe you are looking for