数据库mySql. 我现在想把获得的时间字符串比如"2002-08-01 12:00:01"这样的时间字符串插入到数据库中,因为需要做时间比较,故在相应的数据库字段中没有只是采用简单的vchar型,而是使用了datetime类型。我现在写了如下代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date = sdf.parse("2004-08-01 12:00:01");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
String sqlStr = "insert into tableTest set date = "+sqlDate+"";
以上代码插入数据库中的数据是错误的,请高手们指教!!
5 个解决方案
#1
如果你要在数据库中存时间:
1:long型的,操作时转换
2:string型的,也要转换
如果要比较的话,long型的好一些,容易计算。
1:long型的,操作时转换
2:string型的,也要转换
如果要比较的话,long型的好一些,容易计算。
#2
String sqlStr = "insert into tableTest set date = "+sqlDate+"";
变成:
String sqlStr = "insert into tableTest set date = '"+sqlDate+"'";
变成:
String sqlStr = "insert into tableTest set date = '"+sqlDate+"'";
#3
String sqlStr = "insert into tableTest set date = ?";
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(sqlDate);
stmt.executeUpdate();
stmt.close();
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(sqlDate);
stmt.executeUpdate();
stmt.close();
#4
刚才错了
String sqlStr = "insert into tableTest(字段) value(?)";
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(1, sqlDate);
stmt.executeUpdate();
stmt.close();
String sqlStr = "insert into tableTest(字段) value(?)";
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(1, sqlDate);
stmt.executeUpdate();
stmt.close();
#5
我毕业设计用的是access数据库,是这样的
CString strSQL;
strSQL.Format("UPDATE databasename.tablename SET yourownfield=#%s#",date);
You can write it like this:
strSQL.Format("UPDATE databasename.tablename SET yourownfield='%d-%d-%d %d:%d:%d'",date.GetYear(),date.Month(), date.GetDay(), date.GetHour(), date.GetMinute(), date.GetSecond());
note: after"%d-%d-%d"there is a space
CString strSQL;
strSQL.Format("UPDATE databasename.tablename SET yourownfield=#%s#",date);
You can write it like this:
strSQL.Format("UPDATE databasename.tablename SET yourownfield='%d-%d-%d %d:%d:%d'",date.GetYear(),date.Month(), date.GetDay(), date.GetHour(), date.GetMinute(), date.GetSecond());
note: after"%d-%d-%d"there is a space
#1
如果你要在数据库中存时间:
1:long型的,操作时转换
2:string型的,也要转换
如果要比较的话,long型的好一些,容易计算。
1:long型的,操作时转换
2:string型的,也要转换
如果要比较的话,long型的好一些,容易计算。
#2
String sqlStr = "insert into tableTest set date = "+sqlDate+"";
变成:
String sqlStr = "insert into tableTest set date = '"+sqlDate+"'";
变成:
String sqlStr = "insert into tableTest set date = '"+sqlDate+"'";
#3
String sqlStr = "insert into tableTest set date = ?";
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(sqlDate);
stmt.executeUpdate();
stmt.close();
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(sqlDate);
stmt.executeUpdate();
stmt.close();
#4
刚才错了
String sqlStr = "insert into tableTest(字段) value(?)";
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(1, sqlDate);
stmt.executeUpdate();
stmt.close();
String sqlStr = "insert into tableTest(字段) value(?)";
PreparedStatement stmt = conn.preparedStatement(sqlStr);
stmt.setTimestamp(1, sqlDate);
stmt.executeUpdate();
stmt.close();
#5
我毕业设计用的是access数据库,是这样的
CString strSQL;
strSQL.Format("UPDATE databasename.tablename SET yourownfield=#%s#",date);
You can write it like this:
strSQL.Format("UPDATE databasename.tablename SET yourownfield='%d-%d-%d %d:%d:%d'",date.GetYear(),date.Month(), date.GetDay(), date.GetHour(), date.GetMinute(), date.GetSecond());
note: after"%d-%d-%d"there is a space
CString strSQL;
strSQL.Format("UPDATE databasename.tablename SET yourownfield=#%s#",date);
You can write it like this:
strSQL.Format("UPDATE databasename.tablename SET yourownfield='%d-%d-%d %d:%d:%d'",date.GetYear(),date.Month(), date.GetDay(), date.GetHour(), date.GetMinute(), date.GetSecond());
note: after"%d-%d-%d"there is a space