Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:54:59+00:00 2026-06-13T17:54:59+00:00

i have a mysql db, and i have one table which has many columns

  • 0

i have a mysql db, and i have one table which has many columns including a timestamp one. i want to read the table and write it to a csv file. then i will read from the csv file the data and insert it in another db and table. all these are done through java.
I can read the table date and the timestamp from the first db, but the timestap is not written correctly into the csv file.. only the last few digits are written. for example if i have a timestamp 2012-11-01 02:18:00.0 only the 18:00.0 will be written to the csv. All the other stuff work fine. For example if i hard write a timestamp i can write it to the second db. there;s the code for reading mysql and writing to csv :

                    con = DriverManager.getConnection("jdbc:mysql://localhost/campdb", "...", "...");

                    stmt = (Statement) con.createStatement();
                    fw = new FileWriter(filename);
                    PrintWriter a = new PrintWriter(fw);
                    ResultSet res = stmt.executeQuery("SELECT * FROM CAMPREQ;");
                    while (res.next()) 
                    {
                        String ID = res.getString(1);
                        java.sql.Date date = res.getDate(2);
                        //java.util.Date jdate = new java.util.Date(date.getTime());
                        String company = res.getString(3);
                        String time = res.getString(4);
                        String origin = res.getString(5);
                        String dest = res.getString(6);
                        String timestamp = res.getString(7);
                        System.out.println(timestamp);
                        a.print(ID);
                        a.print(',');
                        a.print(date);
                        a.print(',');
                        a.print(company);
                        a.print(',');
                        a.print(time);
                        a.print(',');
                        a.print(origin);
                        a.print(',');
                        a.print(dest);
                        a.print(',');
                        a.print(timestamp);
                        a.print('\n');
                        a.flush();

                    }

The System.out command prints the timestamp as it should be written, however it is not written correctly in the csv 🙁
thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:55:01+00:00Added an answer on June 13, 2026 at 5:55 pm

    try using the code below, it works for most databases and properly takes all datatypes correctly with correct formatting. This should solve your problem not just for mysql but for any other database

     public static StringBuffer resultSetToStringCsv(ResultSet resultSet) throws Exception{
    StringBuffer stringwriter = new StringBuffer();
    
    ResultSet rs = resultSet;
    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();
    
    //Write column names
    for(int i=1;i<=numberOfColumns;i++){
     stringwriter.append(rsmd.getColumnName(i));
     stringwriter.append(',');
    }
    stringwriter.append('\n');
    //Write rows
    while(rs.next()){
     for(int i=1;i<=numberOfColumns;i++){
      try{
       stringwriter.append(""+rs.getObject(i));
      stringwriter.append(',');
      }catch (Exception e) {
       stringwriter.append("null");
     stringwriter.append(',');
    }
    }    
     stringwriter.append('\n');
    }
    

    You can check http://bmukorera.blogspot.com/2012/11/resultset-query-to-string-generating.html for full code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a large mysql database table in which one column contains values ranging
I have a classified_id variable which matches one document in a MySql table. I
I have a huge table from one mysql db, I want to create a
by default I have one column in MySQL table to be NULL. I want
I have two tables - 'business' and 'business_contacts'. The business_contact table has a many-to-one
I have a table which has no primary key and I can't add one
I have a legacy mysql database and there's this table which has a few
In a MySQL database I have two tables linked in a join. One table
What I have is two tables inside of a mysql database. One table contains
I have a mysql table that have several records linked to one particular tag.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.