1

Classを使用ResultSetTableModelして、データベース データを に表示しますJTable

public class ResultSetTableModel extends AbstractTableModel {

private Connection connection;
private Statement statement;
private PreparedStatement prstatement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
private boolean connectedToDatabase = false;

public ResultSetTableModel(String driver, String url,
        String username, String password, String query)
        throws SQLException, ClassNotFoundException {

    Class.forName(driver);

    connection = DriverManager.getConnection(url, username, password);

    prstatement = (PreparedStatement) connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);

    connectedToDatabase = true;
    setQuery(query);
}

@Override
public Class getColumnClass(int column) throws IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }

    try {
        String className = metaData.getColumnClassName(column + 1);

        return Class.forName(className);
    } catch (Exception exception) {
        exception.printStackTrace();
    }

    return Object.class;
}

@Override
public int getColumnCount() throws IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }
    try {
        return metaData.getColumnCount();

    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    }
    return 0;
}

@Override
public String getColumnName(int column) throws IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }
    try {
        return metaData.getColumnName(column + 1);
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    }
    return "";
}

@Override
public int getRowCount() throws IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }
    return numberOfRows;
}

@Override
public Object getValueAt(int row, int column)
        throws IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }
    try {
        resultSet.absolute(row + 1);
        return resultSet.getObject(column + 1);
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    }

    return "";
}

public void setQuery(String query)
        throws SQLException, IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }
    int a = prstatement.executeUpdate(query);  
    metaData = resultSet.getMetaData();

    resultSet.last();                   // move to last row
    numberOfRows = resultSet.getRow();  // get row number      

    fireTableStructureChanged();
}

public void disconnectFromDatabase() {
    if (!connectedToDatabase) {
        return;
    }

    try {
        prstatement.close();
        connection.close();
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    } finally
    {
        connectedToDatabase = false;
    }
}

public void removeRecord(int row) throws SQLException {
    String deleteQuery = "delete from mytable where id=?";
    PreparedStatement pStatement = connection.prepareStatement(deleteQuery);
    pStatement.setInt(1, row);
    int rowsAffected = pStatement.executeUpdate();
}
}

私の2番目のクラス:

public class d7Table extends JFrame implements ActionListener {

String dbUrl = "jdbc:mysql://localhost/mydb";
String dbDriver = "com.mysql.jdbc.Driver";
public JTable table;
public JButton dellButton;
ResultSetTableModel rstm;
public int selectedRow;

public d7Table() {
    try {
        rstm = new ResultSetTableModel(dbDriver, dbUrl,
                "root", "2323", "select * from mytable");
        table = new JTable(rstm);

    } catch (SQLException ex) {
        System.out.println("Could not connect to database");
    } catch (ClassNotFoundException cnfe) {
    }

    add(new JScrollPane(table), BorderLayout.CENTER);
    add(buttonsPanel(), BorderLayout.SOUTH);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800, 600);
    this.setLocation(300, 60);
    this.setVisible(true);
}

public JPanel buttonsPanel() {
    JPanel buttonP = new JPanel();
    dellButton = new JButton("Delete");
    dellButton.addActionListener(this);
    buttonP.add(dellButton);
    return buttonP;
}

@Override
public void actionPerformed(ActionEvent e) {
     if(e.getSource()== dellButton){
        selectedRow = table.getSelectedRow();
        if(selectedRow>0){
            try{
                rstm.removeRecord(selectedRow);
            }
            catch(SQLException sqle){
                sqle.printStackTrace();
            }
        }
        else{
            System.out.println("Select a row");
        }
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new d7Table();
        }
    });
}
}

今、私のテーブルは正しく入力されていますが、行を選択してクリックして削除しても何も起こりません!

4

1 に答える 1

4

エラーは明らかです -executeQueryデータベースの書き込み操作には使用できません。

代わりにexecuteUpdateを使用してください。

SQL インジェクション攻撃から保護するのPreparedStatementではなく、使用してください。次に、すべてのロジックを と共有するのではなく、メソッドにカプセル化することで簡素化します。StatementremoveRecordsetQuery

public void removeRecord(int row) throws SQLException {
    String deleteQuery = "delete from mytable where id=?";
    PreparedStatement statement = connection.prepareStatement(deleteQuery);
    statement.setInt(1, row);
    int rowsAffected = statement.executeUpdate();
}

余談:setQueryは単純なセッターではありません。たとえば、次のように名前を変更します。updateFromDatabase

于 2013-07-24T22:50:54.810 に答える