Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JTable and database

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JTable and database

    hi guys

    I have a problem whith my code. I am trying to make a JTable with some information of a database. but I have some problem with the Object[][] use for proceed to JTable creation.
    i put my code :
    excuze my english. I'm not so good
     
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.Statement;
     
    import javax.swing.JTable;
     
     
     
    public class Connect {
    	public static void main(String[] args) {
     
    		try {
                             //connection to my database
    			Class.forName("org.postgresql.Driver");
    			System.out.println("Driver O.K.");
    			String url = "jdbc:postgresql://localhost:5432/Ecole";
    			String user = "postgres";
    			String passwd = "*******";
    			Connection conn = DriverManager.getConnection(url, user, passwd);
    			System.out.println("Connexion effective !");
     
     
    			Statement state = conn.createStatement();
    			ResultSet result = state.executeQuery("SELECT * FROM classe");
    			//we take MetaData
    			ResultSetMetaData resultMeta = result.getMetaData();
     
                             //the variable of my JTable
                            String title[] = {"classe", "prof principale"};
    			Object[][] data = null;
    			int i = 0;
    			while(result.next()){
     
                                      // my table Class has 2 column so I write "j<2"
    				for(int j=0;j<2;j++){
     
                                     // to put a line on my data 
    				data[i][j]=result.getString("cls_nom");	
    				j++;
    				data[i][j]=result.getString("cls_id");	
    				}
                                    // to change a line
    				i++;
     
    			}
     
     
    			result.close();
    			state.close();
     
                            // I try here to create a JTable but it doesn't work 
                              // the compiler write (java.lang.NullPointerException)
    			JTable tableau = new JTable( data,title);
    		} catch (Exception e) {
     
    			e.printStackTrace();
    		 }
     
    	}
    }


    error:java.lang.NullPointerException
    at Connect.main(Connect.java:41)


    thank you for your help


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,407
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: JTable and database

    Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    If you're getting errors, post them too. We don't know what "have some problem with the Object[][] use" means, not because of any issues with your English. We just can't be sure what your problem is unless you're more specific or post the related errors.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable and database

    thanks for your remark; i'am seeking in other topic to have some answers

    --- Update ---

    I find my solution : [HELP] Refreshing JTable connected to database

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,407
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: JTable and database

    Thanks for posting the update.

Similar Threads

  1. [HELP] Refreshing JTable connected to database
    By mr.nacho in forum What's Wrong With My Code?
    Replies: 19
    Last Post: December 31st, 2012, 12:32 PM
  2. Replies: 21
    Last Post: November 27th, 2012, 09:58 PM
  3. refreshing a jtable with database
    By kollyisrealisaac in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2011, 01:50 PM