我使用HSQL作为内存数据库进行测试。但是,我无法在列中插入日期。 下面是我的表创建脚本:
INSERT INTO OWN_TABLE(ID, NAME, CAPACITY_SUPPLY_DATE) VALUES(2, '4813', '2090-07-15');
我试过的查询:
Caused by: java.sql.SQLDataException: data exception: invalid datetime format
但它给出了
2090-07-15 00:00:00
我也试过了INSERT INTO LABS_CAPACITY_SUPPLY(ID, SHIP_NODE, CAPACITY_SUPPLY_DATE) VALUES(1, '4813', 'TO_DATE(2090-07-30,"yyy-MM-dd")');
,但它没有用。
假设oracle语法可能有效,我试过:
Unparseable date: "TO_DATE(2090-07-30,"yyy-MM-dd")"
但得到了2.3.2
有人可以告诉我在HSQL中插入日期的正确方法。我有@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.ivMovieIcon = (ImageView)convertView.findViewById(R.id.ivIcon);
holder.tvMovie = (TextView)convertView.findViewById(R.id.tvMovie);
holder.tvTagline = (TextView)convertView.findViewById(R.id.tvTagline);
holder.tvYear = (TextView)convertView.findViewById(R.id.tvYear);
holder.tvDuration = (TextView)convertView.findViewById(R.id.tvDuration);
holder.tvDirector = (TextView)convertView.findViewById(R.id.tvDirector);
holder.rbMovieRating = (RatingBar)convertView.findViewById(R.id.rbMovie);
holder.tvCast = (TextView)convertView.findViewById(R.id.tvCast);
holder.tvStory = (TextView)convertView.findViewById(R.id.tvStory);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
Toast.makeText(MainActivity.this, "Name of Director",
Toast.LENGTH_LONG).show();
}
});
final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
ImageLoader.getInstance().displayImage(movieModelList.get(position).getImage(), holder.ivMovieIcon, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
holder.tvMovie.setText(movieModelList.get(position).getMovie());
holder.tvTagline.setText(movieModelList.get(position).getTagline());
holder.tvYear.setText("Year: " + movieModelList.get(position).getYear());
holder.tvDuration.setText("Duration: " + movieModelList.get(position).getDuration());
holder.tvDirector.setText("Director: " + movieModelList.get(position).getDirector());
// rating bar
holder.rbMovieRating.setRating(movieModelList.get(position).getRating()/2);
StringBuffer stringBuffer = new StringBuffer();
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
stringBuffer.append(cast.getName() + ", ");
}
holder.tvCast.setText("Cast:" + stringBuffer);
holder.tvStory.setText(movieModelList.get(position).getStory());
return convertView;
}
class ViewHolder{
private ImageView ivMovieIcon;
private TextView tvMovie;
private TextView tvTagline;
private TextView tvYear;
private TextView tvDuration;
private TextView tvDirector;
private RatingBar rbMovieRating;
private TextView tvCast;
private TextView tvStory;
}
jar。
答案 0 :(得分:3)
使用适当的标准SQL date
文字:
INSERT INTO OWN_TABLE
(ID, NAME, CAPACITY_SUPPLY_DATE)
VALUES
(2, '4813', DATE '2090-07-15');
或者,如果您确实想使用to_date()
函数,请不要将其放入单引号中。
INSERT INTO LABS_CAPACITY_SUPPLY
(ID, SHIP_NODE, CAPACITY_SUPPLY_DATE)
VALUES
(1, '4813', TO_DATE(2090-07-30,'yyy-MM-dd'));
请注意,对于to_date()
函数,格式字符串的大小写无关紧要。 'yyyy-MM-dd'
与'YYYY-MM-DD'
或'yyyy-mm-dd'
相同。格式为不用于Java SimpleDateFormat
的格式。
如果您是在Java程序中执行此操作,则不应使用上述任何解决方案。改为使用PreparedStatement
:
java.sql.Date dt = ...;
PreparedStatement pstmt = connect.prepareStatement("INSERT INTO OWN_TABLE(ID, NAME, CAPACITY_SUPPLY_DATE) VALUES(?,?,?)";
pstmt.setInt(1, 2);
pstmt.setString(2, "4813");
pstmt.setDate(3, dt);
如何构造java.sql.Date
的实例取决于您从何处获取数据。您可以使用SimpleDateFormat
来解析用户的输入。或者,如果您使用的是Java 8,则可以使用LocalDate,例如java.sql.Date.valueOf(LocaleDate.of(2090,7,30))