在Java程序中如何将数据从数据库导出到文本文件

时间:2015-01-10 06:26:09

标签: java file jdbc

我正在尝试使用java代码编写从Database获取值的文件。

从DB&中检索Resultset值。然后写入文件。我能够写入文件,但我没有得到确切的值,而是得到垃圾值。

代码:

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();  
                        }  
                    }  
                }  

我只写“Latitude&amp; amp;文件的经度,休息在表格中显示。对于DB中的两种数据类型都是DOUBLE。

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

O / P我得到的code2.txt是一些字符串值: 中号 中号

5 个答案:

答案 0 :(得分:1)

那是因为,根据write api,您的整数是按字符类型转换为字符的。请参阅此行cb[nextChar++] = (char) c;

所以int 77 = M字符,是写入文件的内容。您可以将整数转换为String并写入文件,如:

bw.write(String.valueOf(rs.getInt("latitude")));  

答案 1 :(得分:0)

尝试在finally块中关闭BufferedWriter然后关闭FileWriter。

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

答案 2 :(得分:0)

getInt(...)方法返回int值。 您可以使用String.valueOf(rs.getInt(...))将这些值转换为String。 或者,您可以致电rs.getString(...)将这些值从String

中获取为ResultSet s

答案 3 :(得分:0)

2件事:

  • bw.flush()之前使用bw.close()。见this
  • 如果纬度和经度属于double类型,那么为什么不使用ResultSet#getDouble(...)

还分享有关原始经度/纬度值以及您在文件中获得的输出的更多详细信息。这有助于识别问题(如果有的话)。

答案 4 :(得分:0)

您可以看到此解决方案:

exporting sql query result to a csv or excel

在属性文件中设置以下值:

fileExtension=.txt
columSeparator=\t