Добавить строку в таблицу - Java
Формулировка задачи:
у меня есть таблица, и я не могу добавить в нее строку т.к. у меня нет метода addRow. скажите что мне надо сделать?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Diplom extends javax.swing.JFrame {
private ArrayList<Object[]> ResultSets;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
private String[] colNames={"ID", "Name", "Vendore","Type"};
public Diplom() throws ClassNotFoundException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(Diplom.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Diplom.class.getName()).log(Level.SEVERE, null, ex);
}
initComponents();
} try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vanek?user=root&password=");
st = conn.createStatement();
String carid=jTextField1.getText();
String name=jTextField2.getText();
String vendor=jTextField3.getText();
String type=(String) jComboBox1.getSelectedItem();
st.executeUpdate( "insert into otchet (carid, name, vendor, type) "
+ "value("+quotate(carid)+","+quotate(name)+","+quotate(vendor)+","+quotate(type)+")");
rs = st.executeQuery("SELECT *FROM otchet ");
while (rs.next()) {
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
jTable1.setValueAt(rs, i, j);
}}}
} catch (SQLException e) {
if (conn == null) {
System.out.println("failed to connect to database");
} else
if (st == null) {
System.out.println("failed to create statement");
} else
if (rs == null) {
System.out.println("failed to fetch resultset");
} else {
System.out.println("error while fetching results");
}
} finally {
// Да, пустые блоки catch - очень плохо
if (rs != null) {
try { rs.close(); } catch (Exception e) {}
}
if (st != null) {
try { st.close(); } catch (Exception e) {}
}
if (conn != null) {
try { conn.close(); } catch (Exception e) {}
}
}Решение задачи: «Добавить строку в таблицу»
textual
Листинг программы
((DefaultTableModel) table.getTableModel()).addRow(row);