954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java DB program

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

buba_kazouba
Light Poster
27 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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();    }
Muralidharan.E
Junior Poster in Training
74 posts since Apr 2008
Reputation Points: 32
Solved Threads: 6
 

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

buba_kazouba
Light Poster
27 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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 ?

Aviras
Junior Poster in Training
82 posts since Jul 2008
Reputation Points: 24
Solved Threads: 8
 
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

buba_kazouba
Light Poster
27 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
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.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
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

buba_kazouba
Light Poster
27 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
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

buba_kazouba
Light Poster
27 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: