我试图从textfield获取日期,但我只获得了一年(2017年)获取

时间:2017-12-02 07:19:56

标签: java mysql swing

我试图通过使用mysql从文本字段中获取日期,并且我只获取了年份(2017) 这是我的问题:

theQuery("insert into Files11(FileName,Date,Coding,Demo,Audit,Lop,Denials,Cs,Comments,Percentage) values('"+JT_FileName.getText()+"','"+JT_Date1.getText()+"','"+Coding.getText()+"','"+Demo.getText()+"','"+Audit.getText()+"','"+Lop.getText()+"','"+Denials.getText()+"','"+Cs.getText()+"','"+Comments.getText()+"','"+Percentage.getText()+"')");

以前我使用此查询获取完整日期:

theQuery("insert into Files5(FileName,LinesCount,UserId,Date) values('"+JT_FileName.getText()+"','"+JT_LinesCount.getText()+"','"+text.getText()+"','"+JT_Date1.getText()+"')");

获取: 我使用过这个查询

String sql = "SELECT * FROM Files11"; 

任何人都可以帮助我,我在这里缺少什么? 这是我的整个源代码:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;    
import java.io.*;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;

public class NewProjectRCM2 extends JFrame implements ActionListener{
 JLabel JL_Filechooser,JL_FileName,JL_Coding,JL_Date,JL_From,JL_To,JL_Demo,JL_Audit,JL_Lop,JL_Denials,JL_Cs,JL_Comments,JL_Target,JL_Percentage;
 JTextField JT_Filechooser,JT_FileName,JT_Coding,JT_Date1,JT_Demo,JT_Audit,JT_Lop,JT_Denials,JT_Cs,JT_Comments,JT_Target,JT_Percentage;      
 JButton btn_update,openfile,btn_refresh,btn_calculate;                 

    JLabel l1=new JLabel("Username:");    

    JButton b = new JButton("Login");  

    JTextField text = new JTextField(20);


    JTextField Coding,Demo,Audit,Lop,Denials,Cs,Comments,Percentage;




 public NewProjectRCM2(){
     super("USER_ID");
     JL_Filechooser = new JLabel("File chooser:");
     JL_FileName = new JLabel("File Name:");
     JL_Date = new JLabel("Date:");
     JL_From = new JLabel("From:");
     JL_To = new JLabel("To:");
     JL_Coding = new JLabel("Coding:");
     JL_Demo = new JLabel("Demo:");
     JL_Audit = new JLabel("Audit:");
     JL_Lop = new JLabel("Lop:");
     JL_Denials = new JLabel("Denials:");
     JL_Cs = new JLabel("Cs:");
     JL_Comments = new JLabel("Comments:");

     JL_Target=new JLabel("Target:");

     JL_Percentage=new JLabel("Percentage:");


     l1.setBounds(20,20, 80,30);   
     b.setBounds(100,120, 80,30);    
     text.setText(System.getProperty("user.name")); text.setBounds(100, 20, 100, 30);
     text.setEnabled(false);
     text.setFont(new Font("Tahoma", Font.BOLD,25));
     text.setBackground(Color.GRAY);


     JL_Filechooser.setBounds(20, 20, 100, 20);
     JL_FileName.setBounds(20, 50, 100, 20);
     JL_Date.setBounds(20,90, 100, 20);
     JL_From.setBounds(20, 133, 100, 20);
     JL_To.setBounds(250, 133, 100, 20);

     JL_Coding.setBounds(20,180, 100, 20);
     JL_Demo.setBounds(20, 210, 100, 20);
     JL_Audit.setBounds(20, 240, 100, 20);
     JL_Lop.setBounds(20, 270, 100, 20);
     JL_Denials.setBounds(20, 300, 100, 20);
     JL_Cs.setBounds(20, 330, 100, 20);
     JL_Comments.setBounds(20, 360, 100, 20);

     JL_Target.setBounds(250, 180, 100, 20);
     JL_Percentage.setBounds(175,410, 100, 20);




     DefaultTableModel model = new DefaultTableModel();
     JTable jtb = new JTable();//tableadded
     JScrollPane sp=new JScrollPane(jtb);    //tableadded
     sp.setBounds(20, 450, 1000, 200);//tableadded

     JT_Filechooser = new JTextField(20);
     JT_FileName = new JTextField(20);
     JT_Date1 = new JTextField(20);
     Coding = new JTextField(20);
     Demo = new JTextField(20);
     Audit = new JTextField(20);
     Lop = new JTextField(20);
     Denials = new JTextField(20);
     Cs = new JTextField(20);
     Comments = new JTextField(20);

     JT_Target = new JTextField(20); 
     int Target1=100;
     JT_Target.setText(Integer.toString(Target1));
     Percentage = new JTextField(20);





     JT_Date1.setText(String.valueOf(java.time.LocalDate.now())); 
     text.setBounds(100, 20, 100, 30);
     JT_Date1.setEnabled(false);
     JT_Date1.setFont(new Font("Tahoma", Font.BOLD, 15));
     JT_Date1.setBackground(Color.GRAY);




    Calendar cal = Calendar.getInstance();
     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
     text.setBounds(100, 20, 100, 30);   



     JT_Filechooser.setBounds(130,20,150,20);
     JT_FileName.setBounds(130,50,150, 20);
     JT_Date1.setBounds(130,90, 150, 20);
     Coding.setBounds(130,180, 75, 20);
     Demo.setBounds(130,210,75, 20);
     Audit.setBounds(130,240,75, 20);
     Lop.setBounds(130,270, 75, 20);
     Denials.setBounds(130,300, 75, 20);
     Cs.setBounds(130,330, 75, 20);
     Comments.setBounds(130,360,75, 20);

     JT_Target.setBounds(350, 180,75, 20);

     Percentage.setBounds(250,410, 100, 20);






     btn_update = new JButton("UPDATE");
     btn_update.setBounds(270,300,80, 20);
     btn_refresh = new JButton("Refresh");
     btn_refresh.setBounds(480,132, 80, 20);
     btn_calculate = new JButton("RESULT");
     btn_calculate.setBounds(270,360, 80, 20);


     openfile = new JButton("openfile");
     openfile.setBounds(300, 20, 80, 20);
     openfile.addActionListener(this);

     /* UtilDateModel model1 = new UtilDateModel();
     JDatePanelImpl datePanel = new JDatePanelImpl(model1);
     JDatePickerImpl datePicker = new JDatePickerImpl(datePanel,new DateLabelFormatter());
     datePicker.setBounds(130, 110, 180, 30); */

     UtilDateModel model2 = new UtilDateModel();
     JDatePanelImpl datePanel2 = new JDatePanelImpl(model2);
     JDatePickerImpl datePicker1 = new JDatePickerImpl(datePanel2,new DateLabelFormatter());
     datePicker1.setBounds(60, 133, 180, 20);

     UtilDateModel model3 = new UtilDateModel();
     JDatePanelImpl datePanel3 = new JDatePanelImpl(model3);
     JDatePickerImpl datePicker2 = new JDatePickerImpl(datePanel3,new DateLabelFormatter());
     datePicker2.setBounds(273, 133, 180, 20);

     setLayout(null);
     add(JL_Filechooser);
     add(JL_FileName);
    // add(datePicker);
     add(datePicker1);
     add(datePicker2);
     add(JL_Date);
     add(JL_From);
     add(JL_To);
     add(JL_Coding);
     add(JL_Demo);
     add(JL_Audit);
     add(JL_Lop);
     add(JL_Denials);
     add(JL_Cs);
     add(JL_Comments);

     add(JL_Target);
     add(JL_Percentage);


     add(JT_Filechooser);
     add(JT_FileName);
     add(JT_Date1);
     add(Coding);
     add(Demo);
     add(Audit);
     add(Lop);
     add(Denials);
     add(Cs);
     add(Comments);

     add(JT_Target);
     add(Percentage);

     add(btn_update);
     add(btn_refresh);
     add(btn_calculate);

     add(l1);
     add(b);
     add(text);
     add(openfile);
     add(sp);    

     JL_Filechooser.setVisible(false);
     JL_FileName.setVisible(false);
     JL_Date.setVisible(false);
     JL_Coding.setVisible(false);
     JL_From.setVisible(false);
     JL_To.setVisible(false);
     JL_Demo.setVisible(false);
     JL_Audit.setVisible(false);
     JL_Lop.setVisible(false);
     JL_Denials.setVisible(false);
     JL_Cs.setVisible(false);
     JL_Comments.setVisible(false);

     JL_Target.setVisible(false);
     JL_Percentage.setVisible(false);

    // datePicker.setVisible(false);
     datePicker1.setVisible(false);
     datePicker2.setVisible(false);

     JT_Filechooser.setEnabled(false);
     JT_Filechooser.setFont(new Font("Tahoma", Font.BOLD, 15));


     JT_Filechooser.setVisible(false);
     JT_FileName.setVisible(false);
     JT_Date1.setVisible(false);
     Coding.setVisible(false);
     Demo.setVisible(false);
     Audit.setVisible(false);
     Lop.setVisible(false);
     Denials.setVisible(false);
     Cs.setVisible(false);
     Comments.setVisible(false);

     JT_Target.setVisible(false);
     Percentage.setVisible(false);


     btn_update.setVisible(false);
     btn_refresh.setVisible(false);
     btn_calculate.setVisible(false);
     openfile.setVisible(false);
     sp.setVisible(false);

   //button update
    btn_update.addActionListener(new  ActionListener() {

         public void actionPerformed(ActionEvent e) {
         try{
             theQuery("insert into Files11(FileName,Date,Coding,Demo,Audit,Lop,Denials,Cs,Comments,Percentage) values('"+JT_FileName.getText()+"','"+JT_Date1.getText()+"','"+Coding.getText()+"','"+Demo.getText()+"','"+Audit.getText()+"','"+Lop.getText()+"','"+Denials.getText()+"','"+Cs.getText()+"','"+Comments.getText()+"','"+Percentage.getText()+"')");

          }
         catch(Exception ex){}
         }
     });


    btn_calculate.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            int num1=Integer.parseInt(Coding.getText());

            int num2=Integer.parseInt(Demo.getText());
            int num3=Integer.parseInt(Audit.getText());
            int num4=Integer.parseInt(Lop.getText());
            int num5=Integer.parseInt(Denials.getText());
            int num6=Integer.parseInt(Cs.getText());
            //int num7=Integer.parseInt(Comments.getText());
            double value=(num1+num2+num3+num4+num5+num6);
            double percent=value/100;
            double result=percent*100;
            Percentage.setText(result +"");

        }
    });






    btn_refresh.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try{
                 String sql = "SELECT * FROM Files11 where Date between '"+datePicker1.getJFormattedTextField().getText()+"' and '"+datePicker2.getJFormattedTextField().getText()+"'";
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/tut", "root", "root");
            PreparedStatement pstm = con.prepareStatement(sql);
            ResultSet Rs = pstm.executeQuery();
            Vector data=new Vector();//tableadded start

            Vector column=new Vector();
            column.add("FileName");
            column.add("Date");
            column.add("Coding");
            column.add("Demo");  
            column.add("Audit");
            column.add("Lop");
            column.add("Denials");
            column.add("Cs");
            column.add("Comments");
            column.add("Percentage");



            while(Rs.next()){
               Vector row = new Vector();
               row.add(Rs.getString("FileName"));
               row.add(Rs.getInt("Date"));
               row.add(Rs.getInt("Coding"));
               row.add(Rs.getString("Demo"));
               row.add(Rs.getString("Audit"));
               row.add(Rs.getString("Lop"));
               row.add(Rs.getString("Denials"));
               row.add(Rs.getString("Cs"));
               row.add(Rs.getString("Comments"));
               row.add(Rs.getString("Percentage"));

               data.add(row);
            }
            jtb.setModel(new DefaultTableModel(data,column));//tableaddedend
            }
               catch(Exception ex){}    
        }

    });



    b.addActionListener(new ActionListener() {                                                                                                                                                                             
         public void actionPerformed(ActionEvent e) {       
             l1.setVisible(false);
             text.setVisible(false);
             b.setVisible(false);
             JL_Filechooser.setVisible(true);
             JL_FileName.setVisible(true);
             JL_Coding.setVisible(true);
             JL_Date.setVisible(true);
             JL_From.setVisible(true);
             JL_To.setVisible(true);
             JL_Demo.setVisible(true);
             JL_Audit.setVisible(true);
             JL_Lop.setVisible(true);
             JL_Denials.setVisible(true);
             JL_Cs.setVisible(true);
             JL_Comments.setVisible(true);

             JL_Target.setVisible(true);
             JL_Percentage.setVisible(true);


             JT_Filechooser.setVisible(true);
             JT_FileName.setVisible(true);
             Coding.setVisible(true);
             JT_Date1.setVisible(true);
             Demo.setVisible(true);
             Audit.setVisible(true);
             Lop.setVisible(true);
             Denials.setVisible(true);
             Cs.setVisible(true);
             Comments.setVisible(true);

             JT_Target.setVisible(true);

             Percentage.setVisible(true);



           //  datePicker.setVisible(true);
             datePicker1.setVisible(true);
             datePicker2.setVisible(true);


             btn_update.setVisible(true);
             btn_refresh.setVisible(true);
             btn_calculate.setVisible(true);
             openfile.setVisible(true);
             sp.setVisible(true);
        }  
    });   

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     setLocationRelativeTo(null);
     setSize(650,700);

 }



//function to execute the insert update delete query
  public void theQuery(String query){
      Connection con = null;
      Statement st = null;
      try{
          con = DriverManager.getConnection("jdbc:mysql://localhost/tut","root","root");
          st = con.createStatement();
          st.executeUpdate(query);
          JOptionPane.showMessageDialog(null,"YOUR FILE HAS BEEN SUBMITTED AND UPDATED SUCCESSFULLY");
      }catch(Exception ex){
          JOptionPane.showMessageDialog(null,ex.getMessage());
      }
  }

  public void actionPerformed(ActionEvent e) {    
      if(e.getSource()==openfile){      
          JFileChooser fc=new JFileChooser();    
          int i=fc.showOpenDialog(this);    
          if(i==JFileChooser.APPROVE_OPTION){    
              File f=fc.getSelectedFile();    
              String filepath=f.getPath();  
              JT_Filechooser.setText(filepath);
              JT_FileName.setText(f.getName());
              try{ 
              FileInputStream fis = new FileInputStream(f.getAbsolutePath());

              XWPFDocument document = new XWPFDocument(fis);

              List<XWPFParagraph> paragraphs = document.getParagraphs();

              int count = 0;
              int charCount = 0;
              int c = 0;
              for (XWPFParagraph para : paragraphs) {
                count++;
                charCount += para.getText().length();
                c=charCount/65;
                System.out.println(para.getText());
              }
              System.out.println("No. of lines : "+count);
             // JT_Coding.setText(Integer.toString(c));
             // JT_Coding.setEnabled(false);
             // JT_Coding.setBackground(Color.GRAY);
              fis.close();
              }catch (Exception ex) {ex.printStackTrace();  }

          }    
      }    
      }  

     public static void main(String[] args){

         new  NewProjectRCM2();
     }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

这是由于粗心的错误,我返回了整数(Rs.getint)而不是String(Rs.getString)。这很有趣,但我花了很多时间才找到。