【问题标题】:Pass Date and Time to sql server 2014 returns java.lang.NumberFormatException将日期和时间传递给 sql server 2014 返回 java.lang.NumberFormatException
【发布时间】:2023-04-06 01:56:01
【问题描述】:

这篇文章与我之前的文章有关。 最近我有一个项目,我应该将一个字符串和两个日期时间字段发送到 SQL Server 2014 并获取一些字段。(位置) 所以我创建了两个 DatePicker,最终用户可以选择他想要的日期。 和日历的实例。 好吧,在我朋友的帮助下,我一直用到现在:

            Calendar finalCalendar = Calendar.getInstance();
            int fDay = datePicker2.getDayOfMonth();
            int fMonth = datePicker2.getMonth();
            int fYear = datePicker2.getYear();
            finalCalendar.set(Calendar.YEAR, fYear);
            finalCalendar.set(Calendar.MONTH, fMonth);
            finalCalendar.set(Calendar.DAY_OF_MONTH, fDay);
            finalCalendar.set(Calendar.HOUR, 0);
            finalCalendar.set(Calendar.MINUTE, 0);
            finalCalendar.set(Calendar.SECOND, 0);
            finalCalendar.set(Calendar.MILLISECOND, 0);
            SimpleDateFormat fSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            setFinalDate(fSimpleDateFormat.format(finalCalendar.getTime()));

好吧,当我把它传给我的 SP 时,它给了我:

java.lang.NumberFormatException: Invalid int: "18 12:00:00.000"

18 是我选择的那一天,你看到的 12 是我设置为 0 的时间,也是 Minute、Second 和 mili 秒。

我尝试下面的代码来查看代码返回的时间是否为真:

            Toast.makeText(getApplicationContext(), getFinalDate(), Toast.LENGTH_SHORT).show();

它显示了我从 DatePicker 中选择的正确时间。 当我在 SQL Server 管理控制台中插入这种格式的时间时,它可以正常工作。

我将其从 android 传递给 SQL Server 存储过程,如下所示。

callableStatement.setDate(3, java.sql.Date.valueOf(getFinalDate()));

我在 google 中尝试了不同的方法。 SimpleDateFormat 的不同格式。 我想在某个地方我犯了一个错误。 任何帮助将不胜感激。

我有一个可以正常工作的 ConnectionHelper 类。 我这个例子。 并调用 CallableStatement。

            CallableStatement callableStatement;
            ConnectionHelper connectionHelper1 = new ConnectionHelper();

            try {
                callableStatement = connectionHelper1.getMyConnection().prepareCall("{call SpSelectTrackVehiclePath(?, ?, ?)}");
                callableStatement.setString(1, IMEArrayAdapter.getItem(listView.getCheckedItemPosition()));//IMEArrayAdapter in below will explain,gives the first field of SP.


                callableStatement.setTimestamp(2, new java.sql.Timestamp(startCalendar.getTimeInMillis()));
                callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));

                callableStatement.addBatch();
                callableStatement.executeBatch();

                ResultSet resultSet = callableStatement.executeQuery();

                if(resultSet.next()) {
                    do {
                        Toast.makeText(getApplicationContext(), "Bingoo", Toast.LENGTH_SHORT).show();
                        spOut.add(resultSet.getDouble("Latitude"));
                        i++;
                    } while (resultSet.next());
                }else
                {
                    Toast.makeText(getApplicationContext(), "Error ! ", Toast.LENGTH_SHORT).show();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

第一个字段是我从另一个数据库表中获得的字符串。 像这样(在 onCreate 方法中):

ConnectionHelper connectionHelper = new ConnectionHelper();
    try {
        Statement statement = connectionHelper.getMyConnection().createStatement();
        ResultSet resultSet = statement.executeQuery("select Id from Device");
        while(resultSet.next())
        {
            String ime;
            ime = resultSet.getString("Id");
            IMEIs.add(ime); //This is ArrayList<String> Type
            IMEArrayAdapter.getItemViewType(R.id.listView); //get the listView id in XML,
            listView.setAdapter(IMEArrayAdapter); //with this line it set Adapter and all id shows in my ListView
        }
        connectionHelper.closeConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        connectionHelper.closeConnection();
    }

在 MSSQL 服务器管理中,当我使用字段执行此 SP 时,如下所示: IMEI : xxxxxxx 第一时间:2015-11-18 12:00:00:00 最后日期:2015-11-24 12:00:00:00 它返回值。 我这样执行的是 MSSQL Server Management Studio:

USE [xxxxx]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[xxxxxx]
        @DeviceId = N'xxxxxxxxxxxx',
        @DateTimeBegin = N'2015-11-18 00:00:00:000',
        @DateTimeEnd = N'2015-11-24 00:00:00:000'

SELECT  'Return Value' = @return_value

GO

我在MSSQL Server Management Studio中复制SP的执行代码。(上)

【问题讨论】:

    标签: java android sql-server datetime datetime-format


    【解决方案1】:

    我看不到你的getFinalDate()返回什么我猜是字符串yyyy-MM-dd hh:mm:ssjava.sql.Date.valueOf上的正确方式应该是yyyy-MM-dd,不包括时间

    但是既然你有 Calender 对象为什么不做这样的事情(避免所有的解析)

    callableStatement.setDate(3, new java.sql.Date(finalCalendar.getTimeInMillis()));
    

    重要是您的数据库列的类型为DATE

    如果是TIMESTAMPDATETIME,你应该使用:

    callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));
    

    是的,在这种情况下,在 valueOf 中传递字符串,字符串格式应该是yyyy-MM-dd hh:mm:ss

    编辑:这与原始问题无关,而是对 CallableStatement 的跟进。

    如果可调用语句是查询(返回结果集),您需要注册输出并且您没有addBatch 代码将是这样的:

    callableStatement = connectionHelper1.getMyConnection().prepareCall("{call SpSelectTrackVehiclePath(?, ?, ?,?)}");
    callableStatement.setString(1, IMEArrayAdapter.getItem(listView.getCheckedItemPosition()));//IMEArrayAdapter in below will explain,gives the first field of SP.
    
    callableStatement.setTimestamp(2, new java.sql.Timestamp(startCalendar.getTimeInMillis()));
    callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));
    
    callableStatement.registerOutParameter(4, java.sql.Types.INTEGER);
    
    ResultSet resultSet = callableStatement.executeQuery();
    

    【讨论】:

    • 感谢您的回复。但我尝试了你所说的两种方式。并放一个整数,返回resultSet的个数。通过这种方式: if(resultSet.next()) { do { Toast.makeText(getApplicationContext(), "Bingoo", Toast.LENGTH_SHORT).show();我++; } while (resultSet.next());并在 while 循环后打印 i,但它返回默认值,即 0。在 Logcat 中,您所说的这些方式没有错误。(并且使用 yyyy-MM-dd)SQL Server 中的字段类型是 datetime。跨度>
    • callableStatement.setTimestamp(...) 是日期时间类型的正确方法,如果没有插入,请检查您是否有 callableStatement.addBatch(); callableStatement.executeBatch();连接.commit(); (如果它没有自动提交),如果你仍然有问题编辑问题插入你正在执行插入/更新的代码
    • 谢谢。我现在编辑它。我以前使用过这个数据库。我现在编辑它。
    • @alireza azadi。抱歉无法理解评论,“我现在编辑它”,问题?,或者现在您编辑代码使其工作?
    • 问题老兄。 :-D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2017-10-08
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多