Привет !!! У меня проблемка с JTable... или с DB... короче никак не могу понять почему он мне не разрешает сделать ResultSet rs = stm.executeUpdate(sql); в методе public void deleteData(Object row) ... stm.executeQuery можно делать, а вот Update нет!!! | Код | import java.sql.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.table.*;
public class QueryTableModel extends AbstractTableModel {
Vector cache; //will hold String[] objects int colCount; String headers[] = null; Connection db = null; Statement stm = null; String currentURL;
public QueryTableModel() { cache = new Vector(); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception exception) { exception.printStackTrace(); } }
public String getColumnName(int i) { return headers[i]; }
public int getColumnCount() { return colCount; }
public int getRowCount() { return cache.size(); }
public Object getValueAt(int row, int col) { return ((String[]) cache.elementAt(row))[col]; }
public void setHostURL(String url) { if (url.equals(currentURL)) { return; } closeDB(); initDB(url); currentURL = url; }
//All the real work happens here; in a real application, we'd probably //perform the query in a separate thread. public void setQuery(String q) { cache = new Vector(); try { ResultSet rs = stm.executeQuery(q); ResultSetMetaData meta = rs.getMetaData(); colCount = meta.getColumnCount();
//Now we must rebuild the headers array with the new column names. headers = new String[colCount]; for (int h = 1; h <= colCount; h++) { headers[h - 1] = meta.getColumnName(h); }
//Now we must file the cache with the records from our query. //This would not be practical if we were expecting a few million //records in response to our query, but we aren't, so we can do this while (rs.next()) { String[] record = new String[colCount]; for (int i = 0; i < colCount; i++) { record[i] = rs.getString(i + 1); } cache.addElement(record); } fireTableChanged(null);
} catch (Exception exception) { exception.printStackTrace(); } }
public void deleteData(Object row) { //cache = new Vector(); if (cache.isEmpty()) { return; } else { try { String sql = "DELETE FROM personen WHERE nummer = " + row; //cache.removeAllElements();
ResultSet rs = stm.executeUpdate(sql); ResultSetMetaData meta = rs.getMetaData(); colCount = meta.getColumnCount();
//Now we must rebuild the headers array with the new column names. headers = new String[colCount]; for (int h = 1; h <= colCount; h++) { headers[h - 1] = meta.getColumnName(h); }
//Now we must file the cache with the records from our query. //This would not be practical if we were expecting a few million //records in response to our query, but we aren't, so we can do this while (rs.next()) { String[] record = new String[colCount]; for (int i = 0; i < colCount; i++) { record[i] = rs.getString(i + 1); } cache.addElement(record); } fireTableChanged(null); } catch (Exception exception) { } } }
public void initDB(String url) { try { db = DriverManager.getConnection("jdbc:mysql://" + url, "root", "maximus"); stm = db.createStatement(); } catch (Exception exception) { System.out.println("Could not initialize the Database."); exception.printStackTrace(); } }
public void closeDB() { try { if (stm != null) { stm.close(); } if (db != null) { db.close(); } } catch (Exception exception) { exception.printStackTrace(); } } }
|
| Код | import javax.swing.*; import java.awt.event.*; import java.awt.*;
public class DatenbankAbfrageTool extends JFrame {
private JTextField name_in = null; private JTextField vorname_in = null; private JTextField strasse_in = null; private JTextField ort_in = null; private JTextField plz_in = null; private JTextField telefon_in = null; private JButton showData = null; private JButton executeQuery = null; private JButton deleteData = null; private JButton exitApplication = null; private JPanel eingabePanel = null; private QueryTableModel tableModel = null; private JTable resultTable = null;
public DatenbankAbfrageTool() { super("Datendankabfragetool: "); setDefaultCloseOperation(EXIT_ON_CLOSE); eingabePanel = new JPanel(new GridLayout(4, 3));
name_in = new JTextField(); vorname_in = new JTextField(); strasse_in = new JTextField(); ort_in = new JTextField(); plz_in = new JTextField(); telefon_in = new JTextField();
showData = new JButton("Show Data"); executeQuery = new JButton("Execute Query"); deleteData = new JButton("Delete Data"); exitApplication = new JButton("Exit");
eingabePanel.add(new JLabel(" Name: ")); eingabePanel.add(name_in); eingabePanel.add(new JLabel(" Vorname: ")); eingabePanel.add(vorname_in); eingabePanel.add(new JLabel(" Strasse: ")); eingabePanel.add(strasse_in); eingabePanel.add(new JLabel(" Ort: ")); eingabePanel.add(ort_in); eingabePanel.add(new JLabel(" PLZ: ")); eingabePanel.add(plz_in); eingabePanel.add(new JLabel(" Telefon: ")); eingabePanel.add(telefon_in); eingabePanel.add(showData); eingabePanel.add(executeQuery); eingabePanel.add(deleteData); eingabePanel.add(exitApplication);
tableModel = new QueryTableModel(); resultTable = new JTable(tableModel); JScrollPane scrollPane = new JScrollPane(resultTable); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, eingabePanel, scrollPane);
showData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { new MyShowData().start(); } }); deleteData.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tableModel.setHostURL("localhost/pvt"); tableModel.deleteData(resultTable.getValueAt(resultTable.getSelectedRow(), 0)); } });
add(splitPane); pack(); setVisible(true); }
class MyShowData extends Thread {
public void run() { tableModel.setHostURL("localhost/pvt"); tableModel.setQuery("SELECT * FROM personen");
} }
public static void main(String[] args) { new DatenbankAbfrageTool(); } }
|
работаю с MySQL... за совет или просто намёк буду очень признателен!!!!!! долблюсь уже неделю!!!!
|