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
bw.write(String.valueOf(rs.getInt("latitude")));doubles and you are callinggetInt(...)replacegetInt(...)withgetDouble(...)