Results 1 to 3 of 3
  1. #1
    Subhero is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Java/SQL Removing double data

    I know this might seem like a simple query problem [and it probably is but not for me :D].

    The problem is the following that using the DISTINCT method in the query does not remove the double data. I tried using DISTINCT only on gemeente, but then the data does not show up in my ComboBox.

    The point is is that I remove the double data from the gemeente column, which contains a bunch of towns and their postal codes. Then I need to list those towns in a ComboBox but applying DISTINCT to gemeente only works as a query in SQL, but then I do not see the data in my combobox.

    Java Code:
    public Vector<jstageGemeente> getjStageGemeente() {
    
            Statement stmt = null;
            ResultSet rs = null;
            jstageGemeente gemeente = null;
            Vector <jstageGemeente> resultaat = new Vector<jstageGemeente>();
    
            try {
                stmt = connection.createStatement();
                rs = stmt.executeQuery("SELECT DISTINCT id, postcode, gemeente FROM jstageGemeente ORDER BY gemeente");
    
                while (rs.next()) {
                    gemeente = new jstageGemeente();
                    gemeente.setId(rs.getInt(1));
                    gemeente.setPostcode(rs.getInt(2));
                    gemeente.setGemeente(rs.getString(3));
                    resultaat.add(gemeente);
                }
            }
    StudentFrame that loads the data into the combobox

    Java Code:
    public class StudentFrame extends JFrame implements ActionListener {
    
        private Container content;
        private JPanel topLeft, topRight, bottomLeft, bottomRight;
        private JComboBox gemeentenBox, sectorBox, soortBox;
        private JTextField aantalVeld;
        private JButton zoekenButton;
    
        private DAjstageLogin daLogin = null;
        private DAjstageBedrijf daBedrijf = null;
        private DAjstageGemeente daGemeente = null;
        private DAjstageKeuze daKeuze = null;
        private DAjstageSector daSector = null;
        private DAjstageSoort daSoort = null;
        private DAjstageStudent daStudent = null;
        private DAjstageOpdracht daOpdracht = null;
    
        public StudentFrame(DAjstageBedrijf daBedrijf, DAjstageKeuze daKeuze, DAjstageGemeente daGemeente, DAjstageSector daSector, DAjstageOpdracht daOpdracht, DAjstageSoort daSoort, DAjstageStudent daStudent, DAjstageLogin daLogin)
                        throws Exception {
    
            this.daGemeente = daGemeente;
            this.daSector = daSector;
            this.daOpdracht = daOpdracht;
            this.daStudent = daStudent;
            this.daKeuze = daKeuze;
            this.daLogin = daLogin;
            this.daSoort = daSoort;
    
            this.setLocation(150,150);
            setTitle("Welkom");
            setSize(1000,750);
            opbouwScherm();
            addWindowListener(new VensterHandler());
            setVisible(true);
            this.setResizable(false);
    
        }
    
    
        public void opbouwScherm(){
            content = getContentPane();
            content.setLayout(null);
    
            JComboBox gemeentenBox = new JComboBox(daGemeente.getjStageGemeente());
            gemeentenBox.insertItemAt("geen voorkeur", 0);
            JComboBox sectorBox = new JComboBox(daSector.getjStageSector());
            sectorBox.insertItemAt("geen voorkeur", 0);
            JComboBox soortBox = new JComboBox(daSoort.getjStageSoort());
            soortBox.insertItemAt("geen voorkeur", 0);
            JTextField aantalVeld = new JTextField();
            JButton zoekenButton = new JButton("Zoek");
            JLabel gemeentenLabel = new JLabel("Locatie bedrijf:");
            JLabel aantalLabel = new JLabel("Werkzame informatici:");
            JLabel sectorLabel = new JLabel("Sector stagebedrijf:");
            JLabel soortLabel = new JLabel("Soort stage:");
    
            //comboboxen
            gemeentenBox.setBounds(5, 30, 150, 20);
            sectorBox.setBounds(160, 30, 325, 20);
            soortBox.setBounds(495, 30, 140, 20);
            aantalVeld.setBounds(625, 30, 135, 20);
    
            //labels
            gemeentenLabel.setBounds(5,10,120,20);
            sectorLabel.setBounds(160,10,120,20);
            soortLabel.setBounds(495,10,120,20);
            aantalLabel.setBounds(625,10,135,20);
    
            //buttons
            zoekenButton.setBounds(800,30,80,20);
    
            content.add(gemeentenBox);
            content.add(sectorBox);
            content.add(soortBox);
            content.add(aantalVeld);
            content.add(gemeentenLabel);
            content.add(sectorLabel);
            content.add(soortLabel);
            content.add(aantalLabel);
            content.add(zoekenButton);
    
        }

  2. #2
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Well this thread doesn't really belong to this location.
    It must be placed in the database section.

    Anyways,
    I think I have solved your problem.
    You can't have two columns to "DISTINCT". You must only select one column.
    Or you may add a "GROUP BY" phrase to which column you must have the "DISTINCT" phrase working.

    The GUI code on the other hand, is perfectly coded.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,791
    Rep Power
    19

    Default

    Quote Originally Posted by chyrl View Post
    Well this thread doesn't really belong to this location.
    It must be placed in the database section.

    Anyways,
    I think I have solved your problem.
    You can't have two columns to "DISTINCT". You must only select one column.
    Yes you can.
    I've just done it in MySQL.

    OP: You're not eating an exception somewhere are you? Though I can't think why that would give you an exception if the one without the DISTINCT doesn't.

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  2. double or float for importing data?
    By tyang in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 09:30 AM
  3. double and float value in java
    By arvindmer in forum New To Java
    Replies: 6
    Last Post: 05-28-2009, 04:51 PM
  4. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  5. Replies: 0
    Last Post: 10-31-2008, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •