简体   繁体   English

使用java解析从mysql到csv的时间戳

[英]parsing timestamp from mysql to csv using java

i have a mysql db, and i have one table which has many columns including a timestamp one. 我有一个mysql数据库,我有一个表有许多列,包括时间戳一。 i want to read the table and write it to a csv file. 我想读取表并将其写入csv文件。 then i will read from the csv file the data and insert it in another db and table. 然后我将从csv文件中读取数据并将其插入另一个数据库和表中。 all these are done through java. 所有这些都是通过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. 我可以从第一个数据库读取表日期和时间戳,但时间戳没有正确写入csv文件..只写入最后几位数字。 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. 例如,如果我有时间戳2012-11-01 02:18:00.0只有18:00.0将被写入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 : 那里有用于读取mysql和写入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 System.out命令打印应该写入的时间戳,但是在csv中没有正确写入:(谢谢

Don't do things in Java that MySQL can do by itself. 不要用Java自己做的事情。

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

String q = 
  "SELECT * INTO OUTFILE '" + filename + "'"
  + " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'"
  + " LINES TERMINATED BY '\n'"
  + " FROM " + tablename;

stmt.execute(q);

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 您可以查看http://bmukorera.blogspot.com/2012/11/resultset-query-to-string-generating.html获取完整代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM