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 2 of 2

Thread: Display database values into JTables

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Display database values into JTables

    Hey guys i would love someone to help me with my project. I am trying to display all of the data from a database to a jtable using Eclipse.
    When i run this code i dont get any errors and nothing shows on the jtable.
    I watched and followed this guy's video guide ( Java prog#5. JTable- Populate JTable data from database in java Netbeans and Sqlite (mysql) - YouTube )
    and i downloaded this file rs2xml.jar
    *** I am new to programming and still working on my skills ***
    Thanks (Heads up)

    package foodpackage;
     
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import net.proteanit.sql.DbUtils;
     
    /***
     * 
     * @author sa00366
     *
     */
     
    public class Admin {
     
    	String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim();
    	String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb";
     
    	Connection con = null;
    	Statement st = null;
    	ResultSet rs;
    	PreparedStatement pst = null;
     
    	private JFrame frmAdminis;
    	private JTable table;
    	private JButton btnButton;
     
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Admin window = new Admin();
    					window.frmAdminis.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public Admin() {
    		initialize();
    		Update_Table();
     
    	}
     
     
    	public void Update_Table() {
     
    		try {
     
    			String sql = "select * dog";
    			pst=con.prepareStatement(sql);
    			rs = st.executeQuery(sql);
    			table.setModel(DbUtils.resultSetToTableModel(rs));
     
    			Class.forName(MSAccessDriver);
    			String db = "jdbc:odbc:Animal"; // db = database string stored
    											// in the database
    			con = DriverManager.getConnection(db);
    			st = con.createStatement();
    			st.execute(sql);
    		}
     
    		catch (Exception ex) {
     
    		}
    	}
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frmAdminis = new JFrame();
    		frmAdminis.setResizable(false);
    		frmAdminis.setTitle("Title ");
    		frmAdminis.setBounds(100, 100, 1048, 547);
    		frmAdminis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frmAdminis.getContentPane().setLayout(null);
     
    		table = new JTable();
    		table.setBounds(61, 76, 794, 226);
    		frmAdminis.getContentPane().add(table);
     
    		btnButton = new JButton("button");
    		btnButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				Update_Table();
    			}
    		});
    		btnButton.setBounds(61, 391, 200, 50);
    		frmAdminis.getContentPane().add(btnButton);
    	}
    	private static class __Tmp {
    		private static void __tmp() {
    			  javax.swing.JPanel __wbp_panel = new javax.swing.JPanel();
    		}
    	}
    }


  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display database values into JTables

    Try making a JList out of the model you get from that DbUtils, then use that JList in your setModel.

    For example:

    JList myList = new JList(DbUtils.resultSetToTableModel(rs));
    table.setModel(myList);

Similar Threads

  1. buttons and buttongroups send values to database
    By Light Mike in forum JDBC & Databases
    Replies: 2
    Last Post: August 22nd, 2012, 05:49 AM
  2. how to plot a line graph by reading the values from database?
    By priti in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2012, 02:39 AM
  3. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 03:48 AM
  4. Using jbutton to write to jtextfield values to database
    By Sociopath in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 23rd, 2011, 11:53 PM
  5. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 04:15 AM

Tags for this Thread