如何使用掩码从 JFormatedTextField 插入日期
How to Insert date from JFormatedTextField with mask
我正在开发一个应用程序,其中有一些 JFormatedTextFields 具有掩码格式(##/##/#### 为 dd/MM/YYYY)。我正在尝试将这些日期插入数据库,但显示错误 "Error Converting data type nvarchar to Date"
Error Converting data type nvarchar to Date
但我在我的 sql 过程中找不到任何问题,因为如果我 运行 使用 sql 查询分析器的过程它正在工作,但如果我尝试从应用程序执行它显示错误
这是我的所有代码
sql 程序
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
谢谢。
在我看来,您将 Java 中的日期变量分配为字符串,但实际上将它们定义为日期:
String RStart = txt_RStart.getText();
...
@RegistatinStartDate date
然后您尝试将 date 转换为 varchar,但又将其分配回日期?
Declare @RFirst as Date
...
set @RFirst = CONVERT(varchar, @RegistatinStartDate, 103)
我正在开发一个应用程序,其中有一些 JFormatedTextFields 具有掩码格式(##/##/#### 为 dd/MM/YYYY)。我正在尝试将这些日期插入数据库,但显示错误 "Error Converting data type nvarchar to Date" Error Converting data type nvarchar to Date
但我在我的 sql 过程中找不到任何问题,因为如果我 运行 使用 sql 查询分析器的过程它正在工作,但如果我尝试从应用程序执行它显示错误
这是我的所有代码
sql 程序
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
谢谢。
在我看来,您将 Java 中的日期变量分配为字符串,但实际上将它们定义为日期:
String RStart = txt_RStart.getText();
...
@RegistatinStartDate date
然后您尝试将 date 转换为 varchar,但又将其分配回日期?
Declare @RFirst as Date
...
set @RFirst = CONVERT(varchar, @RegistatinStartDate, 103)