本帖最后由 Samding 于 2020-5-15 20:42 编辑
rs.getObject(3)转成整型要在前面加java.math.BigDecimal是什么意思,不加会报错,
rs.getObject(3)不本来就是BigDecimal类型的吗
完整代码:
[Java] 纯文本查看 复制代码 package com.rl.jdbc;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCQuery{
public static void main(String[] args) {
String driverClass = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String username = "scott";
String password = "samding";
Connection conn = null;
Statement stmt = null;
String sql = "select * from person";
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
Object id = rs.getObject("id");
Object pname = rs.getObject(2);
Object gender = ((java.math.BigDecimal) rs.getObject(3)).intValue()==1?"男":"女";
System.out.println(rs.getObject(3).getClass());
System.out.println("编号:"+id+" 姓名:"+pname+" 性别:"+gender);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(stmt != null) {
stmt.close( );
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
person表
|