Jump to content




Recent Status Updates

  • Photo
      15 Sep
    Error

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Grid With Mysql

setpreferredsize mysql grid

  • Please log in to reply
2 replies to this topic

#1 Apprentice123

Apprentice123

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 449 posts

Posted 30 July 2012 - 12:50 PM

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

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
}

  • 0

#2 Andre

Andre

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Location:Dakar, Sénégal
  • Programming Language:C, Java, C++, PHP, JavaScript, PL/SQL, Bash, Others
  • Learning:C#, Transact-SQL

Posted 19 August 2012 - 10:47 AM

I think the problem comes from the getData() function.

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);
   }

First, you're redeclaring a Vector everytime, which is not correct. Every Vector created is then eligible for the garbage collector, but you could create just one to optimize your code and save memory. You should do this instead.

Vector <String> d=new Vector<String>();
while(rs.next())
   {
	    d.add(rs.getString("id"));
	    d.add(rs.getString("name"));
  
	    d.add("\n\n\n\n\n\n\n");
	    data.add(d);
	    d.clear();
   }

Now, to solve your issue, you should first make sure your SQL request returns the result you want by executing it out of the Java program (PhpMyAdmin for example). If the result is correct, please show us the output of your Java program. Does it look like:

line1
line1
line2
line2

or

line1
line2
line1
line2?
  • 0

#3 mindhackerzz

mindhackerzz

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, PL/SQL
  • Learning:Visual Basic .NET

Posted 21 September 2012 - 12:23 AM

Try using List instead of vector. that is more specific datatype then vector.

Try out this one may solved you problem.

List<List<String>> data = new ArrayList<list<String>>();

List
<String> d;
while(rs.next())
{
d = =new ArrayList<String>();
d.add(rs.getString("id"));
d.add(rs.getString("name"));

d
.add("\n\n\n\n\n\n\n");
data.add(d);
}
  • 0





Also tagged with one or more of these keywords: setpreferredsize, mysql, grid