public void save2() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
String date="2004-3-29";
java.util.Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try{
d=sdf.parse(date);
}catch(Exception e)
{
e.printStackTrace();
}
//数据库用户名
String userName="root";
//密码
String userPasswd="qq121121";
//数据库名
String dbName="MySql";
//表名
String tableName="pet1";
//联结字符串
//String url="jdbc:mysql:3306//localhost/"+dbName;
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Connection connection=DriverManager.getConnection(url);
//Statement pstmt=connection.prepareStatement("insert into pet2 values (?)");
PreparedStatement pt = connection.prepareStatement("insert into pet1 values (?)");
pt.setDate(1, new java.sql.Date(d.getTime()));
int j=pt.executeUpdate();
if(j!=0)
System.out.println("ok");
}
}
//转换逻辑
String 转换成YYYY--MM--DD格式的日期类型(叫格式化日期)
再复到Date date中,然后再赋值给SQL类型的日期
String date="2004-3-29";
java.util.Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try{
d=sdf.parse(date);
}catch(Exception e)
{
e.printStackTrace();
}
//数据库用户名
String userName="root";
//密码
String userPasswd="qq121121";
//数据库名
String dbName="MySql";
//表名
String tableName="pet1";
//联结字符串
//String url="jdbc:mysql:3306//localhost/"+dbName;
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Connection connection=DriverManager.getConnection(url);
//Statement pstmt=connection.prepareStatement("insert into pet2 values (?)");
PreparedStatement pt = connection.prepareStatement("insert into pet1 values (?)");
pt.setDate(1, new java.sql.Date(d.getTime()));
int j=pt.executeUpdate();
if(j!=0)
System.out.println("ok");
}
}
//转换逻辑
String 转换成YYYY--MM--DD格式的日期类型(叫格式化日期)
再复到Date date中,然后再赋值给SQL类型的日期