Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Petr

Thread: Problem with jtable, mysql and a JAR file

  1. #1
    pflerman is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Problem with jtable, mysql and a JAR file

    Hello:

    I have a program that i can run ok inside de eclipse IDE.

    This program connect to an mysql database and show the result in a jtable.

    But when i export this program to a JAR file, when i execute the program, do nothing.

    Can anyone help me with this code.

    Thanks

    Java Code:
    import java.awt.Component;
    import java.sql.*;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableModel;
    
    /**
     * Clase de prueba de conexión con una base de datos MySQL
     */
    @SuppressWarnings("unused")
    public class PruebaMySQL extends AbstractTableModel{
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	Vector<String[]> cache;
        int colCount;
    	
    	public PruebaMySQL() 
        {
            
            DefaultTableModel d = new DefaultTableModel();
            String []headers;
            
            cache = new Vector<String[]>();
            
            try
            {
                DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
                
                Connection conexion = DriverManager.getConnection (
                    "jdbc:mysql://localhost/pablo","root", "bauca");
                Statement s = conexion.createStatement();
                ResultSet rs = s.executeQuery ("select * from clientes");
                
                ResultSetMetaData meta = rs.getMetaData();
                colCount = meta.getColumnCount();
                
                headers = new String[colCount];
                for (int h = 1; h <= colCount; h++) {
                  headers[h - 1] = meta.getColumnName(h);
                }
                
                while (rs.next()) {
                	String[] record = new String[colCount];
                    for (int i = 0; i < colCount; i++) {
                      record[i] = rs.getString(i + 1);
                    }
                    cache.addElement(record);
                }
                	
                fireTableChanged(null);
                
                System.out.println(colCount);
                
                // Se recorre el ResultSet, mostrando por pantalla los resultados.
                /*
                while (rs.next())
                {
                    System.out.println (rs.getInt ("Id") + " " + rs.getString (2)+ 
                        " " + rs.getString(3)+ " " +  rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6));
                }
                */
                conexion.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) 
        {
            
            JTable t = new JTable(new PruebaMySQL());
            JFrame f = new JFrame();
            JLabel l = new JLabel("a");
            JScrollPane s = new JScrollPane(t);
            f.getContentPane().add(s);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(500,500);
            f.setVisible(true);
        }
    
    	@Override
    	public int getColumnCount() {
    		// TODO Auto-generated method stub
    		return colCount;
    	}
    
    	@Override
    	public int getRowCount() {
    		// TODO Auto-generated method stub
    		return cache.size();
    	}
    
    	@Override
    	public Object getValueAt(int row, int col) {
    		// TODO Auto-generated method stub
    		return ((String[]) cache.elementAt(row))[col];
    	}
        
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: Problem with jtable, mysql and a JAR file

    )) I guess you have a ClassNotFound exception. When you run this app in eclipse it makes classpath itself, but if you want to run this app without IDE you must do manifest file where point all libraries which you use.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    pflerman is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Problem with jtable, mysql and a JAR file

    Ok.

    I am a beginer, can you tell me how to do that ?

    thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with jtable, mysql and a JAR file

    You haven't told us of any error messages that you're receiving. You also haven't shown us the results of any attempts to debug this yourself with println statements or log statements to a file.

  5. #5
    pflerman is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Problem with jtable, mysql and a JAR file

    Hello

    Y run the jar in console and i have this error,

    Exception in thread "main" java.lang.NoClassDefFoundError: org/gjt/mm/mysql
    er
    at PruebaMySQL.<init>(PruebaMySQL.java:34)
    at PruebaMySQL.main(PruebaMySQL.java:80)
    Caused by: java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

    C:\Documents and Settings\Romulo Lerman\Escritorio>java -jar pauli.jar

    C:\Documents and Settings\Romulo Lerman\Escritorio>java -jar tabla.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: org/gjt/mm/mysql
    er
    at PruebaMySQL.<init>(PruebaMySQL.java:34)
    at PruebaMySQL.main(PruebaMySQL.java:80)
    Caused by: java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

Similar Threads

  1. jtable mysql search
    By hash004 in forum New To Java
    Replies: 8
    Last Post: 09-08-2011, 06:05 PM
  2. Jtable and Mysql
    By ehsan in forum AWT / Swing
    Replies: 1
    Last Post: 08-13-2011, 10:14 PM
  3. Replies: 0
    Last Post: 04-02-2011, 10:22 AM
  4. JTable, Vector, File, Problem.
    By ocean in forum New To Java
    Replies: 4
    Last Post: 11-06-2009, 07:51 AM
  5. Replies: 0
    Last Post: 03-29-2008, 03:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •