用SimpleDateFormat吧。- import java.sql.*;
- import java.text.SimpleDateFormat;
- public class Test {
- public static void main(String age[])
- {
- Connection con=null;
- PreparedStatement pst=null;
- String brithday="2013-01-01";
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","");
- pst=con.prepareStatement("insert into user values(?,?)");
- pst.setString(1, "name");
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- java.util.Date date=sdf.parse(brithday);
- pst.setDate(2, new java.sql.Date(date.getTime()));
- pst.execute();
- }catch(Exception e)
- {
- System.out.println("error");
- }
- }
- }
复制代码 |