首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java windows应用程序在mysql中获取介于两个日期之间的记录

问使用java windows应用程序在mysql中获取介于两个日期之间的记录
EN

Stack Overflow用户
提问于 2015-09-11 13:34:51
回答 1查看 66关注 0票数 1

我输入了两个雇员的数据,所以我得到了两个表e1和e2,在这些表上,我存储了以下表中特定日期的出勤率,这里的日期列类型是employee.See。两个表e1和e2,

代码语言:javascript
复制
    |------------------||-----------------|
    | date    | present|| date   | present|
    |2015-9-7 |  1     ||2015-9-7|  1     |
    |2015-9-8 |  1     ||2015-9-8|  0     |
    |2015-9-9 |  1     ||2015-9-9|  1     |

为了获得员工的出勤率,我有一个用于输入员工Id的文本字段,还有两个用于选择日期的下拉列表,即"dayfrom“显示1,2..31,"monthfrom”显示Jan,Feb..Dec和den samew另外两个下拉列表"dayto“和"monthto”。

所以我的代码是,

代码语言:javascript
复制
JTextField day1,day2,id;
Container c;
int t=0,in=0,re;
Integer mo1=new Integer(0);
Integer mo2=new Integer(0);
JButton sh,home,sha;
JLabel lday1,lday2,lid;
String tp;
String mo[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
int it[]=new int[100];
int ct[]=new int[100];
Choice dob1,dob2,add1,add2;
JOptionPane jp = new JOptionPane();
public emp()// constructor
{

    lid = new JLabel("Id :");  
    lid.setForeground(Color.black);
    lday1 = new JLabel("Day From :");  
    lday1.setForeground(Color.black);
    lday2 = new JLabel("Day To:");  
    lday2.setForeground(Color.black);

    id = new JTextField(); 
    id.setForeground(Color.black); 
    id.setBackground(Color.white);
    day1 = new JTextField(); 
    day1.setForeground(Color.black); 
    day1.setBackground(Color.white);
    day2 = new JTextField(); 
    day2.setForeground(Color.black); 
    day2.setBackground(Color.white);
    sh = new JButton("Show"); 
    sh.setForeground(Color.white); 
    sh.setBackground(new Color(128,0,0));
    dob1 = new Choice();
    dob2 = new Choice();
    dob1.add("1");
    dob1.add("2");
    dob1.add("3");
    dob1.add("4");
    dob1.add("5");
    dob1.add("6");
    dob1.add("7");
    dob1.add("8");
    dob2.add("JAN");
    dob2.add("FEB");
    dob2.add("MAR");
    dob2.add("APR");
    dob2.add("MAY");
    dob1.setBounds(190,150,40,20);
    dob2.setBounds(250,150,55,20);
    add1 = new Choice();
    add2 = new Choice();
    add1.add("1");
    add1.add("2");
    add1.add("3");
    add1.add("4");
    add1.add("5");
    add1.add("6");
    add1.add("7");
    add1.add("8");
    add2.add("JAN");
    add2.add("FEB");
    add2.add("MAR");
    add2.add("APR");
    add2.add("MAY");
    c.add(lid);
    c.add(lday1);
    c.add(lday2);
    c.add(id);
    c.add(sh);
    c.add(dob1);
    c.add(dob2);
    c.add(add1);
    c.add(add2);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

}
public void actionPerformed(ActionEvent ae)
{   

    Object source = ae.getSource();
    if(source==sh)
    {
        re=0;
        if((id.getText()).length()!=0)
        {
        try
        {

            t=0;
            for(in=0;in<12;in++)
            {
                if(mo[in]==dob2.getSelectedItem())
                mo1=in+1;

            }
            ResultSet rs = stm.executeQuery("select * from e"+id.getText()+" where date_format(day,'%d')="+dob1.getSelectedItem()+" and date_format(day,'%m')="+mo1);
            while(rs.next())
            {   
                t=1;    
                re=rs.getInt("pre");
                if(re==1)
                jp.showMessageDialog(this,"Present";
                else
                jp.showMessageDialog(this,"Absent");
            }
            if(t==0)
            {
                jp.showMessageDialog(this,"Sorry, No Such Record exists");
                t=0;
            }

    }

}

但在这里,我得到的输出是“不存在这样的记录”。

EN

回答 1

Stack Overflow用户

发布于 2015-09-11 15:46:42

您可以直接创建简单的mysql查询,如下所示:

SELECT * FROM [TABLE NAME] WHERE DATE(date) BETWEEN 'yyyy-mm-dd' AND 'yyy-mm-dd'

它将为您提供所选员工的出勤列表。因此,您将从代码中创建日期格式字符串,并将其传递给mysql查询。

希望能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32516162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档