简体   繁体   中英

add a column to an existing Table from SQL using DbUtils

I am developing a swing application in which I can fetch the data from the database and display it in a table by using rs2xml.jar.

Here is my code:

try{ 
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","","");
            PreparedStatement statement = con.prepareStatement("SELECT ID FROM table WHERE ID LIKE ? AND FullName LIKE ? AND Telephone LIKE ? AND Email LIKE ?" ); 
            statement.setString(1, '%'+ID1+'%');
            statement.setString(2, '%'+FullName1+'%');
            statement.setString(3, '%'+Telephone1+'%');
            statement.setString(4, '%'+Email1+'%');
            ResultSet set = statement.executeQuery();
            jTable1.setModel(DbUtils.resultSetToTableModel(set));

        }
        catch(SQLException ex){
            System.err.println("SQLException: " + ex.getMessage());
       }

Now, I need to add a column in this existing table using other Query.

I found this answer but i am getting this error: Type mismatch: cannot convert from TableModel to DefaultTableModel

Any Ideas please?

Type mismatch: cannot convert from TableModel to DefaultTableModel

You can't just assign a TableModel to a DefaultTableMOdel .

Assuming DBUtils creates a DefaultTableModel then you need to "cast" the TableModel to a DefaultTableModel .

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addColumn(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM