将日期和时间传递到sql server 2014返回java.lang.NumberFormatException
这篇文章与我以前的文章有关。 最近,我有一个项目,其中我应该向SQL Server 2014发送一个字符串和两个datetime字段,并获取一些字段 所以我创建了两个日期选择器,最终用户可以选择他想要的日期。 和日历的实例。 在我朋友的帮助下,我一直使用这个:将日期和时间传递到sql server 2014返回java.lang.NumberFormatException,java,android,sql-server,datetime,datetime-format,Java,Android,Sql Server,Datetime,Datetime Format,这篇文章与我以前的文章有关。 最近,我有一个项目,其中我应该向SQL Server 2014发送一个字符串和两个datetime字段,并获取一些字段 所以我创建了两个日期选择器,最终用户可以选择他想要的日期。 和日历的实例。 在我朋友的帮助下,我一直使用这个: Calendar finalCalendar = Calendar.getInstance(); int fDay = datePicker2.getDayOfMonth();
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的时间,也是分、秒和毫秒
我尝试在下面的代码中查看代码返回的时间是否为真:
Toast.makeText(getApplicationContext(), getFinalDate(), Toast.LENGTH_SHORT).show();
它显示了我从DatePicker中选择的正确时间。
当我在SQLServer管理控制台中插入这种时间格式时,它可以正常工作
我从android将其传递给SQL Server存储过程,如下所示
callableStatement.setDate(3, java.sql.Date.valueOf(getFinalDate()));
我在谷歌尝试了不同的方法。
SimpleDataFormat的不同格式。
我想在某个地方我犯了一个错误。
任何帮助都将不胜感激
我有一个正确工作的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();
}
第一个字段是字符串,我从另一个DB表中获取。
如下所示(在onCreate方法中):
我在MSSQL Server Management Studio中复制了SP的执行代码。(如上)我看不到您的
getFinalDate()
返回了什么我猜字符串yyyy-MM-dd hh:MM:ss
,正确的方式是java.sql.Date.valueOf
应该是yyyy-MM-dd
,不包括时间
但是既然您有了日历
对象,为什么不这样做(避免所有解析)
重要的是数据库中的列的类型为DATE
如果是时间戳
或日期时间
,则应使用:
callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));
是的,在这种情况下,在valueOf中传递字符串时,字符串格式应为yyyy-MM-dd hh:MM:ss
编辑:这与原始问题无关,只是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();
}
如果可调用语句是一个查询(returnresultset),则需要注册器输出,而不需要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();
谢谢你的回复。但我试了你说的两种方法。并输入一个返回结果集数量的整数。通过这种方式:if(resultSet.next()){do{Toast.makeText(getApplicationContext(),“Bingoo”,Toast.LENGTH_SHORT.).show();i++;}while(resultSet.next());并打印i after while循环,但它返回默认值,即0。在Logcat中,您所说的这些方式没有错误。(对于yyyy MM dd),SQL Server中的字段类型为datetime.callableStatement.setTimestamp(…)是datetime类型的正确方式,如果未插入,请检查是否有callableStatement.addBatch();callableStatement.executeBatch();connection.commit();(如果它不在自动提交上),如果插入执行insert/updateThx的代码时仍然存在编辑问题。我现在编辑它。我以前使用过此数据库。我现在编辑它。@alireza azadi。抱歉,无法理解注释“我现在编辑它”,问题?,还是现在你编辑代码,让它工作?问题伙计。:-D
callableStatement.setTimestamp(3, new java.sql.Timestamp(finalCalendar.getTimeInMillis()));
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();