i wanted to create a gui java program that reads info from MySql then output it in a JTable.
but i first tried to use the JTable because it was my first time to use it.
It worked perfectly but when i tried to read from the database it doesn't display the JTable although i don't read anydata from the database here is the code.
the function IDs() when called the JTable don't appear.

import java.awt.Dimension;
import java.sql.*;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public final class MyFrame extends JFrame{
     String[] columnNames;
     Object[][] data;
    public MyFrame() throws ClassNotFoundException, SQLException{
        setVisible(true);
        setSize(900, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        int i,j;
        String[] col={"ID"};
        IDs();//when i remove this line the JTable appears
        define();
       //Create and set up the content pane.
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);

    }//end of constructor
    private void define(){
        String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
        Object[][] data = {
	    {"Kathy", "Smith","Snowboarding", new Integer(5), new Boolean(false)},
	    {"John", "Doe","Rowing", new Integer(3), new Boolean(true)},
	    {"Sue", "Black","Knitting", new Integer(2), new Boolean(false)},
	    {"Jane", "White","Speed reading", new Integer(20), new Boolean(true)},
	    {"Joe", "Brown","Pool", new Integer(10), new Boolean(false)}
        };
        this.columnNames=columnNames;
        this.data=data;
    }

    private Statement connect() throws ClassNotFoundException, SQLException{
                 Class.forName("com.mysql.jdbc.Driver");
                 Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/buba","root","root");
                 return con.createStatement();
    } 
    public Vector IDs() throws ClassNotFoundException, SQLException{
        Vector vec=new Vector();
        Statement stm=connect();
        return vec;
    }

}//end of class

please help i need to read data from the database

Recommended Answers

All 8 Replies

i wanted to create a gui java program that reads info from MySql then output it in a JTable.
but i first tried to use the JTable because it was my first time to use it.
It worked perfectly but when i tried to read from the database it doesn't display the JTable although i don't read anydata from the database here is the code.
the function IDs() when called the JTable don't appear.

please help i need to read data from this connect() method

Did you got any exception from the following code ?

private Statement connect() throws ClassNotFoundException, SQLException{                 Class.forName("com.mysql.jdbc.Driver");                 Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/buba","root","root");                 return con.createStatement();    }

Did you got any exception from the following code ?

private Statement connect() throws ClassNotFoundException, SQLException{                 Class.forName("com.mysql.jdbc.Driver");                 Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/buba","root","root");                 return con.createStatement();    }

no actually i tried to read from the database and System.out.println the data and it output it perfectly i don't see any problem in the database

What exactly does your IDs() method do, e.g. why does it return an unused vector, and you don't even use the returned Vector in your constructor ?

What exactly does your IDs() method do, e.g. why does it return an unused vector, and you don't even use the returned Vector in your constructor ?

i coded it before to return data in that vector but when i saw that the JTable doesn't work with that vector i tried to just call it without using it. and i found that the JTable also don't work although it does nothing.
when i comment it.the JTable appears

i coded it before to return data in that vector but when i saw that the JTable doesn't work with that vector i tried to just call it without using it. and i found that the JTable also don't work although it does nothing.
when i comment it.the JTable appears

That will be because your IDs() method gets only connection, create statement, but DOES NOT execute any query. Its like driving your car down to petrol station, park in front of pump and waiting to get it filled on its own.

That will be because your IDs() method gets only connection, create statement, but DOES NOT execute any query. Its like driving your car down to petrol station, park in front of pump and waiting to get it filled on its own.

man please understand me i said i tried to let it do something before but it didn't work so i let it do nothing to see where is the problem and as you can see the problem is not in this method it's in the connection method

no actually i tried to read from the database and System.out.println the data and it output it perfectly i don't see any problem in the database

man please understand me i said i tried to let it do something before but it didn't work so i let it do nothing to see where is the problem and as you can see the problem is not in this method it's in the connection method

Make up your mind. Either it is working and you did not ruin query or it doesn't work and is throwing exception, in witch case you should post exception. You should use try/catch statement instead of hopping that Java will give you some meaningful messages on runtime

Make up your mind. Either it is working and you did not ruin query or it doesn't work and is throwing exception, in witch case you should post exception. You should use try/catch statement instead of hopping that Java will give you some meaningful messages on runtime

i said it's working it's working
its working perfectly THE JTABLE IS NOT WORKING the database is working no exceptions no nothing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.