字符串到java.sql.Date [英] String to java.sql.Date

查看:80
本文介绍了字符串到java.sql.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了很多。我确实看过。我花了几个小时环顾四周,并试图找出答案。我应该编写一个程序,该程序在数据库中存储相当于约会列表的内容,包括描述,日期,开始时间和结束时间。它必须接受用户的输入才能添加或取消约会,据我所知,这意味着我需要将字符串转换为日期。

I realize this has been asked a lot. I did actually look. I've spent hours looking around and trying to figure this out. I'm supposed to be making a program that stores what amounts to a list of appointments in a database, with a description, date, start time, and end time. It has to take input from the user to add or cancel appointments, so as far as I know that means I need to convert a string to a date.

这些是我的导入:
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

These are my imports: import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Time; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Scanner;

如您所见,那里没有java.util.Date。这是我遇到错误的地方:

As you can see, no java.util.Date there. Here is the bit where I'm getting the error:

private static java.sql.Date getDay()
{
    Scanner in = new Scanner(System.in);
    String input;
    Date apptDay = null;
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    java.sql.Date sqlDate;
    System.out.println("\nPlease enter the date of the appointment, format: yyyy/mm/dd");
    while(apptDay == null)
    {
        try
        {
            input = in.next();
            apptDay = (Date) df.parse(input);
        }
        catch(ParseException e)
        {
            System.out.println("Please enter a valid date! Format is yyyy/mm/dd");
        }
    }
    sqlDate = new Date(apptDay.getTime());
    return sqlDate;
}

我在其中添加了java.sql.Dates并对其进行了修改一堆试图使其工作,但它仍然给我这个:

I've added java.sql.Dates to it and mucked about with it a bunch trying to get it to work, but it's still giving me this:

线程 main中的异常java.lang.ClassCastException:java无法将Calendar.getDay(Calendar.java:47)上的.util.Date强制转换为java.sql.Date

任何关于我做错了什么或如何做这项工作的想法都将不胜感激。

Any ideas on what I'm doing wrong or how to make this work would be very much appreciated.

编辑:我想,如果我再添加一点调用此代码的代码,这样也许可以更清楚地说明我是如何使用它的,所以这里是addAppointment()方法,因此您可以看到getDay()的调用位置以及调用的方向。

I thought perhaps it would help if I added the bit of code that is calling this so maybe it will be more clear how I am trying to use it, so here is the addAppointment() method, so you can see where getDay() is being called and where it's going.

public static void addAppointment() throws SQLException
{
    //get the info
    String desc = getDesc();
    java.sql.Date apptDay = getDay();
    Time[] times = getTime();
    Time startTime = times[0];
    Time endTime = times[1];
    int key;

    Connection conn = SimpleDataSource.getConnection(); //connect to the database

    try
    {
        PreparedStatement max = conn.prepareStatement("SELECT MAX(ID) FROM Calendar");
        ResultSet result = max.executeQuery();
        key = result.getInt("ID") + 1; 
        PreparedStatement stat = conn.prepareStatement(
                "INSERT INTO Calendar " +
                "VALUES (?, ?, ?, ?, ?)"); 
        stat.setInt(1, key);
        stat.setString(2, desc); 
        stat.setDate(3, apptDay);
        stat.setTime(4, startTime);
        stat.setTime(5, endTime);
        stat.execute();
        System.out.println("\nAppointment added!\n");
    }
    finally
    {
        conn.close(); //finished with the database
    }
}


推荐答案

将输入格式更改为 yyyy-MM-dd 并使用 java.sql.Date.valueOf(字符串日期)方法,该方法将上述格式的字符串直接转换为java.sql.Date值。

It would be much simpler to change the input format to yyyy-MM-dd and use java.sql.Date.valueOf(String date) method which converts a string in the above format to a java.sql.Date value directly.

这篇关于字符串到java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆