Results 1 to 3 of 3
  1. #1
    Apprentice123 is offline Member
    Join Date
    Jul 2012
    Posts
    20
    Rep Power
    0

    Default Grid with JFrame and MySQL

    I tried to make a datagrid with mysql.
    Why appears twice the data of the mysql database?

    Java Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package testegrid;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.util.Vector;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    /**
    *
    * @author
    */
    public class DataInJTable extends javax.swing.JFrame {
            //This Vector Of A String Vector will be used to hold data from
    // database table to display in JTable.
    static Vector<Vector<String>> data=new Vector<Vector<String>>();
    
    static JTable table;
    
    public DataInJTable()
    {
      super("JTabe with MySql Database");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      JPanel topPanel=new JPanel();
    
      JLabel label1=new JLabel("MySQL Database Name  :   test");
      label1.setPreferredSize(new Dimension(200,30));
     
      JLabel label2=new JLabel("MySQL Table Name     :  One");
      label2.setPreferredSize(new Dimension(200,30));
     
      topPanel.add(label1);
      topPanel.add(label2);
     
      getContentPane().add(topPanel,BorderLayout.NORTH);
     
     
      Vector<String> headers=new Vector<String>();
       headers.add("chave");
       headers.add("Nome");
      getData();
     
      //this is the model which contain actual body of JTable
      DefaultTableModel model = new DefaultTableModel(data, headers);
      table=new JTable(model);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     
     
      header_size();
     
      JScrollPane scroll = new JScrollPane(table);
     
      scroll.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      scroll.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
     
      getContentPane().add(scroll,BorderLayout.SOUTH);
      pack();
      setResizable(false);
      setVisible(true);
    }
    
    
    /**
      * Setting the particular Column Size in JTable
      */
    public static void header_size() {
                    TableColumn column = table.getColumnModel().getColumn(0);
                    column.setPreferredWidth(100);
    
                    column = table.getColumnModel().getColumn(1);
                    column.setPreferredWidth(350);
    
             }
    
    
    /**
      * Fetching Data From MySql Database
      * and storing in a Vector of a Vector
      * to Display in JTable
      */
    private static void getData()
    {
      // Enter Your MySQL Database Table name in below Select Query.
      String str="select * from one";
      Connection cn;
      ResultSet rs;
      Statement st;
     
      try {
       // Change the database name, hosty name,
       // port and password as per MySQL installed in your PC.
       cn=DriverManager.getConnection("jdbc:mysql://" +
             "localhost:3306/test","root","12345");
       st=cn.createStatement();
      
       rs=st.executeQuery(str);
      
       while(rs.next())
       {
            Vector <String> d=new Vector<String>();
       
            d.add(rs.getString("id"));
            d.add(rs.getString("name"));
       
            d.add("\n\n\n\n\n\n\n");
            data.add(d);
       }
      
      } catch (SQLException e) {
      
       e.printStackTrace();
      }
    }
    
    public static void main(String[] args) {
     
      new DataInJTable();
    }
            /**
             * This method is called from within the constructor to initialize the form.
             * WARNING: Do NOT modify this code. The content of this method is always
             * regenerated by the Form Editor.
             */
            @SuppressWarnings("unchecked")
            // <editor-fold defaultstate="collapsed" desc="Generated Code">
            private void initComponents() {
                    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                    getContentPane().setLayout(layout);
                    layout.setHorizontalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGap(0, 400, Short.MAX_VALUE)
                    );
                    layout.setVerticalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGap(0, 300, Short.MAX_VALUE)
                    );
                    pack();
            }// </editor-fold>
       
            // Variables declaration - do not modify
            // End of variables declaration
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Grid with JFrame and MySQL

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Grid with JFrame and MySQL

    Stick some debugging in there, specifically the getData() method.
    You're mixing static stuff with non-static a lot there for no good reason, so I'm wondering if it's being called twice.
    Find out what the value of data at the end of that method for example.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Open Grid Scheduler/Grid Engine
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-27-2012, 08:00 PM
  2. Replies: 4
    Last Post: 03-05-2012, 12:26 AM
  3. drawing a centered grid with JFrame
    By adonaldson in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2012, 10:01 PM
  4. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 08:56 PM
  5. How do you draw a grid in a JFrame
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 03:31 AM

Posting Permissions

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