Skip to main content

Posts

Showing posts from April, 2018

Convert MySQL Table into CSV using JAVA

Here's the code for converting available mysql table into csv. String filename ="D:/test.csv"; Connection conn=null; Statement stmt=null; try { //filewriter FileWriter fw = new FileWriter(filename); conn=DbConnection.getConnection(); //db connection function String query = "select * from tablename"; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query);  while (rs.next()) { fw.append(rs.getString(1));fw.append(','); fw.append(rs.getString(2)); fw.append(','); fw.append(rs.getString(3)); fw.append('\n'); } fw.flush(); fw.close(); conn.close(); System.out.println("CSV File is created successfully."); } catch (Exception e) { e.printStackTrace(); }