|
|
Re: Date insertion problem in database
|
Posted: Dec 18, 2006 1:03 PM
|
|
I hav posted this in another thread but didnt get reply.Pls I m sorry for doble posting but I m stuck here in my progrmme. I seek ur help.My Follwing programe throws an Exception which is-java.sql.SQLException.....Data Type mismatch in criteria expression.In My program there are two customiazed class- 1. DBase() and 2-JDateChooser. DBase is used to connect database.JDateChooser is a class which creates a textfield with a calendar besides it. After selecting a date frm dat calendar it shows the date in MMM dd,yyyy format. It also returns the date by getDate() in dat format. I tried to get dat date and format it in dd mm yyyy format. But the above exception occurs.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import tools.diit.*;
import java.sql.*;
import java.text.*;
import java.util.Date;
import com.toedter.calendar.*;
public class SimpleDatabaseExm extends JFrame{
JDateChooser dtc= new JDateChooser();
JButton btn=new JButton();
String tableName = "Stat";
String primaryKey= "ID";
Statement stmt;
ResultSet rs;
Calendar cl=new GregorianCalendar();
SimpleDatabaseExm (){
stmt = new DBase().getStatement("./Data/Diit13.mdb","","");
this.setSize(300,300);
this.setLocation(300,150);
this.getContentPane().add(dtc,BorderLayout.NORTH);
this.getContentPane().add(btn,BorderLayout.SOUTH);
btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Date stdate=dtc.getDate();
getDate(stdate);
}
});
}// end constr
public static void main(String[] args)
{
SimpleDatabaseExm dbconn=new SimpleDatabaseExm();
dbconn.setVisible(true);
}
public void refresh()
{
try{
rs =stmt.executeQuery("SELECT * FROM " + tableName);
rs.next();
}catch(SQLException sqle){
System.out.println("ERROR REFRESH : " + sqle);
}
}//End of Method refresh()
public void getDate(Date d3)
{
//refresh();
SimpleDateFormat sdf = new SimpleDateFormat("dd mm yyyy");
try{
String s=sdf.format(d3);
String s3=sdf.format(new Date());
System.out.println("Start date-"+ s +"End Date"+ s3);
String st="INSERT INTO Stat(Year,Income) VALUES('"+s+"','"+s3+"')";
stmt.executeUpdate(st);
refresh();
}
catch(SQLException sqle)
{ System.out.println("ERROR DISPLAY : " + sqle); }
catch(NullPointerException ex)
{ System.out.println("Null Pointer Ex"); }
}
}
|
|