将日期和时间传递给 sql 服务器 2014 returns java.lang.NumberFormatException
Pass Date and Time to sql server 2014 returns java.lang.NumberFormatException
这个post和我之前的post有关。
最近我有一个项目,我应该将一个字符串和两个日期时间字段发送到 SQL Server 2014 并获取一些字段。(位置)
所以我创建了两个 DatePicker,最终用户可以 select 他想要的日期。
和日历的实例。
好吧,在我这里的朋友的帮助下,我一直用到现在:
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的时间,还有分,秒,毫秒。
然后我尝试使用下面的代码来查看代码 returns 的时间是否正确:
Toast.makeText(getApplicationContext(), getFinalDate(), Toast.LENGTH_SHORT).show();
它显示了我从 DatePicker 中选择的正确时间。
当我在 SQL 服务器管理控制台中插入这种时间格式时,它可以正常工作。
然后我将其从 android 传递给 SQL 服务器存储过程,如下所示。
callableStatement.setDate(3, java.sql.Date.valueOf(getFinalDate()));
我在 google 中尝试了不同的方法。
不同格式的 SimpleDateFormat。
我想我在某个地方犯了一个错误。
任何帮助将不胜感激。
我有一个 ConnectionHelper Class 可以正常工作。
我实例化了这个。
并调用 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();
}
第一个字段是我从另一个 Table 数据库中获取的字符串。
像这样(在 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
它 returns 值。
我这样执行的是 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的执行代码。(上)
我看不出你的 getFinalDate()
是什么 return 我猜是字符串 yyyy-MM-dd hh:mm:ss
,java.sql.Date.valueOf
上的正确方法应该是 yyyy-MM-dd
,不包括时间[=23=]
但是既然你有 Calender
对象为什么不做这样的事情(避免所有解析)
callableStatement.setDate(3, new java.sql.Date(finalCalendar.getTimeInMillis()));
重要 是您在数据库中的列类型为 DATE
如果是 TIMESTAMP
或 DATETIME
你应该使用:
callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));
是的,在这种情况下,在 valueOf 中传递字符串,字符串格式应为 yyyy-MM-dd hh:mm:ss
编辑:这与原始问题无关,而是对 CallableStatement 的跟进。
如果可调用语句是一个查询(return 结果集),您需要注册输出并且不需要 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();
这个post和我之前的post有关。 最近我有一个项目,我应该将一个字符串和两个日期时间字段发送到 SQL Server 2014 并获取一些字段。(位置) 所以我创建了两个 DatePicker,最终用户可以 select 他想要的日期。 和日历的实例。 好吧,在我这里的朋友的帮助下,我一直用到现在:
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的时间,还有分,秒,毫秒。
然后我尝试使用下面的代码来查看代码 returns 的时间是否正确:
Toast.makeText(getApplicationContext(), getFinalDate(), Toast.LENGTH_SHORT).show();
它显示了我从 DatePicker 中选择的正确时间。 当我在 SQL 服务器管理控制台中插入这种时间格式时,它可以正常工作。
然后我将其从 android 传递给 SQL 服务器存储过程,如下所示。
callableStatement.setDate(3, java.sql.Date.valueOf(getFinalDate()));
我在 google 中尝试了不同的方法。 不同格式的 SimpleDateFormat。 我想我在某个地方犯了一个错误。 任何帮助将不胜感激。
我有一个 ConnectionHelper Class 可以正常工作。 我实例化了这个。 并调用 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();
}
第一个字段是我从另一个 Table 数据库中获取的字符串。 像这样(在 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 它 returns 值。 我这样执行的是 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的执行代码。(上)
我看不出你的 getFinalDate()
是什么 return 我猜是字符串 yyyy-MM-dd hh:mm:ss
,java.sql.Date.valueOf
上的正确方法应该是 yyyy-MM-dd
,不包括时间[=23=]
但是既然你有 Calender
对象为什么不做这样的事情(避免所有解析)
callableStatement.setDate(3, new java.sql.Date(finalCalendar.getTimeInMillis()));
重要 是您在数据库中的列类型为 DATE
如果是 TIMESTAMP
或 DATETIME
你应该使用:
callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));
是的,在这种情况下,在 valueOf 中传递字符串,字符串格式应为 yyyy-MM-dd hh:mm:ss
编辑:这与原始问题无关,而是对 CallableStatement 的跟进。
如果可调用语句是一个查询(return 结果集),您需要注册输出并且不需要 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();