0

I am trying to write a file fetching values from Database using java code.

The Resultset values are retrieved from DB & 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.

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("<center>");
                    out.println("<h2>LIVE  VEHICLE DATA </h2>");

                    out.println("<table border='1'>");
                    while(rs.next())
                     {
                           out.println("<tr>");

                           out.println("<th>DAYS</th>");
                           out.println("<th>HOURS</th>");
                           out.println("<th>SPEED</th>");
                           out.println("<th>HEADING</th>");
                           out.println("<th>GPSDATA</th>");
                           out.println("</tr>");

                          out.println("<tr>");

                          out.println("<td>"+rs.getInt("timeofday") +"</td>");
                          out.println("<td>"+rs.getInt("timeofhours") +"</td>");
                          out.println("<td>"+rs.getInt("speed") +"</td>");
                          out.println("<td>"+rs.getInt("heading") +"</td>");
                          out.println("<td>"+rs.getInt("gpssource") +"</td>");

                          out.println("</tr>"); 

                          bw.write(rs.getInt("latitude"));  
                          bw.write(rs.getInt("longitude"));


                    }
                    out.println("</table>");

                    out.println("</center>"); 
                    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();  
                        }  
                    }  
                }  

I am Writing only Latitude & Longitude to File ,Rest Displaying it in a table.For Both data type in DB is DOUBLE.

                      bw.write(rs.getInt("latitude"));  
                      bw.write(rs.getInt("longitude"));

O/P that I am getting code2.txt is some string values : M M

5
  • 1
    Try: bw.write(String.valueOf(rs.getInt("latitude"))); Commented Jan 10, 2015 at 6:29
  • I am Getting like this 1277 where 12 is lat 77 is lon,But Iam not getting Full value i.e after decimal (.) my latitude in DB is 12.12212 & longitude 77.23213213 How to fetch this Commented Jan 10, 2015 at 6:39
  • That is because those values are doubles and you are calling getInt(...) replace getInt(...) with getDouble(...) Commented Jan 10, 2015 at 6:40
  • offtopic: creating connection on each doPost may be expensive Commented Jan 10, 2015 at 6:46
  • How to I add Sap-rate them by , comma 12.56558985 77.8944564613 Commented Jan 10, 2015 at 6:48

5 Answers 5

1

That's because, your integer is type casted to character as per the write api. See this line cb[nextChar++] = (char) c;

So int 77 = M character and is what is being written onto file. You could convert your integer to String and write onto file like:

bw.write(String.valueOf(rs.getInt("latitude")));  
Sign up to request clarification or add additional context in comments.

Comments

0

Try to close BufferedWriter and then FileWriter in finally block.

bw.close();
fw.close();

Comments

0

The getInt(...) method returns an int value. You can convert those values to String by using String.valueOf(rs.getInt(...)). Or you can call rs.getString(...) to get those values as Strings from the ResultSet

Comments

0

2 things:

  • Use bw.flush() before bw.close(). See this.
  • If latitude and longitude are of type double, then why not use ResultSet#getDouble(...)?

Also share some more details regarding the original longitude/latitude values and the output that you're getting in the file. This will help is identifying the problem (if any).

Comments

0

You can see this solution:

exporting sql query result to a csv or excel

In poperties files set this values:

fileExtension=.txt
columSeparator=\t

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.