在组合框中选择时在jtable中获取值

时间:2013-12-26 05:34:07

标签: java swing jtable

1.i有一个从数据库中获取值的jcombobox, 2.在选择值时,我想在jtable中显示该特定行,

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    Object obj = jComboBox1.getSelectedItem();
    String tmpr = obj.toString();
    Rtable rObj = new Rtable();
    rObj.setUserName(tmpr);
}

private void Update_table(){     
    String Sql = "SELECT * FROM r_db1.dbo.user_names " + jComboBox1.getSelectedItem() 

我收到类似"UnsupportedOperationException("Not supported yet.")"

的错误

1 个答案:

答案 0 :(得分:0)

Try this
use loadcombo() method to load ur jcombobox.

void loadcombo()
    {
        try
        {
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/db.accdb";
                                //userID = "Admin";
                                password = "***";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    //sql = "SELECT * FROM tblUserProfile";
                    con=DriverManager.getConnection(url);//,"system","manager"
                    //con=DriverManager.getConnection("jdbc:odbc:shop","system","manager"); 
            st=con.createStatement();

            rs= st.executeQuery("select distinct(Name) from Table");
                while(rs.next())
            {
                jComboBox.addItem(rs.getString(1));
                    }
                st.close();
                        con.close();
        }
        catch(Exception e)
        {
            System.out.println("GG"+e);
        }

    }


use jcombobx actionlistener to load data into jtable.


jComboBox.addActionListener(new ActionListener(){


        public void actionPerformed(ActionEvent ae){
            try
        {
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/db.accdb";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";";
                    //sql = "SELECT * FROM tblUserProfile";
                    Object name=jComboBox.getSelectedItem();
                    con=DriverManager.getConnection(url);//,"system","manager"
                    //con=DriverManager.getConnection("jdbc:odbc:shop","system","manager"); 
            st=con.createStatement();


                                model.setRowCount(0);

        data = getvalue(name);

        JTable table1=new JTable(data,header);
        for(int i=0;i<table1.getRowCount();i++){
        Object[] d={data.get(i).get(0),data.get(i).get(1),data.get(i).get(2)};model.addRow(d);


                        }
                    con.close();
        }
        catch(Exception e)
        {
            System.out.println("GG"+e);
        }

        }
public Vector getvalue(Object name)throws Exception
{
Vector<Vector<String>> vector = new Vector<Vector<String>>();

Connection conn = dbConnection();
PreparedStatement pre = conn.prepareStatement("select * from Table where Name='"+name+"'");
ResultSet rs = pre.executeQuery();

while(rs.next())
{
Vector<String> c = new Vector<String>();
c.add(rs.getString(4)); 
c.add(rs.getString(5)); 
c.add(rs.getString(6)); 
vector.add(c);

}
/*Close the connection after use (MUST)*/
if(conn!=null)
conn.close();

return vector;
}


        });