title: In Java program how to export data from database to text file
body: <p>I am trying to write a file fetching values from Database using java code. </p>
  
  <p>The <code>Resultset</code> values are retrieved from DB &amp; Then writing to a file.I am able to write to File ,But I am not getting the exact value ,instead getting the Junk Value.</p>
  
  <p>Code:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
          response.setContentType("text/html");    
          PrintWriter out = response.getWriter();    
  
          String n=request.getParameter("device");
  
                  Connection conn = null;  
                  PreparedStatement pst = null;  
                  ResultSet rs = null;  
  
                  String url = "jdbc:mysql://localhost:3306/";  
                  String dbName = "db5";  
                  String driver = "com.mysql.jdbc.Driver";  
                  String userName = "root";  
                  String password = "root";  
  
  
  
                   try {  
                          Class.forName(driver).newInstance();  
                          conn = DriverManager.getConnection(url + dbName, userName, password);  
  
  
  
                      File file = new File("D://Code2.txt");
                      FileWriter fw = new FileWriter(file.getAbsoluteFile());
                      BufferedWriter bw = new BufferedWriter(fw);
  
  
                      pst = conn.prepareStatement("select latitude,longitude,speed,heading,timeofday,timeofhours,gpssource  from nidgis where nidevid=?");
                      pst.setString(1, n);
                      rs = pst.executeQuery();
                      out.println("&lt;center&gt;");
                      out.println("&lt;h2&gt;LIVE  VEHICLE DATA &lt;/h2&gt;");
  
                      out.println("&lt;table border='1'&gt;");
                      while(rs.next())
                       {
                             out.println("&lt;tr&gt;");
  
                             out.println("&lt;th&gt;DAYS&lt;/th&gt;");
                             out.println("&lt;th&gt;HOURS&lt;/th&gt;");
                             out.println("&lt;th&gt;SPEED&lt;/th&gt;");
                             out.println("&lt;th&gt;HEADING&lt;/th&gt;");
                             out.println("&lt;th&gt;GPSDATA&lt;/th&gt;");
                             out.println("&lt;/tr&gt;");
  
                            out.println("&lt;tr&gt;");
  
                            out.println("&lt;td&gt;"+rs.getInt("timeofday") +"&lt;/td&gt;");
                            out.println("&lt;td&gt;"+rs.getInt("timeofhours") +"&lt;/td&gt;");
                            out.println("&lt;td&gt;"+rs.getInt("speed") +"&lt;/td&gt;");
                            out.println("&lt;td&gt;"+rs.getInt("heading") +"&lt;/td&gt;");
                            out.println("&lt;td&gt;"+rs.getInt("gpssource") +"&lt;/td&gt;");
  
                            out.println("&lt;/tr&gt;"); 
  
                            bw.write(rs.getInt("latitude"));  
                            bw.write(rs.getInt("longitude"));
  
  
                      }
                      out.println("&lt;/table&gt;");
  
                      out.println("&lt;/center&gt;"); 
                      bw.close();                   
                  }
                  catch (Exception e) 
                  {  
                      System.out.println(e);  
                  } 
                  finally 
                  {  
                      if (conn != null)
                      {  
                          try {  
                              conn.close();  
                          } catch (SQLException e) {  
                              e.printStackTrace();  
                          }  
                      }  
                      if (pst != null) {  
                          try {  
                              pst.close();  
                          } catch (SQLException e) {  
                              e.printStackTrace();  
                          }  
                      }  
                      if (rs != null) {  
                          try {  
                              rs.close();  
                          } catch (SQLException e)
                          {  
                              e.printStackTrace();  
                          }  
                      }  
                  }  
  </code></pre>
  
  <p>I am Writing only Latitude &amp; Longitude to File ,Rest Displaying it in a table.For Both data type in DB is DOUBLE.</p>
  
  <pre><code>                      bw.write(rs.getInt("latitude"));  
                        bw.write(rs.getInt("longitude"));
  </code></pre>
  
  <p>O/P that I am getting code2.txt is some string values :
  M
  M</p>