我在从不同表格的列中填充的值时遇到问题,而不是我想要从中提取的表格。
从mySQL中,我得到了所需的null结果;但是,从程序中,它显示的值(如果我为该行指定了一个值)或来自不同表的值。这甚至在使用别名并明确说明要从哪个表中提取时。
我有一个设备表和一个设备类型表。设备表具有与设备相同的所有字段,但它只是1和0表示需要哪些字段。 (这在生成编辑/创建设备GUI表单时使用。)
仅当两个表中的值为resultSet.getInt("column_with_issue")
时使用INT
时才会发生这种情况。只要类型不同,它在我resultSet.getString("column_with_no_issues")
时返回一个正确的空值。
这是我的原始查询,以及我根据修改建议尝试更改的内容。
SELECT d.*, dt.type, os.os_name FROM device d inner join device_type dt on d.type_id = dt.id left outer join operating_system os on d.os = os.id
WHERE d.division_number = 1 or 1
AND (d.asset_tag = 1 or 1)
AND (dt.type = 1 or 1);
其他人的stackoverflow / javaranch问题中描述的可能修复。这将为每个特定列提供自己的名称。然而,即使我去拉(作为一个具有两种不同变量类型的示例)dwireless
,我仍然得到应该来自device_type.wireless
的那种设备的整数值,而不是预期的空值。
SELECT d.id as did, d.division_number as ddivision_number, d.current_location as dcurrent_location, d.type_id as dtype_id, d.model as dmodel, d.asset_tag as dasset_tag, d.serial_number as dserial_number, d.manufacture_date as dmanufacture_date, d.screen as dscreen, d.os as dos, d.users_name as dusers_name, d.office_installed as doffice_installed, d.memory as dmemory, d.series as dseries, d.ip_address as dip_address, d.host_name as dhostname, d.vm_host as dvm_host, d.processor as dprocessor, d.wireless as dwireless, d.purchase_quality as dpurchase_quality, d.purchase_price as dpurchase_price, d.active as dactive, d.notes as dnotes, dt.type, os.os_name
FROM device d inner join device_type dt on d.type_id = dt.id left outer join operating_system os on d.os = os.id
WHERE d.division_number = 1 or 1
AND (d.asset_tag = 1 or 1)
AND (dt.type = 1 or 1);
这是我用来从结果集中提取变量的Java代码。
private ObservableList<Device> getDevices(String sqlStatement) throws SQLException {
ObservableList<Device> devices = FXCollections.observableArrayList();
// Run the query to select all of our devices.
Statement searchDeviceStatement = Main.myConn.createStatement();
ResultSet rs = searchDeviceStatement.executeQuery(sqlStatement);
// Add all of our devices into our array list to be displayed.
// These are the actual columns of the database.
while (rs.next()) {
devices.add(new Device(rs.getInt("did"), rs.getInt("ddivision_number"), rs.getInt("dtype_id"),
rs.getString("type"), rs.getString("dmodel"), rs.getInt("dasset_tag"), rs.getString("dserial_number"),
rs.getInt("dmanufacture_date"), rs.getString("dscreen"), rs.getInt("dos"), rs.getString("os_name"),
rs.getString("dusers_name"), rs.getInt("doffice_installed"), rs.getString("dmemory"),
rs.getString("dseries"), rs.getString("dip_address"), rs.getString("dhost_name"),
rs.getString("dvm_host"), rs.getString("dprocessor"), rs.getInt("dwireless"),
rs.getString("dpurchase_quality"), rs.getDouble("dpurchase_price"), rs.getInt("dactive"), rs.getString("dnotes")));
}
return devices;
}
答案 0 :(得分:0)
答案:我是个白痴。整数的默认值为0,而字符串为null。 JDBC返回null但是当它创建具有该值的Device对象时,它默认为0.这在this页面上的1/3处被解释。