Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default com.mysql.jdbc.Driver Exception !

    Hey everyone, Hoe you're just fine, despite I run that code at the lab. in the university without error, but it just does not work on my pc, everytime i run that program, I got com.mysql.jdbc.Driver, how the database works fine and I make queries and updates to the table inside, but I just can't use java to make queries and updates, here's the code :


    Java Code:
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    public class MyProcedure extends javax.swing.JFrame {
    
        public MyProcedure() {
            initComponents();
        }
        
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            txtid = new javax.swing.JTextField();
            load = new javax.swing.JButton();
            txtname = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            load.setText("Load");
            load.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loadActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(107, 107, 107)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtname, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(load, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtid, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
                    .addContainerGap(124, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(load)
                    .addGap(18, 18, 18)
                    .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(180, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void loadActionPerformed(java.awt.event.ActionEvent evt) {                                     
           Connection cn = null;
             
             String url = "jdbc:mysql://127.0.0.1:3306/alaa";
             String user ="root";
             String pass = "123";
            try {
                int i = Integer.parseInt(txtid.getText());
             Class.forName("com.mysql.jdbc.Driver").newInstance();
             cn =  DriverManager.getConnection(url,user,pass);
             CallableStatement cs = cn.prepareCall(" call MaProcedure(?)");
             cs.setInt(1, i);
             ResultSet rs = cs.executeQuery();
             while(rs.next()){
                 txtname.setText(rs.getString(2));
             }
    
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
    
        }                                    
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MyProcedure().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton load;
        private javax.swing.JTextField txtid;
        private javax.swing.JTextField txtname;
        // End of variables declaration                   
    
    }
    Note: I have broadband cable installed on my pc .
    please help me out figuring out what's wrong with ma code .
    thank you
    Last edited by DarrylBurke; 06-09-2011 at 10:14 AM. Reason: Change QUOTE tags to CODE tags

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Post the full exception you are receiving. Exceptions are gold mines when it comes to debugging applications. Second, you mention using 2 computers with only 1 working - 127.0.0.0 is localhost (eg the computer you are running on), is the database and its associated tables/data installed?

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    You are using netbeans? Did you already add MySQL library to your project?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    com.mysql.jdbc.Driver Exception is not an exception.
    It's a fraction of the text of your exception.
    You're also losing the stack trace so you'll not find out (easily) where the exception came from.
    Instead of doing this:
    Java Code:
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
    }
    do this:
    Java Code:
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        ex.printStackTrace();
    }
    to at least get a stack trace you can use.

    Oh, and use code tags, not quote tags for code.

  5. #5
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Hey, I added ex.printStackTrace(); to the exception and running the code resulted in :

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    by the way I've already copied mysql-connector-java-5.0.8-bin file to C:\Program Files\Java\jre6\lib on my pc .

    thanks a million

  6. #6
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Good news, I fixed the problem up by adding mysql-connector-java-5.0.8-bin to my project properties but can i ask, do i have to add jdbc driver to every project I create ?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by RealSilhouette View Post
    Hey, I added ex.printStackTrace(); to the exception and running the code resulted in :

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    by the way I've already copied mysql-connector-java-5.0.8-bin file to C:\Program Files\Java\jre6\lib on my pc .

    thanks a million
    Then you are not finding out where the stack trace is going...it won't (for example) be printing in the JOptionPane. It will be in whatever console applies to your app (or log file).

    Also, do not add libraries to your JRE (or JDK) deployment.

    Answering your followup, yes. You add libraries to your applications. Different applications may require different version of a jar file.

Similar Threads

  1. com.mysql.jdbc.Driver
    By uthpalaw in forum Eclipse
    Replies: 2
    Last Post: 10-14-2010, 05:09 AM
  2. JDBC Driver
    By Ursula in forum New To Java
    Replies: 6
    Last Post: 08-23-2010, 05:41 PM
  3. Replies: 4
    Last Post: 03-31-2010, 12:08 PM
  4. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  5. Replies: 1
    Last Post: 10-16-2008, 02:09 PM

Posting Permissions

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