转换为java.sql.Date以支持dd-MMM-yy格式。

huangapple go评论80阅读模式
英文:

Convertion to java.sql.Date for dd-MMM-yy format

问题

我正在使用JDBC,并调用一个带有日期参数的存储过程,但我的数据库表日期格式是dd-MMM-yy格式,因此我将我的字符串日期转换为dd-MMM-yy格式,但我无法使用setDate(1, sdt),因为sdt必须是java.sql.Date类型,而java.sql.Date的格式是yyyy-MM-dd,因此我需要帮助。

我的存储过程定义如下:

PROCEDURE pStoreData(d_sumDttm IN DATE, i_Retval out number);

简短的代码:

System.out.print("Enter report date:");
String sdate = scanner.nextLine();
final Date date = new Date();
final SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("dd-MMM-yy");
final String sysdt = format.format(date);
java.sql.Date sqldt = java.sql.Date.valueOf(sysdt);
callablestate = connection.prepareCall("{call Report.pStoreDate(?,?)}");
callablestate.setDate(1, sqldt);
callablestate.registerOutParameter(2, Types.REF_CURSOR);
callable.execute();
英文:

I am doing jdbc and calling a procedure with date parameter but my db tables date format is in dd-MMM-yy format hence i converted my string date into dd-MMM-yy format but I am unable to setDate(1,sdt) cuz sdt must be in java.sql.Date type and java.sql.Date format is yyyy-MM-dd hence I need help

My procedure is defined thus:

PROCEDURE pStoreData(d_sumDttm IN DATE, i_Retval out number);

Short Code:

    System.out.print("Enter report date:");
    String sdate = scanner.nextLine();
    final Date date = new Date();
    final SimpleDateFormat format = new SimpleDateFormat();
    format.applyPattern(dd-MMM-yy);
    final String sysdt = format.format(date);
    java.sql.Date sqldt = java.sql.Date.valueOf(sysdt);
    callablestate = connection.prepareCall("{call Report.pStoreDate(?,?)}");
    callablestate.setDate(1,sqldt);
    callablestate.registerOutParameter(2,Types.REF_CURSOR);
    callable.execute();

答案1

得分: 2

更新

由于楼主似乎在如何使用原始解决方案方面遇到了困难,因此发布此更新。

亲爱的楼主,

Java 只以一种方式实例化日期/时间/日期时间,然后您可以以自定义方式格式化它。数据库也是同样的方式工作。因此,不管您向用户显示日期的格式是什么,或者用户以什么格式输入日期;一旦您通过应用相应的格式将其解析为日期/时间/日期时间对象,您只需将其传递给 Java/数据库,Java/数据库将会处理其余部分。

import java.time.LocalDate;
import java.util.Scanner;

import com.mysql.jdbc.CallableStatement;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter report date in MM-dd-yyyy format: ");
        String strDate = scanner.nextLine();
        LocalDate localDate = LocalDate.parse(strDate, DateTimeFormatter.ofPattern("MM-dd-yyyy"));
        CallableStatement st = conn.prepareCall("{call Report.pStoreDate(?,?)}");
        st.setObject(1, localDate);
        st.registerOutParameter(2, Types.REF_CURSOR);
        st.execute();
    }
}

原始答案

我建议您不要使用过时且容易出错的 java.util.Date。请改为使用如下所示的 LocalDate

LocalDate localDate = LocalDate.now();
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, localDate);
st.executeUpdate();
英文:

Update

Posting this update since OP seems to be struggling with how to use the original solution.

Dear OP,

Java instantiates date/time/date-time in just one way and then you can format it in your custom way. The database works the same way. So, it doesn't matter what format you display to the user or in what format the user enters the date; once you parse it into date/time/date-time object by applying the corresponding format, you just pass it to Java/DB and Java/DB will take care of the rest.

import java.time.LocalDate;
import java.util.Scanner;

import com.mysql.jdbc.CallableStatement;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter report date in MM-dd-yyyy format: ");
		String strDate = scanner.nextLine();
		LocalDate localDate = LocalDate.parse(strDate, DateTimeFormatter.ofPattern("MM-dd-yyyy"));
		CallableStatement st = conn.prepareCall("{call Report.pStoreDate(?,?)}");
		st.setObject(1, localDate);
		st.registerOutParameter(2, Types.REF_CURSOR);
		st.execute();
	}
}

Original answer:

I suggest you do not use the outdated and error-prone java.util.Date. Use LocalDate instead as shown below:

LocalDate localDate = LocalDate.now();
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, localDate);
st.executeUpdate();

huangapple
  • 本文由 发表于 2020年8月10日 03:04:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63330240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定