0

how to send a null date into database

asked 2012-10-11 18:37:03 +0800

Webtibet3 gravatar image Webtibet3
30

Hy,

i have a zul page with datebox field
i have a controleur (.java) page which want get the date
And i have a dao file who insert the information into database, but sometimes i don't need date to send but i can't send null date in.


I want to put a blank data into my database, thank's for your answears

delete flag offensive retag edit

7 Replies

Sort by » oldest newest

answered 2012-10-11 19:51:46 +0800

jj gravatar image jj
638 3

if you use JDBC, use the setNull(...) method. If you use any ORM, it should be built-in.

link publish delete flag offensive edit

answered 2012-10-11 21:37:21 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

Does you table allow null in that date field?
Are you using JDBC or an ORM?
What database?
What have you tried so far? GIve us some code.
And what exactly is happening that leads you to believe it is not working? Exception? View of database?

link publish delete flag offensive edit

answered 2012-10-12 07:01:16 +0800

Webtibet3 gravatar image Webtibet3
30

Hi,

I have JDBC, it's mysql database.
my original code :

evenement.zul :
<window id="modalDialog" title="Ajout d'un Evenement" border="normal" width="990px" height="630px"
closable="true" action="show: slideDown;hide: slideUp" position="center,center" apply="admin.ctrl.CtrlAjoutEvent" contentStyle="overflow:auto">
<grid width="970px">
<row>
Date de l'evenement
<datebox id="dateEvent"/>
</row>


controleur :

DaoEvenement dao = new DaoEvenement();
System.out.println(""+ dateEvent.getValue());
Evenement newEvent = new Evenement(0, nom.getValue(), description.getValue(), "", illustration.toString(), dateEvent.getValue(), new java.sql.Date(new Date().getTime()), 0, 0);
setGeo(adresse.getValue(), newEvent);

//on vérifie si l'ajout c'est bien passé et on affiche un message
if(dao.insertAjoutEvent(newEvent)){
Messagebox.show("L'évènement "+newEvent.getNom()+"a été ajouté avec succès", "Information", Messagebox.OK, Messagebox.INFORMATION, new org.zkoss.zk.ui.event.EventListener() {
public void onEvent(Event evt) throws InterruptedException {
if (evt.getName().equals("onOK")) {
Executions.sendRedirect("");
}
}
});


Dao :

public class DaoEvenement extends DaoBase {
public DaoEvenement() throws IOException {
super();
// TODO Auto-generated constructor stub
}

public boolean insertAjoutEvent(Evenement event) throws DAOException{
// Connection
Connection conn = null;

// Statement
PreparedStatement prepStmt = null;

//valeur de retour
boolean isInsert = false;

try {
conn = getConnection();
if (conn != null) {

// Insert a row
String sql = "INSERT INTO evenement values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
prepStmt = conn.prepareStatement(sql);
int n = 1;
prepStmt.setInt(n++, event.getId());
prepStmt.setString(n++, event.getNom());
prepStmt.setString(n++, event.getDescription());
prepStmt.setDate(n++, new Date(event.getDateEvent().getTime()));
prepStmt.setDate(n++, (Date) event.getDateAjout());
prepStmt.setString(n++, event.getAdresse());
prepStmt.setDouble(n++,event.getLat());
prepStmt.setDouble(n++,event.getLngt());
prepStmt.setString(n++, event.getIllustration());

System.out.println(" requete : "+prepStmt);

if(prepStmt.executeUpdate() == 1){
isInsert = true;
}
}
} catch (SQLException se) {
System.err.println("Error with the database or reqsuest!");
se.printStackTrace();
} finally {
try {
if (prepStmt != null)
prepStmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
throw new DAOException("probleme à la fermeture de la connexion" + se);
}
}
return isInsert;
}


Thank's for your help.
When i click on submit button of zul i have a java null pointer exception

link publish delete flag offensive edit

answered 2012-10-12 07:13:05 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

At which line you have the null pointer exception?

link publish delete flag offensive edit

answered 2012-10-12 09:16:09 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

@Webtibet3 Please post code inside SourceCode tag(Symbol #) so it will be easy for everyone to read the code
thanks

link publish delete flag offensive edit

answered 2012-10-16 09:22:33 +0800

Webtibet3 gravatar image Webtibet3
30

re-edit :

Hi,
I have JDBC, it's mysql database.
my original code :

evenement.zul :

<window id="modalDialog" title="Ajout d'un Evenement" border="normal" width="990px" height="630px"
closable="true" action="show: slideDown;hide: slideUp" position="center,center" apply="admin.ctrl.CtrlAjoutEvent" contentStyle="overflow:auto">
<grid width="970px">
<row>
Date de l'evenement
<datebox id="dateEvent"/>
</row>

controleur :

DaoEvenement dao = new DaoEvenement();
System.out.println(""+ dateEvent.getValue());
Evenement newEvent = new Evenement(0, nom.getValue(), description.getValue(), "", illustration.toString(), dateEvent.getValue(), new java.sql.Date(new Date().getTime()), 0, 0);
setGeo(adresse.getValue(), newEvent);

//on vérifie si l'ajout c'est bien passé et on affiche un message
if(dao.insertAjoutEvent(newEvent)){	
Messagebox.show("L'évènement "+newEvent.getNom()+"a été ajouté avec succès", "Information", Messagebox.OK, Messagebox.INFORMATION, new org.zkoss.zk.ui.event.EventListener() {
public void onEvent(Event evt) throws InterruptedException {
if (evt.getName().equals("onOK")) {
Executions.sendRedirect("");
}
}
});

Dao :

public class DaoEvenement extends DaoBase {
public DaoEvenement() throws IOException {
super();
// TODO Auto-generated constructor stub
}

public boolean insertAjoutEvent(Evenement event) throws DAOException{
// Connection
Connection conn = null;

// Statement
PreparedStatement prepStmt = null;

//valeur de retour
boolean isInsert = false;

try {
conn = getConnection();
if (conn != null) {

// Insert a row
String sql = "INSERT INTO evenement values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
prepStmt = conn.prepareStatement(sql);
int n = 1;
prepStmt.setInt(n++, event.getId());
prepStmt.setString(n++, event.getNom());
prepStmt.setString(n++, event.getDescription());
prepStmt.setDate(n++, new Date(event.getDateEvent().getTime()));
prepStmt.setDate(n++, (Date) event.getDateAjout());
prepStmt.setString(n++, event.getAdresse());
prepStmt.setDouble(n++,event.getLat());
prepStmt.setDouble(n++,event.getLngt());
prepStmt.setString(n++, event.getIllustration());

System.out.println(" requete : "+prepStmt);

if(prepStmt.executeUpdate() == 1){
isInsert = true;
}
}
} catch (SQLException se) {
System.err.println("Error with the database or reqsuest!");
se.printStackTrace();
} finally {
try {
if (prepStmt != null)
prepStmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
throw new DAOException("probleme à la fermeture de la connexion" + se);
}
}
return isInsert;
}

Thank's for your help.
When i click on submit button of zul i have a java null pointer exception at dao.DaoEvenement.insertAjoutEvent(DaoEvenement.java:43)
>> at admin.ctrl.CtrlAjoutEvent.onClick$submitBtn(CtrlAjoutEvent.java:74)

link publish delete flag offensive edit

answered 2012-10-16 14:28:28 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

Have you traced this null pointer exception back to the exact line of code that caused it?

For example in the code you have posted above line 43 is se.printStackTrace(); It's highly unlikely that the SQLException is null at this point which leads me to believe that the code you have posted here is not the exact code you are working with. For example if you had comments at the beginning of the file but did not post them here it would throw the line count off.

Now since you say you think the date should be null I would look at line 28 above you have the statement:

prepStmt.setDate(n++, new Date(event.getDateEvent().getTime()));

My guess is that event.getDateEvent() is returning null and thus trying to getTime() from a null object is not going to work.

So it has nothing to do with your database.

an aside....
Why do you create a new Date in line 28 but just cast the object to a Date in line 29?

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-10-11 18:37:03 +0800

Seen: 134 times

Last updated: Oct 16 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More