title: How do I output Data from MySQL using Java?
body: <p>I'm new at coding (JAVA) and I'm doing my project at the moment and I'm facing a serious issue, that's I couldn't output my DB as CSV.</p>
  <p>We need to use Java to control MySQl using Insert, find, update, and delete functions. I've got no problems with these functions and I've finished the codes.</p>
  <p>The last step is to output the data from MySQL and output the altered DB (after using update, delete...functions). Using MySQL to output the data might be easier but unfortunately we were required to use Java to do this.</p>
  <p>The thing is I can output the CSV file but the result isn't what I want. I expect to see something like this:
  <a href="https://i.stack.imgur.com/DdBVv.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  <p>but instead, this is what was saved to my local laptop:</p>
  <pre><code>    package MidteramWork2;
  
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  
  public class OutPutCSV {
  
      public static void main(String[] args) {
          String sql = &quot;select * from 台北市餐廳名單&quot;;
          try {
              Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
              Connection conn = DriverManager.getConnection(
                      &quot;jdbc:mysql://localhost:3306/Restaurant?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true&amp;characterEncoding=UTF-8&quot;,
                      &quot;root&quot;, &quot;password&quot;);
              PreparedStatement pstmt = conn.prepareStatement(sql);
              ResultSet rs=pstmt.executeQuery();
              while(rs.next()) {
                  FileOutputStream fo = new FileOutputStream(&quot;/Users/elenahu/Desktop/SaveFile/台北市餐廳名單.csv&quot;);
                  InputStream bin=rs.getBinaryStream(6);
                  int in;
                  while((in=bin.read())!=-1) {
                      fo.write(in);
                  }
                  fo.flush();
                  fo.close();
                  bin.close();
              }
              
      
  
      conn.close();
      } catch (SQLException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/jclBo.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Thank you very much.</p>