0
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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.