简体   繁体   中英

Error when trying to populate JTable from mysql using arraylist

I have tried to populate data from mysql to JTable

Although i got the size of rows printed. i couldnt figure out what's wrong on the for loop

used the following code and now i got error as mentioned in the end. however, the query works on DBMS.

One to select data from database

public class Search{
 public ArrayList<String []> select_by_book(){
        ArrayList<String []> data = new ArrayList<>();

    try {

        String query="select * "
                + "from detail d "
                + "join user_editor_author uea on d.detail_id = uea.detail_id "
                + "join author a on a.author_id = uea.author_id "
                + "join editor e on e.editor_id = uea.editor_id "
                + "join user u on u.user_id = uea.user_id"
                + "where d.reference_type = 'book'"
                + "and u.user_id="+LogInAndRegister.id;

        ResultSet rs = connect.get(query);
        while(rs.next()){
            String tmp[] ={rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5)
                    ,rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(9),
                    rs.getString(10),rs.getString(18),rs.getString(20)};

            data.add(tmp);
        }
        rs.close();
        return data;
    } catch (SQLException ex) {
        Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}    
 }

Populate table method on the class with table:

 public class SearchGui{

DefaultTableModel dtm ;
ArrayList<String[]> database;
Search thisSearch=new Search();

 private void populateTable(){ 
        dtm=(DefaultTableModel)jTable.getModel();
        database = thisSearch.select_by_book();
       // System.out.println(thisSearch.all_reference().size());

        for(Object data[]:database){
            dtm.addRow(data);

        }
    }}

The error that i got:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'd.reference_type = 'book'and u.user_id=1' at line 1

mysql query

CREATE TABLE IF NOT EXISTS `author` (
  `author_id` int(11) NOT NULL AUTO_INCREMENT,
  `author_name` varchar(100) NOT NULL,
  PRIMARY KEY (`author_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

CREATE TABLE IF NOT EXISTS `detail` (
  `detail_id` int(11) NOT NULL AUTO_INCREMENT,
  `title_name` varchar(200) DEFAULT NULL,
  `issn` varchar(150) DEFAULT NULL,
  `volume` varchar(100) DEFAULT NULL,
  `issue` varchar(100) DEFAULT NULL,
  `start_page` int(11) DEFAULT NULL,
  `end_page` int(11) DEFAULT NULL,
  `year` varchar(4) DEFAULT NULL,
  `location` varchar(200) DEFAULT NULL,
  `publisher` varchar(200) NOT NULL,
  `reference_type` varchar(200) NOT NULL,
  `user_id` int(10) NOT NULL,
  PRIMARY KEY (`detail_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

CREATE TABLE IF NOT EXISTS `editor` (
  `editor_id` int(11) NOT NULL AUTO_INCREMENT,
  `editor_name` varchar(100) NOT NULL,
  PRIMARY KEY (`editor_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) NOT NULL,
  `user_email` varchar(100) NOT NULL,
  `user_username` varchar(100) NOT NULL,
  `user_password` varchar(100) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

the first row of the table has checkbox but not others. So, i havent populated the first column.

What do you mean you haven't populated the first column? You are using the addRow(...) method to add data to the TableModel, so the first column will always have data from the first entry in the array.

i couldnt figure out what's wrong on the for loop

dtm.addRow(new Object[]{i+1,data[1]});

The first Object in your Array is an Integer, NOT a Boolean.

i have tried to populate data from mysql to jtable. the first row of the table has checkbox but not others.

That is not the way tables work. Data needs to be consistent in the columns, otherwise you need to customize the JTable behaviour.

Unless you post demo code (with hard coded data because we don't have access to your database) that attempts to show what you are doing we can't provide any real suggestions.

Edit:

Here is an example of a SSCCE that works:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        DefaultTableModel model = new DefaultTableModel(0, 3);
        model.addRow( new Object[] { "1", "2", "3" } );
        model.addRow( new Object[] { "one", "two", "three" } );

        JTable table = new JTable(model);
        add( new JScrollPane(table) );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );

    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

Post your SSCCE that doesn't work so we can see what the data is that you are trying to load into the TableModel. This is how you simplify the problem so we can understand what you are doing. Again your database is irrelevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM