I am working on a app there are some JFormatedTextFields with mask formatted (##/##/#### as dd/MM/YYYY). I am trying to insert these date into Database but it is showing an error "Error Converting data type nvarchar to Date"
Error Converting data type nvarchar to Date
but I could not find any problem in my sql procedure because if I run that procedure using sql query analyzer it is working but if I try to execute from app it is showing error
here is my all codes
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
and here is the java codes
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);
}
}
any Idea where is the problem in my codes?
I am using java netbeans and sql server 2008
Thank you.
It looks to me that you have you assign your date variables in Java as strings, but you have them actually defined as dates:
String RStart = txt_RStart.getText();
...
#RegistatinStartDate date
And then you try to convert from date to varchar but assign it back to a date?
Declare #RFirst as Date
...
set #RFirst = CONVERT(varchar, #RegistatinStartDate, 103)
Related
I'm trying to insert values into a table (inquiry) the first value is of type Date , and I'm getting an SQL error SQL State S1009. what is the proper way to convert the date , what am I doing wrong?
String sqlStatement = "INSERT INTO inquiry (INQUIRY_DATE,INQUIRY_NOTE,INQUIRER_ID,PROGRAM_ID,CLASS_ID,CORPORATE_ID)\n"
+ "VALUES (?,?,?,?);";
ps = con.prepareStatement(sqlStatement);
java.sql.Date sDate = new java.sql.Date(inquiry.getInquiryDate().getTime());
int parameterIndex = 1;
ps.setDate(parameterIndex, sDate);
ps.setString(parameterIndex++, inquiry.getInquiryNote());
ps.setInt(parameterIndex++, inquiry.getInquirer().getInquirerID());
ps.setInt(parameterIndex++, inquiry.getProgramID());
ps.setInt(parameterIndex++, inquiry.getClassProgramID());
ps.setInt(parameterIndex++, 1);
sqlStatement = "INSERT INTO inquiry (INQUIRY_DATE,INQUIRY_NOTE,INQUIRER_ID,PROGRAM_ID,CLASS_ID,CORPORATE_ID)\n"
+ "VALUES (?,?,?,?);";
The parameterized query doesn't have enough ?, you queried 6 columns with 2 ? missing, it should be VALUES (?,?,?,?,?,?); ? are used for holding the places for your setXXX() column values
I am using jCalender to input date
String from_date = ((JTextField) txt_bilty_date_start.getDateEditor().getUiComponent()).getText();
String to_date = ((JTextField) txt_bilty_date_end.getDateEditor().getUiComponent()).getText();
String sql = " "; //what query I need to use
I'm not java pro but SQL statement could be like this
select * from POSTS where Id = 1
and Date between '2011/02/25' and '2011/02/27'
or can use
select * from POSTS where Id = 1
and Date >= '2011/02/25' and Date <= '2011/02/27'
I am using MySQL 5.5 and I am inserting datetime into my table through Java, using the query:
INSERT INTO player_sale(transaction_id,player_id,game_id,game_type_id,ticket_nbr,mrp_amt,deduct_from_bonus,deduct_from_deposit,deduct_from_winning,good_cause_amt,vat_amt,taxable_sale,transaction_date,is_cancel) VALUES(48160,1001501,1,2,0,1.0,0.0,1.0,0.0,0.0,0.0,1.0,'2015-07-09 18:45:01','N');
but the entry inserted is 0000-00-00 00:00:00 in the transaction_date column.
In addition to it, when I am executing the above query, through the query browser interface of MySQL, the correct datetime is inserted . The datatype of transacion_date is datetime.
I have dug out a lot of web, but I couldn't find the answer regarding the same.
I used the following java code :
try{
Timestamp transactionDate = new Timestamp(Calendar.getInstance().getTimeInMillis());
String query="INSERT INTO player_sale(transaction_id,player_id,game_id,game_type_id,ticket_nbr,mrp_amt,deduct_from_bonus,
deduct_from_deposit,deduct_from_winning,good_cause_amt,vat_amt,taxable_sale,transaction_date,is_cancel)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
PreparedStatement pt = con.prepareStatement(query);
pt.setLong(1, 1);
pt.setLong(2, 110021);
pt.setInt(3, 1);
pt.setInt(4, 1);
pt.setLong(5, 12345678);
pt.setDouble(6, 10.00);
pt.setDouble(7, 20.00);
pt.setDouble(8,30.00);
pt.setDouble(9, 40.00);
pt.setDouble(10, 10.00);
pt.setDouble(11, 2.00);
pt.setDouble(12, 1.00);
pt.setTimestamp(13, transactionDate);
pt.setString(14, "N");
pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
I am getting this error when calling my stored procedure with an out parameter. It points to where I register the out parameter. I don't see what I'm missing here. Thanks in advance.
java.sql.SQLException: Parameter number 6 is not an OUT parameter
Here's my method in Java:
String sp = "{CALL `hotelbuddy2.0`.`reservation.newres`(?,?,?,?,?,?)}";
CallableStatement cs = DB2.con2.prepareCall(sp);
cs.setInt(1, guest.getGuestId());
cs.setTimestamp(2, checkIn);
cs.setTimestamp(3, checkOut);
cs.setString(4, comment);
cs.setString(5, UserDetails.username);
cs.registerOutParameter(6, Types.INTEGER);
cs.execute();
reservationId = cs.getInt(6);
cs.close();
Here's my Stored Procedure in Mysql:
CREATE DEFINER=`root`#`localhost` PROCEDURE `reservation.newres`(
IN p_guest_id INT,
IN p_check_in TIMESTAMP,
IN p_check_out timestamp,
IN p_comment VARCHAR(300),
IN p_created_by VARCHAR(45),
OUT p_reservation_id INT
)
BEGIN
INSERT INTO reservation
SET guest_id = p_guest_id,
check_in = p_check_in,
check_out = p_check_out,
`comment` = p_comment,
created_by = p_created_by;
SELECT LAST_INSERT_ID() INTO p_reservation_id;
END
Iam trying to send an integer value from mysql Procedure which must be recieved by Java class. this i'hv done
CREATE DEFINER=`root`#`localhost` PROCEDURE `updateCountry`(o_name varchar(35), u_name varchar(50),m_by varchar(35),out msg int)
BEGIN
if not exists (select country from country where country = u_name) then
UPDATE country SET country= u_name, modify_date=now(), modify_by=m_by where country = o_name;
end if;
if exists (select country from country where country = u_name) then
set msg := '1';
end if;
END
and my java class is :
public void modifyCountry(String mod, String real, String crby)throws Exception {
Date d = new Date(System.currentTimeMillis());
System.out.print(d);
CallableStatement cs = conn
.prepareCall("{ call updateCountry(?,?,?,?)}");
cs.setString(1, real);
cs.setString(2, mod);
// cs.setDate(3, d);
cs.setString(3, crby);
cs.registerOutParameter(4, Types.INTEGER);
int check = cs.getInt(4);
System.out.print(check);
cs.execute();
}
And this is the error which im getting
No output parameters returned by procedure.
Try to execute the statement first then read the output parameter