MySql中的Row_Number()结果值为Double,但在IBM Data Studio结果Int中

时间:2016-09-28 23:35:03

标签: java mysql swing

我希望java中的结果值与IBM Data Studio中的Int一样,但在我的情况下是java生成double值,我不知道为什么?,请帮忙修复它!

这是我在表

中生成Number的java代码

code vector

private void polDatToTab(ResultSet rs, JTable table) throws SQLException{
        String[] colHead = new String[] {"No","NIK","Nama"};
        DefaultTableModel tm = new DefaultTableModel();
        ResultSetMetaData rsd = rs.getMetaData();

        Vector<String> nameCol = new Vector<String>();
        int kolCount = rsd.getColumnCount();
        for(int i=0;i<colHead.length;i++){
            nameCol.add(colHead[i]);
        }

        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while(rs.next()){
        Vector<Object> vec = new Vector<Object>();
        for(int j=1;j<=kolCount;j++){
            vec.add(rs.getObject(j));
        }
           data.add(vec);
        }
        tm.setDataVector(data, nameCol);
        table.setModel(tm);
    }

这是我在Gui中显示Table的java代码

enter image description here

private void srcEmp(){
        String srcE = "SELECT (@ROW_NUMBER:=@ROW_NUMBER + 1) AS  No_Urut,NIK,NAMA FROM PAYROLL.KARYAWAN,"
                + "(SELECT @ROW_NUMBER:=0) AS T WHERE NAMA LIKE '%"+srcRes+"%'";
        DbConnect co = new DbConnect();
        co.connectDB();

        try {
            st = co.connection.createStatement();
            ResultSet ul = st.executeQuery(srcE);
            polDatToTab(ul, tabResSrc);
        } catch (SQLException ex) {
            Logger.getLogger(ResSrc.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

这个我在IBM Data Studio中的sql代码在int

中生成true值
    SELECT (@ROW_NUMBER:=@ROW_NUMBER + 1) AS No_Urut,NIK,NAMA 
FROM PAYROLL.KARYAWAN,(SELECT @ROW_NUMBER:=0) AS T 
WHERE NAMA LIKE '%"+srcRes+"%'

这是我在Java Gui中的结果:

enter image description here

和我在IBM Data Studio中的结果

enter image description here

1 个答案:

答案 0 :(得分:1)

您的polDatToTab方法并不通用,看起来它只能用于此特定结果集。由于这一行,我正在跳到那个结论

String[] colHead = new String[] {"No","NIK","Nama"};

因此,您知道第一列应该是一个int。因此,

    vec.add(rs.getInt(1));
    for(int j=2; j<=kolCount; j++){
        vec.add(rs.getObject(j));            
    }

技巧