语句 return 错误的值
Statement return wrong values
我有一个使用 HSQLDB
的 JAVA 应用程序。有一个名为 'tblUser'
的 table 包含一个名为 'tongHH'
的列 bigint
类型。如果我使用 HSQLDB DatabaseManager 运行 以下查询:
select sum(tongHH) from tblUser.
DBM returns 正确的值
如果我使用以下 JAVA 代码通过相同的查询读出此数据,java 总是打印出错误的值。我得到 65,我预计 3905
public int getParentID(String query) {
int a = 0;
Connection conn = null;
ResultSet rs = null;
try {
conn = MyConection.getConnection();
Statement st = conn.createStatement();
rs = st.executeQuery(query);
while(rs.next()){
a=rs.getByte(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
MyConection.closeConnection(conn, null, rs);
} catch (Exception e) {
}
}
return a;
}
你想return一个int,但是当你从记录集中读取值时,你使用的是getByte(1),应该是getInt(1)
您试图从 ResultSet
中检索的 byte
溢出了。
作为 int
的值 3905
是:
00000000 00000000 00001111 01000001
获取值作为 byte
只保留最后 8 位,或者
01000001
即65
.
改为调用 the getInt
method。
我有一个使用 HSQLDB
的 JAVA 应用程序。有一个名为 'tblUser'
的 table 包含一个名为 'tongHH'
的列 bigint
类型。如果我使用 HSQLDB DatabaseManager 运行 以下查询:
select sum(tongHH) from tblUser.
DBM returns 正确的值 如果我使用以下 JAVA 代码通过相同的查询读出此数据,java 总是打印出错误的值。我得到 65,我预计 3905
public int getParentID(String query) {
int a = 0;
Connection conn = null;
ResultSet rs = null;
try {
conn = MyConection.getConnection();
Statement st = conn.createStatement();
rs = st.executeQuery(query);
while(rs.next()){
a=rs.getByte(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
MyConection.closeConnection(conn, null, rs);
} catch (Exception e) {
}
}
return a;
}
你想return一个int,但是当你从记录集中读取值时,你使用的是getByte(1),应该是getInt(1)
您试图从 ResultSet
中检索的 byte
溢出了。
作为 int
的值 3905
是:
00000000 00000000 00001111 01000001
获取值作为 byte
只保留最后 8 位,或者
01000001
即65
.
改为调用 the getInt
method。