i already searched the whole web but I can't find a solution, so i decided to ask here.
Following problem: I am using those 2 methods to populate a JTable with a DefaultTableModel, the ResultSet shows the right amount of data entries (i already searched it with System.out.println) but the JTable always misses the first row
I am using a method to get a ResultSet like this:
public static JTable DBCFillBTableAuftraege() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
JTable table = null;
try {
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement( );
rs = stmt.executeQuery("SELECT a.auftrags_nr, k.firma, a.auftragsdatum, a.lieferdatum, a.rechnungsbetrag, k.name, k.strasse, k.plz, k.ort from auftrag a join kunde k on k.kunden_nr = a.kunden_nr ");
while (rs.next()) {
table = new JTable(tableModel(rs));
return table;
}
} catch (SQLException e) {
System.out.println(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return table;
}
Which then calls this method:
public static DefaultTableModel TableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
System.out.println(rs);
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
In my main:
TableAuftrag = DBCFillBTableAuftraege();
scpaneBestell.setViewportView(TableAuftrag);
It works fine, it is showing me my column headers and the data, except one problem:
It is always missing the first row and starts with the second one
Your problem starts here...
// Move to first row...
while (rs.next()) {
table = new JTable(tableModel(rs));
return table;
}
and manifests
// Move to second row and beyond...
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
Basically, in the first while (rs.next()), you move to the first row, but in the second while (rs.next()) you move to the second row, without handling the first...
It might be better to simply pass the ResultSet directly to the TableModel.
Convention would also suggest that you probably should not need to create a new JTable, but instead, just create a new TableModel and apply to a pre-existing table
while (rs.next()) {
table = new JTable(tableModel(rs));
return table;
}
You're consuming the first row of the result set here, and then call tableModel(), which consumes the other rows. The while loop is useless. Just do
table = new JTable(tableModel(rs));
return table;
Related
I'm trying to import all of the data from mysql database into a jtable using arraylists but something isn't working right, as i get the number of rows right but they're all values of the last row
Here's the code
public ArrayList<medicaments> medicaments_list() {
ArrayList<medicaments> medicament_lists = new ArrayList<medicaments>();
String select_nom_type_med = "select * from medicaments where login=?";
PreparedStatement stmt;
ResultSet rs2;
medicaments med;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
stmt = con.prepareStatement(select_nom_type_med);
stmt.setString(1, Utilisateur.getLogin());
rs2 = stmt.executeQuery();
while (rs2.next()) {
emptytable = false;
med = new medicaments(rs2.getInt("med_id"), rs2.getString("login"), rs2.getString("med_nom"), rs2.getString("med_type"), rs2.getString("date_debut"), rs2.getString("date_fin"), rs2.getString("frequence"), rs2.getString("temps_1"), rs2.getString("temps_2"), rs2.getString("temps_3"), rs2.getString("temps_4"), rs2.getString("temps_5"), rs2.getString("Stock"), rs2.getString("rappel_stock"));
medicament_lists.add(med);
}
} catch (ClassNotFoundException e1) {
System.out.println(e1);
} catch (SQLException e1) {
System.out.println(e1);
}
return medicament_lists;
}
public void populate_jTable_from_db() {
ArrayList<medicaments> dataarray = medicaments_list();
model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(dataarray.size());
int row = 0;
for (medicaments data : dataarray) {
model.setValueAt(data.get_nom_med(), row, 0);
model.setValueAt(data.get_type_med(), row, 1);
row++;
}
jTable1.setModel(model);
}
and here's the result :(there's 3 rows in my database and po is the last one i added)
Make sure your recordset actually contains something and it's what you really want:
while (rs2.next()) {
if (rs2.getString("login").equals(Utilisateur.getLogin())) {
emptytable = false;
med = new medicaments(rs2.getInt("med_id"), rs2.getString("login"),
rs2.getString("med_nom"), rs2.getString("med_type"),
rs2.getString("date_debut"), rs2.getString("date_fin"),
rs2.getString("frequence"), rs2.getString("temps_1"),
rs2.getString("temps_2"), rs2.getString("temps_3"),
rs2.getString("temps_4"), rs2.getString("temps_5"),
rs2.getString("Stock"), rs2.getString("rappel_stock"));
medicament_lists.add(med);
}
}
I seem to be have a problem when trying to get the coulmn count from a resultset's metadata. THe error is Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state.
THe idea is to use this table model to populate a JTable with the result from the database query. However when I do I get the above error.
The relevant code is:
public DefaultTableModel buildFlightModel()
throws SQLException {
query="SELECT Airline.AirlineName, Flight.FlightID, Flight.Location, Flight.Destination, Flight.ArriveTime, Flight.LeaveTime FROM Flight INNER JOIN Airline ON Airline.AirlineID=Flight.AirlineID;";
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:coursework.db");
stmt = c.createStatement();
rs=stmt.executeQuery(query);
while(rs.next()){
//System.out.println(s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
Any thoughts/ideas/help would be deeply appreciated. Thank you.
This exception is thrown when ResultSet.isclose() == true. Try to create a Clone of the MetaData before starting the Read-Loop.
I have a problem in updating my single jtable from Microsoft Access Database.
I do not have any error messages. I tried out the Query for the database using like, but my jtable does not populate in relation to the data stored in the database.
public TableModel setAuthorSearchEjournalTableValues(){
String eJournalAuthor = jTextField30.getText();
ResultSet tableData = DCE.searchEjournalbyAuthor(eJournalAuthor);
try {
ResultSetMetaData md = tableData.getMetaData();
int columnCount = md.getColumnCount();
Vector columns = new Vector(columnCount);
for (int i = 1; i <= columnCount; i++) {
columns.add(md.getColumnName(i));
}
Vector data = new Vector();
Vector row;
while (tableData.next()) {
row = new Vector(columnCount);
for (int i = 1; i <= columnCount; i++) {
row.addElement( tableData.getString(i) );
}
data.add(row);
}
DefaultTableModel tableModel = new DefaultTableModel(data, columns);
return tableModel;
//Debugging
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}`
And my result retrieving query is here
public ResultSet searchEjournalbyAuthor(String author){
conn = Connect();
try{
String myQuery = "select AuthorDetails,Year,Titleofarticle,Journaltitle,volume,pagenumbers,URL,AccessedDate from Ejournalcitation where AuthorDetails = '"+author+"'";
pst = conn.prepareStatement(myQuery);
rs = pst.executeQuery();
return rs;
}catch(SQLException ex){
ex.printStackTrace();
}
return null;
}
with the code to display as
private void jTextField30KeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(jComboBox1.getSelectedIndex()==0&&jComboBox2.getSelectedIndex()==0){//BookTitle
jTable1.setModel(setTitleSearchBookTableValues());
}
else if(jComboBox1.getSelectedIndex()==0&&jComboBox2.getSelectedIndex()==1){//BookAuthor
jTable1.setModel(setAuthorSearchBookTableValues());
}
else if(jComboBox1.getSelectedIndex()==1&&jComboBox2.getSelectedIndex()==0){//WebSiteTitle
jTable1.setModel(setTitleSearchWebSiteTableValues());
}
else if(jComboBox1.getSelectedIndex()==1&&jComboBox2.getSelectedIndex()==1){//WebSiteAuthor
jTable1.setModel(setAuthorSearchWebSiteTableValues());
}
else if(jComboBox1.getSelectedIndex()==2&&jComboBox2.getSelectedIndex()==0){//Ejournaltitle
jTable1.setModel(setTitleSearchEjournalTableValues());
}
else if(jComboBox1.getSelectedIndex()==3&&jComboBox2.getSelectedIndex()==1){//EjournalAuthor
jTable1.setModel(setAuthorSearchEjournalTableValues());
}
else{
JOptionPane.showMessageDialog(null, "Please enter a value and select the type of search");
}
}
Help me out!! Thanks in advance
you must include
DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
tableModel.fireTableDataChanged();
with the corrected query
String myQuery = "select AuthorDetails,Year,Titleofarticle,Journaltitle,volume,pagenumbers,URL,AccessedDate from Ejournalcitation where AuthorDetails like '%"+author+"%'";
I want to display the data out of my result set into a JTable.
When I run the following code the table doesn't update.
public void getHouses(int price) {
Connection conn;
ArrayList<Integer> ID = new ArrayList<Integer>();
ArrayList<String> Price = new ArrayList<String>();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Houses");
Statement statement = conn.createStatement();
ResultSet rec = statement.executeQuery("SELECT * FROM Houses WHERE Price <= " + price + "");
while (rec.next()) {
ID.add(rec.getInt("ID"));
Price.add(rec.getString("Price"));
}
String[] columnNames = {"House ID", "House Price"};
Object[][] rows = new Object[ID.size()][2];
for (int i = 0; i < ID.size(); i++) {
rows[i][0] = ID.get(i);
rows[i][1] = Price.get(i);
}
jTable1 = new JTable(rows, columnNames);
statement.close();
} catch (SQLException se) {
} catch (ClassNotFoundException cnf) {}
}
NOTE!
I added the JTable to the gui by drag and drop.
I also tested that my resultset has the data in it.
You need to learn about OP Swing MVC pattern, you need to declare a TableModel which your data store then set it to your table, like:
TableModel myData = new DefaultTableModel(columnVector, dataVector);
jTable1.setModel(myData);
Read more about DefaultTableModel
I would like to create DefaultTableModel from ResultSet. To do that, I need Object[][].
For that, I have to specify the size of the object before I iterate through the table: I go to the rs.last(), then rs.getRow(), then rs.beforeFirst();
After that, the rs.next() does not executes in the while cycle.
What am I doing wrong?
public static DefaultTableModel buildTableModel(ResultSet _resultSet) {
ResultSetMetaData metaData;
Object[] columnNames = null;
Object[][] tableData = null;
int columnCount;
int currentRowNumber = 0;
try {
metaData = _resultSet.getMetaData();
columnCount = metaData.getColumnCount();
columnNames = new Object[columnCount];
_resultSet.last();
tableData = new Object[_resultSet.getRow()][columnCount];
_resultSet.beforeFirst();
for (int currentColumn = 0; currentColumn <= columnCount; currentColumn++) {
columnNames[currentColumn] = metaData.getColumnName(currentColumn + 1);
}
while (_resultSet.next()) {
for (int columnIndex = 0; columnIndex <= columnCount; columnIndex++) {
tableData[currentRowNumber][columnIndex] = _resultSet.getObject(columnIndex + 1);
}
currentRowNumber++;
}
} catch (SQLException ex) {
System.out.println("bad");
}
return new DefaultTableModel(tableData, columnNames);
}
Probably your ResultSet is not scroll insensitive, that is, it can only be traversed forward.
See the documentation here:
[...] A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. [...]
To create a bi-directional one, do something like:
Connection conn = DriverManager.getConnection(...);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ...);
ResultSet rset = stmt.executeQuery(sql);
At first you need to print column name from ResultSetMetaData. Than you apply _resultSet.last(); and _resultSet.beforeFirst();. This way it has been working my machine.
public static DefaultTableModel buildTableModel(ResultSet _resultSet) {
ResultSetMetaData metaData;
Object[] columnNames = null;
Object[][] tableData = null;
int columnCount;
int currentRowNumber = 0;
try {
metaData = _resultSet.getMetaData();
columnCount = metaData.getColumnCount();
columnNames = new Object[columnCount];
// Print column here.
for (int currentColumn = 0; currentColumn <= columnCount; currentColumn++) {
columnNames[currentColumn] = metaData.getColumnName(currentColumn + 1);
}
tableData = new Object[_resultSet.getRow()][columnCount];
//Here point resultSet cursor to last and beforeFirst.
_resultSet.last();
_resultSet.beforeFirst();
// After swaping the above part. Now it will enter on while loop.
while (_resultSet.next()) {
for (int columnIndex = 0; columnIndex <= columnCount; columnIndex++) {
tableData[currentRowNumber][columnIndex] = _resultSet.getObject(columnIndex + 1);
}
currentRowNumber++;
}
} catch (SQLException ex) {
System.out.println("bad");
}
return new DefaultTableModel(tableData, columnNames);
}