当前位置: 技术问答>java相关
如何返回查询结果ResultSet中int类型的数据
来源: 互联网 发布时间:2015-06-20
本文导语: 使用JDBC连接数据库,查询某表中数据,结果返回至ResultSet rs 可正常获得字符类型数据rs.getString("..."), 但对于int类型数据 使用rs.getString("...")产生类型不匹配错误 使用rs.getInt("...")产生服...
使用JDBC连接数据库,查询某表中数据,结果返回至ResultSet rs
可正常获得字符类型数据rs.getString("..."),
但对于int类型数据
使用rs.getString("...")产生类型不匹配错误
使用rs.getInt("...")产生服务器内部错误(500 Internal server...)
希望能得到大家的帮助,非常感谢!
可正常获得字符类型数据rs.getString("..."),
但对于int类型数据
使用rs.getString("...")产生类型不匹配错误
使用rs.getInt("...")产生服务器内部错误(500 Internal server...)
希望能得到大家的帮助,非常感谢!
|
protected String helper (ResultSet rs, int dataType, int col)
throws SQLException
{
String retVal = null;
Integer intObj;
// ask for data depending on the datatype
switch(dataType) {
case Types.DATE:
java.sql.Date date = rs.getDate(col);
retVal = date.toString();
break;
case Types.TIME:
java.sql.Time time = rs.getTime(col);
retVal = time.toString();
break;
case Types.TIMESTAMP:
java.sql.Timestamp timestamp = rs.getTimestamp(col);
retVal = timestamp.toString();
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
retVal = rs.getString(col);
break;
case Types.NUMERIC:
case Types.DECIMAL:
java.math.BigDecimal numeric = rs.getBigDecimal(col, 10);
retVal = numeric.toString();
break;
case Types.BIT:
boolean bit = rs.getBoolean(col);
Boolean boolObj = new Boolean(bit);
retVal = boolObj.toString();
break;
case Types.TINYINT:
byte tinyint = rs.getByte(col);
intObj = new Integer(tinyint);
retVal = intObj.toString();
break;
case Types.SMALLINT:
short smallint = rs.getShort(col);
intObj = new Integer(smallint);
retVal = intObj.toString();
break;
case Types.INTEGER:
int integer = rs.getInt(col);
intObj = new Integer(integer);
retVal = intObj.toString();
break;
case Types.BIGINT:
long bigint = rs.getLong(col);
Long longObj = new Long(bigint);
retVal = longObj.toString();
break;
case Types.REAL:
float real = rs.getFloat(col);
Float floatObj = new Float(real);
retVal = floatObj.toString();
break;
case Types.FLOAT:
case Types.DOUBLE:
double longreal = rs.getDouble(col);
Double doubleObj = new Double(longreal);
retVal = doubleObj.toString();
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
byte[] binary = rs.getBytes(col);
retVal = new String(binary);
break;
}
return retVal;
}
把所有的方法给你:
你的rs.getInt("...")中的字段为整型吗?
throws SQLException
{
String retVal = null;
Integer intObj;
// ask for data depending on the datatype
switch(dataType) {
case Types.DATE:
java.sql.Date date = rs.getDate(col);
retVal = date.toString();
break;
case Types.TIME:
java.sql.Time time = rs.getTime(col);
retVal = time.toString();
break;
case Types.TIMESTAMP:
java.sql.Timestamp timestamp = rs.getTimestamp(col);
retVal = timestamp.toString();
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
retVal = rs.getString(col);
break;
case Types.NUMERIC:
case Types.DECIMAL:
java.math.BigDecimal numeric = rs.getBigDecimal(col, 10);
retVal = numeric.toString();
break;
case Types.BIT:
boolean bit = rs.getBoolean(col);
Boolean boolObj = new Boolean(bit);
retVal = boolObj.toString();
break;
case Types.TINYINT:
byte tinyint = rs.getByte(col);
intObj = new Integer(tinyint);
retVal = intObj.toString();
break;
case Types.SMALLINT:
short smallint = rs.getShort(col);
intObj = new Integer(smallint);
retVal = intObj.toString();
break;
case Types.INTEGER:
int integer = rs.getInt(col);
intObj = new Integer(integer);
retVal = intObj.toString();
break;
case Types.BIGINT:
long bigint = rs.getLong(col);
Long longObj = new Long(bigint);
retVal = longObj.toString();
break;
case Types.REAL:
float real = rs.getFloat(col);
Float floatObj = new Float(real);
retVal = floatObj.toString();
break;
case Types.FLOAT:
case Types.DOUBLE:
double longreal = rs.getDouble(col);
Double doubleObj = new Double(longreal);
retVal = doubleObj.toString();
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
byte[] binary = rs.getBytes(col);
retVal = new String(binary);
break;
}
return retVal;
}
把所有的方法给你:
你的rs.getInt("...")中的字段为整型吗?
|
可能是你的字段类型不是int型的,可以先getString(),再
Integer.parseInt(s);
Integer.parseInt(s);
|
用rs.getString("...").trim()
|
rs.getObject("d");
|
ResultSetMetaData
int getColumnType(int column)
Retrieves the designated column's SQL type.
int getColumnType(int column)
Retrieves the designated column's SQL type.