Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java GUI and Database problem

    Hello,I was trying to create a simple Library I.S. application but am experience a problem during runtime

    //LibraryIS.java
    [CODE]


    package library.is;

    import javax.swing.JTabbedPane;
    import javax.swing.SwingConstants;
    import javax.swing.*;
    import java.awt.*;

    /**
    *
    * @author codename47
    */
    public class LibraryIS {


    //Window
    JFrame mainFrame;
    JTabbedPane menuTab;
    Members members;
    JPanel membersPanel;
    public LibraryIS(){
    mainFrame=new JFrame("Library IS");
    mainFrame.setLayout(new BorderLayout());
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setBounds(200, 200, 800, 600);
    menuTab=new JTabbedPane();


    members=new Members();
    membersPanel=new JPanel();

    membersPanel.add(members.CreateRegisterUI(),BorderLayout.NORTH);
    membersPanel.add(members.getRegisteredMembers(),BorderLayout.SOUTH);

    menuTab.addTab("Members",null,membersPanel,"Member Management");

    mainFrame.add(menuTab);

    }

    public static void main(String[] args) {
    // TODO code application logic here
    LibraryIS libraryIS = new LibraryIS();
    }
    }

    [/ICODE]
    when to tried to comment out this line
    Code:
     membersPanel.add(members.getRegisteredMembers(),BorderLayout.SOUTH);
    everthing seems to work fine I went back to the function code and removed the try-catch block it works fine
    //Members.java
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package library.is;
    
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.swing.*;
    
    
    /**
     *
     * @author codename47
     */
    public class Members {
        
        private int mID;
        private String mName;
        private String mAddress;
        private String mDate_issue;
        private String mType;
        private String mStatus;
        private int mAmount;
        
        private Connection conn;
        
        public Members(){
            
        }
        public int mRegister(){
            
               
               Statement statement=null;
               try{
                   if(mID<0){
                       JOptionPane.showMessageDialog(null,"Member ID empty");
                       return -1;
                   }
                   if(mName.isEmpty()){
                       JOptionPane.showMessageDialog(null,"Member Name empty");
                       return -1;
                   }
                   if(mAddress.isEmpty()){
                       JOptionPane.showMessageDialog(null,"Member Address empty");
                       return -1;
                   }
                   if(mAmount<0){
                       JOptionPane.showMessageDialog(null,"Amount empty");
                       return -1;
                   }
                   if(mStatus.isEmpty()){
                       JOptionPane.showMessageDialog(null,"Member Status empty");
                       return -1;
                   }
                   if(mType.isEmpty()){
                       JOptionPane.showMessageDialog(null,"Member Type empty");
                       return -1;
                   }
                   
                   if(mDate_issue.isEmpty()){
                       JOptionPane.showMessageDialog(null,"Date Issue empty");
                       return -1;
                   }
                   
                   if(mID >0 && mAmount>0){
                        Connection conn=null;
                        try {
                                Class.forName("com.mysql.jdbc.Driver");
                                conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","user","");
    
                                if(conn!=null){
    
                                JOptionPane.showMessageDialog(null,"Connected to database");
    
    
                                }else
                                {
                                    JOptionPane.showMessageDialog(null,"Could not connect to Database");
                                }
                        } 
                        catch (Exception ex) {
                                JOptionPane.showMessageDialog(null,ex.toString());
                            }
                                    if(conn==null){
                                    JOptionPane.showMessageDialog(null,"Could not Connect to DB server");
                       
                      
                       
                     }
                   else{
                        //create Statement for querying database
                       statement=conn.createStatement();
                       if(statement.execute("INSERT INTO membership(IdNo,Name,Address,Date_of_Issue,Type_of_Ms,Status_of_Ms,Amount)"+
                               " VALUES("+mID+", '"+mName+"', '"+mAddress+"','"+mDate_issue+"','"+mType+"','"+mStatus+"',"+mAmount+")")){
                            JOptionPane.showMessageDialog(null,"Error while   registering "+mName);
                       }
                       else{
                            JOptionPane.showMessageDialog(null,mName+"  register successfully");
                            return 1;
                            
                       }
                   } 
                 }
                  
               }
               catch(Exception e){
                   JOptionPane.showMessageDialog(null,e.toString());
               }
            return 0;
            
            
        }
        
        
        
      public Component CreateRegisterUI (){
          //  JPanel membersPanel=new JPanel();
            JPanel regPanel=new JPanel();
            regPanel.setSize(100,100);
            JPanel curPanel=new JPanel();
            regPanel.setLayout(new GridLayout(10,2));
            JLabel regLabel=new JLabel("Member Registration",SwingConstants.CENTER);
            regPanel.add(regLabel);
            JLabel span=new JLabel("   ");
            regPanel.add(span);
            JLabel lblID=new  JLabel("IDNO:");
            regPanel.add(lblID);
            final JTextField txtID=new JTextField(10);
            regPanel.add(txtID);
            JLabel lblName=new JLabel("Name:");
            regPanel.add(lblName);
            final JTextField txtName=new JTextField(10);
            regPanel.add(txtName);
            JLabel lblAddress=new JLabel("Address:");
            regPanel.add(lblAddress);
            final JTextField txtAddress=new JTextField(10);
            regPanel.add(txtAddress);
            JLabel lblDate=new JLabel("Date Issue:");
            regPanel.add(lblDate);
            final JTextField txtDate=new JTextField(10);
            regPanel.add(txtDate);
            JLabel lblType=new JLabel("Type of Ms:");
            regPanel.add(lblType);
            final JTextField txtType=new JTextField(10);
            regPanel.add(txtType);
            JLabel lblStatus=new JLabel("Status of Ms:");
            regPanel.add(lblStatus);
            final JTextField txtStatus=new JTextField(10);
            regPanel.add(txtStatus);
            JLabel lblAmount=new JLabel("Amount:");
            regPanel.add(lblAmount);
            final JTextField txtAmount=new JTextField(10);
            regPanel.add(txtAmount);   
            JLabel span2=new JLabel("  ");
            JButton membersAdd=new JButton("ADD");
            membersAdd.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    
                  try{
                      if(txtID.getText().isEmpty())
                      mID=-1;
                      else
                          mID=Integer.parseInt(txtID.getText());
                      if(txtAmount.getText().isEmpty())
                        mAmount=-1;
                      else
                        mAmount=Integer.parseInt(txtAmount.getText());
                  
                  }catch(Exception ex){
                      JOptionPane.showMessageDialog(null,ex.toString());
                      mID=-1;
                      mAmount=-1;
                  }
                  
                  
                  mName=txtName.getText();
                  txtName.setText("");
                  mAddress=txtAddress.getText();
                  txtAddress.setText("");
                  mType=txtType.getText();
                  txtType.setText("");
                  mStatus=txtStatus.getText();
                  txtStatus.setText("");
                  mDate_issue=txtDate.getText();
                  txtDate.setText("");
                  txtID.setText("");
                  txtAmount.setText("");
                  mRegister();
                  
                }
            
                
            });
            regPanel.add(membersAdd);
            regPanel.add(span2);
           // membersPanel.add(regPanel,BorderLayout.NORTH);
            
            
            return regPanel;
      }
     
      public Component getRegisteredMembers(){
          
         
          JPanel scrollpane = new JPanel();
           
         try{
                    Class.forName("com.mysql.jdbc.Driver");
                        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","user","");
    
                                if(conn!=null){
    
                                JOptionPane.showMessageDialog(null,"Connected to database");
                                
                                JButton btn=new JButton("Press");
                                
                                JTextField txt=new JTextField("Sample Text", 10);
                                scrollpane.add(txt);
                                scrollpane.add(btn);
                               
                                }else
                                {
                                    JOptionPane.showMessageDialog(null,"Could not connect to Database");
                                }
                        }
                        catch(Exception ex){
                            JOptionPane.showMessageDialog(null,ex.toString());
              }
                       
                       return new JPanel().add(scrollpane);
                                
    
                  
           
         }
       
        
    }
    Please Help ,Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    And what is the error you receive?
    Make sure you print a stack trace to the error stream and run it via a command line interface as well so you can see the stack failure.

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Am not receiving any errors am using Netbeans IDE v7.1.1 I have also tried on Eclipse Indigo same thing happening.I tried to run the class in the Project folder and I got this:
    Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: LibraryIS (wrong name: library/is/LibraryIS)
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    	at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    	at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: LibraryIS. Program will exit.
    But none of this when I run it through the IDEs

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you run it on the cli, you'd need to add the classpath to point to where it loads the appropriate classes.
    If you're using an IDE, then you'll have a console output as well as the gui available. You can simply add the System.err.println or just an exceptionvar.printStackTrace(); (usually just ex.printStackTrace()) to add the trace.


  •  

    Posting Permissions

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