简体   繁体   English

在Java中接受日期类型的命令行参数

[英]accepting date type command line argument in java

There is this practice CRUD application I'm trying to write. 我正在尝试编写此练习CRUD应用程序。 It's an app to store contacts with birth date & stuff. 这是一个用于存储联系人的生日和事物的应用程序。 Since I'm using command line to accept input, & storing this in HSQLDB database, I'm not sure how to accept a Date type argument. 由于我使用命令行来接受输入并将其存储在HSQLDB数据库中,因此我不确定如何接受Date类型的参数。 I tried parsing it to long from Sting, But it keeps throwing an SQLDataException . 我试图从Sting解析它很长时间,但是它一直抛出SQLDataException

here r the lines of code that i use for this variable 这是我用于此变量的代码行

//this in the class with main

    private Date dob;

    public Date getDob() {
    return dob;
        }



    public void setDob(Date dob) {
            this.dob = dob;
        }

    System.out.println("Enter date of birth of "+n+" in the format dd/mm/yyyy
    String date = sc2.nextLine();
    long d = Date.parse(date);
    uv.setDob(d);

//class with all the business logic

public void addContact(String cb, String name, long dob, String email, String phno, String tag) {
sql = "insert into "+ cb + "(name,dob,email,phone,tag) values (?,?,?,?,?)";
 try {
    con = JDBCHelper.getConnection();
    ps1 = con.prepareStatement(sql);
    ps1.setString(1, name);
    ps1.setLong(2, dob);
    ps1.setString(3, email);
    ps1.setString(4,phno);
    ps1.setString(5, tag);
 }   
 catch(SQLException e) {
    e.printStackTrace();
 }

}

exception it gives me on entering the date is 它给我输入日期的例外是

java.sql.SQLSyntaxErrorException: incompatible data type in conversion
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.setLongParameter(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.setLong(Unknown Source)
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.types.DateTimeType.convertJavaToSQL(Unknown Source)

You have to create a sql Date . 您必须创建一个sql Date The constructor takes a long so you should be fine. 构造函数需要很长时间,所以您应该没问题。

ps1.setDate(2, new java.sql.Date(dob));

To parse string to date you may use the class SimpleDateFormat , eg: 要解析日期字符串,可以使用SimpleDateFormat类,例如:

SimpleDateFormat simpleDateFormat =
        new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
Date date = simpleDateFormat.parse(date);

Then you may use the setDate(int, Date) method of PreparedStatement . 然后,您可以使用PreparedStatementsetDate(int, Date)方法。

Parse the date using SimpleDateFormat as the first step and ensure that the the inputs is in the format you desire. 第一步,使用SimpleDateFormat解析日期,并确保输入的格式符合您的要求。 You can then use the same value in your prepared statement. 然后,您可以在准备好的语句中使用相同的值。

Also, try this 另外,试试这个

For arbitrary date formats use SimpleDateFormat to get java.util.Date , and then convert it into JDBC date: java.sql.Date sqlDate = new java.sql.Date(date.getTime())) . 对于任意日期格式,请使用SimpleDateFormat获取java.util.Date ,然后将其转换为JDBC日期: java.sql.Date sqlDate = new java.sql.Date(date.getTime())) For SQL-compatible format (yyyy-MM-dd) there's java.sql.Date.valueOf(String) helper. 对于与SQL兼容的格式(yyyy-MM-dd),有java.sql.Date.valueOf(String)帮助器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM