Reputation: 181
import java.io.*;
import java.sql.*;
public class CsvF {
public static void main(String[] args) {
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","sandp");
conn.setAutoCommit(false);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from tet where to_char(S_DATE,'HH24') = '23'");
ResultSetMetaData rsmd = rs.getMetaData();
FileWriter cname = new FileWriter("D:\\asd.csv");
BufferedWriter bwOutFile = new BufferedWriter(cname);
StringBuffer sbOutput = new StringBuffer();
sbOutput.append("S_DATE");
bwOutFile.append(sbOutput);
bwOutFile.append(System.getProperty("line.separator"));
System.out.println("No of columns in the table:"+ rsmd.getColumnCount());
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{
String fname = rsmd.getColumnName(i);
}
System.out.println();
while(rs.next())
{
System.out.print(rs.getString(1));
bwOutFile.append(rs.getString(1));
bwOutFile.append(System.getProperty("line.separator"));
bwOutFile.flush();
System.out.println();
}
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
System.out.println("Unable to connect to database" +e);
}
}
}
here i am getting data into asd.csv file as follows= 2015-2-26.23.21. 0. 0
2015-2-26.23.43. 0. 0
2015-2-27.23.28. 0. 0
2015-2-27.23.50. 0. 0
2015-3-1.23.19. 0. 0
2015-3-1.23.41. 0. 0
but i want to get data as following format = 2015-02-26 23:21
2015-02-26 23:43
2015-02-27 23:28
2015-02-27 23:50
2015-03-01 23:19
2015-03-01 23:41
i am getting an extra dot(".") in the output... what i am doing wrong? i have create a table named tet with coloumn(S_DATE) where S_DATE is timestamp...can anyone help me?
Upvotes: 0
Views: 8085
Reputation: 706
Use rs.getTimestamp() instead of rs.getString() and convert your timestamp to string using SimpleDateFormat.
TimeStamp ts= rs.getTimeStamp(1);
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String yourFormatedDate = formater.format(ts);
Upvotes: 1