如何使用掩码从JFormatedTextField插入日期

时间:2015-11-02 16:00:27

标签: java sql-server date jformattedtextfield

我正在开发一个应用程序,有一些带有掩码格式的JFormatedTextFields(## / ## / ####作为dd / MM / YYYY)。我试图将这些日期插入数据库,但它显示错误“错误将数据类型nvarchar转换为日期” Error Converting data type nvarchar to Date

但我在我的sql过程中找不到任何问题,因为如果我使用sql查询分析器运行该过程它正在工作,但如果我尝试从app执行它显示错误

这是我的所有代码

sql procedure

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Proc_set_ExamDeclaration]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Proc_set_ExamDeclaration]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  Procedure [dbo].[Proc_set_ExamDeclaration]
                     (
                    @SchoolCode nvarchar (10),
                    @ClassCode nvarchar (4),
                    @GroupName nvarchar(50),
                    @ExamCode nvarchar (4),
                    @RegistationFess numeric(38,2),
                    @RegistatinStartDate date,
                    @RegistatinEndDate date,
                    @ExamStartDate date,
                    @ExamStatus nvarchar(10)
                    )
as

BEGIN
-- Insert statements for procedure here
Declare @RFirst as Date, @REnd as Date, @EStart as Date
set @RFirst = CONVERT(varchar, @RegistatinStartDate, 103)
set @REnd = CONVERT(varchar, @RegistatinEndDate, 103)
set @EStart = CONVERT(varchar, @ExamStartDate, 103)
IF NOT EXISTS (SELECT * FROM [dbo].[ExamDeclaration] where ClassCode = @ClassCode AND GroupName = @GroupName AND ExamCode = @ExamCode AND ExamStatus = 'Active')
BEGIN
INSERT INTO ExamDeclaration (SchoolCode, ClassCode,GroupName,ExamCode,RegistationFess,RegistatinStartDate,RegistatinEndDate,ExamStartDate,ExamStatus)
VALUES (@SchoolCode, @ClassCode,@GroupName,@ExamCode,@RegistationFess,@RFirst,@REnd,@EStart,@ExamStatus)
END
ELSE 
Update ExamDeclaration 
set RegistationFess = @RegistationFess,
RegistatinStartDate = @RFirst,
RegistatinEndDate = @REnd,
ExamStartDate = @EStart,
ExamStatus = @ExamStatus
Where ClassCode = @ClassCode AND GroupName=@GroupName AND ExamCode = @ExamCode
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

这是java代码

    String School_Code = txt_SchoolCode.getText();
    String ClassCode = txt_ClassCode.getText();
    String groupName = com_groupname.getSelectedItem().toString();
    String examCode = txt_ExamCode.getText();
    String fees = txt_fees.getText();        
    String RStart = txt_RStart.getText();//((JTextField) txt_RStart.getDateEditor().getUiComponent()).getText();
    String REnd = txt_REnd.getText();//((JTextField) txt_REnd.getDateEditor().getUiComponent()).getText();
    String EStart = txt_ExamStart.getText();//((JTextField) txt_ExamStart.getDateEditor().getUiComponent()).getText();        
    String Estatus = com_eStatus.getSelectedItem().toString();
    if(ClassCode.isEmpty() && examCode.isEmpty() && fees.isEmpty() /*&& RStart.isEmpty() && REnd.isEmpty() && EStart.isEmpty()*/){JOptionPane.showMessageDialog(null, "All Fields are Required !!");}
    else{
        try{
            String sqlExamD = "Exec Proc_set_ExamDeclaration ?,?,?,?,?,?,?,?,?";
            pst=conn.prepareStatement(sqlExamD);
            pst.setString(1, School_Code);
            pst.setString(2, ClassCode);
            pst.setString(3, groupName);
            pst.setString(4, examCode);
            pst.setString(5, fees);
            pst.setString(6, RStart);
            pst.setString(7, REnd);
            pst.setString(8, EStart);
            pst.setString(9, Estatus);

            pst.execute();
            JOptionPane.showMessageDialog(null, "Saved Successfuly");
            btn_new.doClick();

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }

任何想法我的代码中的问题在哪里?

我正在使用java netbeans和sql server 2008

谢谢。

1 个答案:

答案 0 :(得分:0)

在我看来,您已将Java中的日期变量指定为字符串,但实际上将它们定义为日期:

Declare @RFirst as Date
...
set @RFirst = CONVERT(varchar, @RegistatinStartDate, 103)

然后你尝试将日期转换为varchar,但将其分配回日期?

{{1}}