Crystalxxx
asked on
XMLGregorianCalendar to sql date in Netbeans
Does anyone know how to convert/transfer XMLGregorianCalendar to java.sql.Date?
Im using Netbeans glassfish ESB to create a patient admission service. The WSDL service is based on the xml schema(xsd). however, as im taking patient's DoB as an input, the textbox cannot convert the XMLGregorianCalendar (specified in the schema as Date) to sql (based on the connected MySQL database).
Anyone know how to solve it? Im thinkinig of using the SQL Date Converter, but really dont have a clue.
Please Help
Im using Netbeans glassfish ESB to create a patient admission service. The WSDL service is based on the xml schema(xsd). however, as im taking patient's DoB as an input, the textbox cannot convert the XMLGregorianCalendar (specified in the schema as Date) to sql (based on the connected MySQL database).
Anyone know how to solve it? Im thinkinig of using the SQL Date Converter, but really dont have a clue.
Please Help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
java.sql.Date d = new java.sql.Date(cal.toGregor ianCalenda r().getTim eInMillis( ));
ASKER
Thanks a lot. But both "toGregorianCalendar()" and "getTimeInMillis()" are showing errors. Do I need do declare or import sth beforehand? The error msg is saying cannot find the method.
What code have you got already - please let's see it
should need any imports
make sure 'cal' is declared as a javax.xml.datatype.XMLGreg orianCalen dar
make sure 'cal' is declared as a javax.xml.datatype.XMLGreg
javax.xml.datatype.XMLGreg orianCalen dar cal = ....;
java.sql.Date d = new java.sql.Date(cal.toGregor ianCalenda r().getTim eInMillis( ));
java.sql.Date d = new java.sql.Date(cal.toGregor
ASKER
The code is shown as following. Basically the data source is from MySQL database, field name is DoB. Here im using three input elements to retrieve the data to perform search. Familyname,Firstname and DoB. It's just the DoB that's not working. I have specified it as txtbox, rather than calendar.
package patientui;
import com.sun.rave.faces.convert er.SqlDate Converter;
import com.sun.rave.web.ui.appbas e.Abstract PageBean;
import com.sun.webui.jsf.componen t.Button;
import com.sun.webui.jsf.componen t.PanelLay out;
import com.sun.webui.jsf.componen t.StaticTe xt;
import com.sun.webui.jsf.componen t.TextFiel d;
import java.sql.Date;
import javax.xml.datatype.XMLGreg orianCalen dar;
import javax.faces.FacesException ;
import org.netbeans.j2ee.wsdl.pat ientservic e.patient. PatientPor tType;
import org.netbeans.j2ee.wsdl.pat ientservic e.patient. PatientSer vice;
import org.netbeans.xml.schema.pa tientschem a.PatDetai lsReq;
import org.netbeans.xml.schema.pa tientschem a.PatDetai lsRes;
public class PatientLookUp extends AbstractPageBean {
// <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
/**
* <p>Automatically managed component initialization. <strong>WARNING:</strong>
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.</p>
*/
private void _init() throws Exception {
}
private PanelLayout lpFind = new PanelLayout();
public PanelLayout getLpFind() {
return lpFind;
}
public void setLpFind(PanelLayout pl) {
this.lpFind = pl;
}
private TextField txtfFN = new TextField();
public TextField getTxtfFN() {
return txtfFN;
}
public void setTxtfFN(TextField tf) {
this.txtfFN = tf;
}
private TextField txtfGN = new TextField();
public TextField getTxtfGN() {
return txtfGN;
}
public void setTxtfGN(TextField tf) {
this.txtfGN = tf;
}
private StaticText stmsgFN = new StaticText();
public StaticText getStmsgFN() {
return stmsgFN;
}
public void setStmsgFN(StaticText st) {
this.stmsgFN = st;
}
private PanelLayout lpView = new PanelLayout();
public PanelLayout getLpView() {
return lpView;
}
public void setLpView(PanelLayout pl) {
this.lpView = pl;
}
private Button btnSearch01 = new Button();
public Button getBtnSearch01() {
return btnSearch01;
}
public void setBtnSearch01(Button b) {
this.btnSearch01 = b;
}
private TextField txtDoB = new TextField();
public TextField getTxtDoB() {
return txtDoB;
}
public void setTxtDoB(TextField tf) {
this.txtDoB = tf;
}
public PatientLookUp() {
}
try {
_init();
} catch (Exception e) {
log("Page1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
}
}
@Override
public void prerender() {
init();
lpFind.setVisible(true);
lpView.setVisible(false);
stmsgFN.setVisible(false);
}
protected SessionBean1 getSessionBean1() {
return (SessionBean1) getBean("SessionBean1");
}
/**
* <p>Return a reference to the scoped data bean.</p>
*
* @return reference to the scoped data bean
*/
protected RequestBean1 getRequestBean1() {
return (RequestBean1) getBean("RequestBean1");
}
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1) getBean("ApplicationBean1" );
}
public String btnLookup_action() {
stmsgFN.setText("");
stmsgFN.setVisible(false);
boolean blRecordExist = false;
PatDetailsRes patRes = null;
try {
PatDetailsReq msgPatDetailsReq = new PatDetailsReq();
msgPatDetailsReq.setFamily Name((Stri ng) txtfFN.getValue());
msgPatDetailsReq.setGivenN ame((Strin g) txtfGN.getValue());
XMLGregorianCalendar cal =txtDoB.getTime();
java.sql.Date d = new java.sql.Date(cal.toGregor ianCalenda r().getTim eInMillis( ));
PatientService service = new PatientService();
PatientPortType port = service.getPatientPort();
patRes = port.getPatientDetails(msg PatDetails Req);
blRecordExist = patRes.isRecordExist();
package patientui;
import com.sun.rave.faces.convert
import com.sun.rave.web.ui.appbas
import com.sun.webui.jsf.componen
import com.sun.webui.jsf.componen
import com.sun.webui.jsf.componen
import com.sun.webui.jsf.componen
import java.sql.Date;
import javax.xml.datatype.XMLGreg
import javax.faces.FacesException
import org.netbeans.j2ee.wsdl.pat
import org.netbeans.j2ee.wsdl.pat
import org.netbeans.xml.schema.pa
import org.netbeans.xml.schema.pa
public class PatientLookUp extends AbstractPageBean {
// <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
/**
* <p>Automatically managed component initialization. <strong>WARNING:</strong>
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.</p>
*/
private void _init() throws Exception {
}
private PanelLayout lpFind = new PanelLayout();
public PanelLayout getLpFind() {
return lpFind;
}
public void setLpFind(PanelLayout pl) {
this.lpFind = pl;
}
private TextField txtfFN = new TextField();
public TextField getTxtfFN() {
return txtfFN;
}
public void setTxtfFN(TextField tf) {
this.txtfFN = tf;
}
private TextField txtfGN = new TextField();
public TextField getTxtfGN() {
return txtfGN;
}
public void setTxtfGN(TextField tf) {
this.txtfGN = tf;
}
private StaticText stmsgFN = new StaticText();
public StaticText getStmsgFN() {
return stmsgFN;
}
public void setStmsgFN(StaticText st) {
this.stmsgFN = st;
}
private PanelLayout lpView = new PanelLayout();
public PanelLayout getLpView() {
return lpView;
}
public void setLpView(PanelLayout pl) {
this.lpView = pl;
}
private Button btnSearch01 = new Button();
public Button getBtnSearch01() {
return btnSearch01;
}
public void setBtnSearch01(Button b) {
this.btnSearch01 = b;
}
private TextField txtDoB = new TextField();
public TextField getTxtDoB() {
return txtDoB;
}
public void setTxtDoB(TextField tf) {
this.txtDoB = tf;
}
public PatientLookUp() {
}
try {
_init();
} catch (Exception e) {
log("Page1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
}
}
@Override
public void prerender() {
init();
lpFind.setVisible(true);
lpView.setVisible(false);
stmsgFN.setVisible(false);
}
protected SessionBean1 getSessionBean1() {
return (SessionBean1) getBean("SessionBean1");
}
/**
* <p>Return a reference to the scoped data bean.</p>
*
* @return reference to the scoped data bean
*/
protected RequestBean1 getRequestBean1() {
return (RequestBean1) getBean("RequestBean1");
}
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1) getBean("ApplicationBean1"
}
public String btnLookup_action() {
stmsgFN.setText("");
stmsgFN.setVisible(false);
boolean blRecordExist = false;
PatDetailsRes patRes = null;
try {
PatDetailsReq msgPatDetailsReq = new PatDetailsReq();
msgPatDetailsReq.setFamily
msgPatDetailsReq.setGivenN
XMLGregorianCalendar cal =txtDoB.getTime();
java.sql.Date d = new java.sql.Date(cal.toGregor
PatientService service = new PatientService();
PatientPortType port = service.getPatientPort();
patRes = port.getPatientDetails(msg
blRecordExist = patRes.isRecordExist();
ASKER
I know the line "XMLGregorianCalendar cal =txtDoB.getTime();" is not correct. It's showing error already. But im not sure how to link it to the text field i specified on the form, also the database field "DoB", which has a data type of "date"
Many thanks
Many thanks
compiles fine here
> I know the line "XMLGregorianCalendar cal =txtDoB.getTime();" is not correct.
so where are you getting a XMLGregorianCalendar from?
so where are you getting a XMLGregorianCalendar from?
ASKER
It specified at the beginning as import javax.xml.datatype.XMLGreg orianCalen dar;
May I ask how you compile it? Im using Netbeans but everything is a mess
May I ask how you compile it? Im using Netbeans but everything is a mess
>>It's showing error already.
Please post the error
Please post the error
> It specified at the beginning as import javax.xml.datatype.XMLGreg orianCalen dar;
No, I meant where is the XMLGregorianCalendar instance you want to convert coming from, and why are you actually using it at all
> May I ask how you compile it? Im using Netbeans but everything is a mess
eclipse at the moment
No, I meant where is the XMLGregorianCalendar instance you want to convert coming from, and why are you actually using it at all
> May I ask how you compile it? Im using Netbeans but everything is a mess
eclipse at the moment
ASKER
Oh...i guess im lost. Didn't want to use the Calendar as input element because it's not user-friendly in case of selecting a date of birth. Because the original database im connecting and the schema declare this as date, im having problem inputting information to store in the database. I hope i explained this clearly.
>> May I ask how you compile it?
Yes - i was wondering that too - did you give objects your schema?
Yes - i was wondering that too - did you give objects your schema?
Please go to the command line, change to the root directory of your project and run the following, and afterwards attach the file 'comp.txt' from the same directory. You can delete it afterwards
ant -logfile comp.txt compile
looks like you just need something like the following to parse the date, then you can instert the date in the database
http://helpdesk.objects.com.au/java/how-do-i-parse-a-java-string-that-specifies-a-date (change the format to suit your needs)
http://helpdesk.objects.com.au/java/how-do-i-parse-a-java-string-that-specifies-a-date (change the format to suit your needs)
ASKER
Thanks guys this code now works,
XMLGregorianCalendar cal = msgPatDetailsReq.getDoB();
java.sql.Date d = new java.sql.Date(cal.toGregor ianCalenda r().getTim eInMillis( ));
But im wondering how can i link it bk to the textbox again? What i did by using the code above was the date conversion, does anyone know how to retrieve the information afterwards? The textbox name is txtDoB
Thanks
XMLGregorianCalendar cal = msgPatDetailsReq.getDoB();
java.sql.Date d = new java.sql.Date(cal.toGregor
But im wondering how can i link it bk to the textbox again? What i did by using the code above was the date conversion, does anyone know how to retrieve the information afterwards? The textbox name is txtDoB
Thanks
ASKER
However, the date above has given me a LONG format
ASKER
I think the problem is because im using "getTimeInMillis()“. Is there any other way?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use the following to format a date in short format:
String sDate = String.format("%td/%<tm/%<ty", date);
Open in new window