将Derby DB导出为CSV文件

时间:2018-03-04 22:50:25

标签: java database csv

我有一个到CSV的derby db输出,如果我不导出数字(在数据库中设置为VARCHAR),它会很有效。

有没有办法导出字符串数字而不会截断它们?

例如,数据库的编号为16,321.42,但导出仅将其截断为16。我假设逗号是一个因素?

附加是用于选择字段的屏幕。非程序员(员工)在下载某些数据组合时需要使用该屏幕。

这是我的代码:

选择要导出为CSV的数据库字段:

enter image description here

    private void SaveFileJFileChooser() throws IOException{
        JFileChooser fc = new JFileChooser();
        File f = new File(new File("main.csv").getCanonicalPath());
     fc.setDialogTitle("SAVE OPTION");
     int returnValue = fc.showSaveDialog(null);
         if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fc.getSelectedFile();
            SaveFile = fc.getSelectedFile().toString();
            saveCsv();
    }    // If canceled in the JFileChooser then do this: 

private void doSQLFIELDS() {

    if(rbtn1.isSelected()){
    dwn1A = "PROJNAME,";
 } else {dwn1A ="";}

 if(rbtn2.isSelected()){
    dwn2A ="UNITS,";
     }else {dwn2A="";}



 if(rbtn3.isSelected()){
    dwn3A = "CRM,";
 } else {dwn3A ="";}

 if(rbtn4.isSelected()){
    dwn4A = "CITY,";
    } else {dwn4A ="";}

  if(rbtn5.isSelected()){
    dwn5A = "STATE,";
 } else {dwn5A ="";}

  if(rbtn6.isSelected()){
    dwn6A = "DATE,";
    } else {dwn6A ="";}

  if(rbtn7.isSelected()){
    dwn7A = "MATTOTAL,";
 } else {dwn7A ="";}

  if(rbtn8.isSelected()){
dwn8A = "LABORTOTAL,";
} else {dwn8A ="";}

  if(rbtn9.isSelected()){
dwn9A = "MISCTOTAL,";
} else {dwn9A ="";}

   if(rbtn10.isSelected()){
dwn10A = "MATLABTOTAL,";
} else {dwn10A ="";}

  if(rbtn11.isSelected()){
dwn11A = "SHIP,";
 } else {dwn11A ="";}

   if(rbtn12.isSelected()){
dwn12A = "PRONTAX,";
} else {dwn12A ="";}

    if(rbtn13.isSelected()){
dwn13A = "UNITNTAX,";
} else {dwn13A ="";}

     if(rbtn14.isSelected()){
dwn14A = "SALEPERUNITNTAX,";
} else {dwn14A ="";}

      if(rbtn15.isSelected()){
dwn15A = "INSTALLTOTAL,";
} else {dwn15A ="";}

      if(rbtn16.isSelected()){
dwn16A = "PERCENTTAX,";
} else {dwn16A ="";}

         if(rbtn17.isSelected()){
dwn17A = "CURRENTTAX,";
} else {dwn17A ="";}

          if(rbtn18.isSelected()){
dwn18A = "TOTALSWTAX,";
} else {dwn18A ="";}

  if(rbtn19.isSelected()){
dwn19A = "MARGINMULT,";
} else {dwn19A ="";}

   if(rbtn20.isSelected()){
dwn20A = "MARGIN,";
} else {dwn20A ="";}

    if(rbtn21.isSelected()){
dwn21A = "PROINSTALLER,";
} else {dwn21A ="";}

  if(rbtn22.isSelected()){
dwn22A = "INSTALLTYPE,";
} else {dwn22A ="";}

   if(rbtn23.isSelected()){
dwn23A = "PRODUCT,";
} else {dwn23A ="";}

 if(rbtn24.isSelected()){
dwn24A = "STATUPDATE,";
} else {dwn24A ="";}

    dwn25A = "ID";

String sqlquote =           dwn1A+dwn2A+dwn3A+dwn4A+dwn5A+dwn6A+dwn7A+dwn8A+
dwn9A+dwn10A+ dwn11A+dwn12A+dwn13A+dwn14A+dwn15A+
dwn16A+dwn17A+dwn18A+dwn19A+ dwn20A+dwn21A+dwn22A+
dwn23A+dwn24A+dwn25A;
sqlquote2 = sqlquote;
System.out.println(sqlquote2);

sqlquote2 = sqlquote.replaceAll(“\ s”,“”);

SQL2 =  "select "+sqlquote2 +" from APP.PROTABLE" ;
System.out.println(SQL2);
jtxtfldDISPLAY.setText(SQL2);




}


  private void saveCsv()throws IOException {
    String FILE_HEADER = sqlquote2;


    Statement stmt;
    ResultSet rst;
    Connection c;
     // change to the filename slection in JFileChooser
// SaveFile = "c;\\csvmain.csv";
           try {   

        c = DriverManager.getConnection("jdbc:derby:proposaldb");
        stmt = c.createStatement();
        rst = stmt.executeQuery(jtxtfldDISPLAY.getText() );
        FileWriter fw = new FileWriter(SaveFile);


        CSVPrinter csvPrinter = new CSVPrinter(fw,format);
        // Adds the header line
        fw.append(FILE_HEADER.toString());
        fw.append(NEW_LINE_SEPARATOR);

             while (rst.next()){
                     ResultSetMetaData rsmd = rst.getMetaData();
                      int columnCount = rsmd.getColumnCount();


                 for(int i=1; i <=columnCount; i++)
                 {
                    if(rst.getObject(i)!=null)
                     {
                         String data= rst.getObject(i).toString();
                         csvPrinter.printRecord(data);

                     }
                     else
                     {
                         String data= "null";
                          csvPrinter.printRecord(data);


                     }
                 } 
              //new line entered after each row
               fw.append(System.getProperty("line.separator"));
             }        
                 fw.flush();
                  fw.close();
                  csvPrinter.close();

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException ex) {
      Logger.getLogger(ExcelLOAD.class.getName()).log(Level.SEVERE, null, ex);
  }

};

0 个答案:

没有答案