Ok, I will just give you a quick solution:
package stack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Test {
JTable table = new JTable();
public void selectFrom(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
DefaultTableModel model=(DefaultTableModel)table.getModel();
int rc= model.getRowCount();
for(int i = 0;i<rc;i++){
model.removeRow(0);
}
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb","postgres","123");
st = conn.createStatement();
rs = st.executeQuery("select * from mytable");
while(rs.next()){
//Populate table
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void insertInto(){
Connection conn = null;
PreparedStatement pst = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb","postgres","123");
pst = conn.prepareStatement("INSERT INTO mytable(id,first_column) VALUES(?,?)");
//Insert into table using pst
pst.execute();
selectFrom();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}