Java String 转成 Mysql Date
publicvoidsave2()throwsInstantiationException,IllegalAccessException,ClassNotFoundException,SQLException{
Stringdate="2004-3-29";
java.util.Dated=newDate();
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
try{
d=sdf.parse(date);
}catch(Exceptione)
{
e.printStackTrace();
}
//数据库用户名
Stringusername="root";
//密码
StringuserPasswd="qq121121";
//数据库名
Stringdbname="MySql";
//表名
Stringtablename="pet1";
//联结字符串
//Stringurl="jdbc:mysql:3306//localhost/"+dbName;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Stringurl="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Connectionconnection=DriverManager.getConnection(url);
//Statementpstmt=connection.prepareStatement("insertintopet2values(?)");
PreparedStatementpt=connection.prepareStatement("insertintopet1values(?)");
pt.setDate(1,newjava.sql.Date(d.getTime()));
intj=pt.executeUpdate();
if(j!=0)
System.out.println("ok");
}
}
//转换逻辑
String转换成YYYY--MM--DD格式的日期类型(叫格式化日期)
再复到Datedate中,然后再赋值给SQL类型的日期