如何从向量中获取特定索引的值?

时间:2013-04-03 06:22:38

标签: java vector

当我使用elementAt(2)返回矢量数据时,我得到了这个输出。

Hello [162, Experiment 3.doc, E:\Desktop\Experiment 3.doc, doc, 35.5 kb]

我想索引值:“Experiment 3.doc”。

// Function to fetch data from Database and store in jtable
public Vector getEmployee(String searchQuery)throws Exception
{
    Connection con = null;
    try{
        Class.forName(driver); 
    } catch(java.lang.ClassNotFoundException e) {
        e.printStackTrace();
    }

    try{
        Vector<Vector<String>> employeeVector = new Vector<>();
        con = DriverManager.getConnection(url,"conjure","conjure");

        String query = "SELECT * FROM APP.FILES WHERE NAME LIKE '%"+searchQuery+"%'";
        Statement st = con.createStatement();

        ResultSet rs = st.executeQuery(query);

        while(rs.next()) {
            Vector<String> file = new Vector<>();
            file.add(rs.getString(1)); //Empid
            file.add(rs.getString(2)); //name
            file.add(rs.getString(3)); //position
            file.add(rs.getString(4)); //externsion
            file.add(rs.getString(5)); //size
            employeeVector.add(file);
        }
        rs.close();
        return employeeVector;
        } catch (Throwable err) {
           err.printStackTrace();
           System.out.println("Inside two");
        } finally {
        con.close();
    }
    return null;
}

// Button click, show data in jtable.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{ 
    String searchQuery = jTextField1.getText();
    //get data from database
    DBEngine dbengine = new DBEngine();
    try {
        data = dbengine.getEmployee(searchQuery);
        System.out.println("Hello "+data.elementAt(2));
        jTable1.setModel(new DefaultTableModel(data,header));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}      

现在我想在其他地方使用第3列,即文件名(Experiment 3.doc)。 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您有VectorVector

data.elementAt(2)将返回VectorString个。然后,您需要从此结果Vector中获取下一个元素,大概是data.elementAt(2).getElementAt(2)