Results 1 to 4 of 4
  1. #1
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default Can NOT connect to Mysql

    Hi All
    Below is my code for a simple form which connects to Mysql. Problem is if I just connect to database and not have any other code (i.e Form) that works fine but when I try to connect with the form code, it gives me errors. why??? some help will be greatly appreciated thank you.
    Zed

    code
    Java Code:
     
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.SQLException; 
    import java.sql.Driver;
    
    public class PersonalInfo extends Frame {
    
    Connection conn=null; 
    Panel Np = new Panel();
    Panel Sp = new Panel();
    
    public PersonalInfo(){
    
    NorthPanel();
    add(Np,BorderLayout.NORTH);
    SouthPanel();
    add(Sp,BorderLayout.SOUTH);
    }
    public void NorthPanel(){
    Label LfirstName = new Label("First Name");
    TextField TfirstName = new TextField(20);
    Label LlastName =new Label("Last Name");
    TextField TlastName=new TextField(20);
    Np.setLayout(new GridLayout(2,1));
    Np.add(LfirstName);
    Np.add(TfirstName);
    Np.add(LlastName);
    Np.add(TlastName);
    }
    public void SouthPanel(){
    Button Submit=new Button("Submit");
    Sp.add(Submit);
    Button Cancel = new Button("Cancel");
    Sp.add(Cancel);
    } 
    public static void connect(){
    
    System.out.println("MySQL Connect Example"); 
    String url = "jdbc:mysql://localhost:3306/"; 
    String dbName = "test"; 
    String userName = "c3031843"; 
    String password = "vw84a3"; 
    String driver = "com.mysql.jdbc.Driver"; 
    
    try {
    Statement stmt;
    ResultSet rs;
    
    Class.forName(driver).newInstance(); 
    Connection conn = DriverManager.getConnection(url+dbName,userName,password); 
    System.out.println("URL: " + url);
    System.out.println("Connection: " + conn);
    System.out.println("Connected to the database");
    
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT * " + "from category");
    System.out.println("Display all results:");
    while(rs.next()){
    int Int= rs.getInt("cat_id");
    String name = rs.getString("category");
    System.out.println("\tid= " + Int + "\tstr = " + name);
    }//end while loop
    
    conn.close(); 
    System.out.println("Disconnected from database"); 
    } 
    catch (Exception e) { 
    e.printStackTrace(); 
    } 
    }
    public static void main (String[] args) {
    System.out.println("Personal information");
    PersonalInfo PerInfo = new PersonalInfo();
    PerInfo.setTitle("Personal Information");
    PerInfo.setSize(400,200);
    PerInfo.setVisible(true);
    connect();
    PerInfo.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    }
    
    }
    Errors Code
    Java Code:
     
    Personal information
    MySQL Connect Example
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at PersonalInfo.connect(PersonalInfo.java:52)
    at PersonalInfo.main(PersonalInfo.java:80)
    
    Process completed.

  2. #2
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    Is MySQL driver in your classpath?
    Please don't laugh at my English... I'm trying my best! :)

  3. #3
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default

    Yes it is, like I said if I don't use the form code and just use the code that connect to database it works like charm.
    Zed

  4. #4
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default

    Thanks everyone it been sorted just looked at it the path was NOT correct

Similar Threads

  1. J2EE = Servlet+Mysql connect
    By initmax in forum Java Servlet
    Replies: 2
    Last Post: 10-30-2009, 10:42 AM
  2. J2EE = Servlet+Mysql connect
    By initmax in forum Java Servlet
    Replies: 2
    Last Post: 10-30-2009, 08:57 AM
  3. connect java with mysql in rhel5
    By niraj.vara in forum New To Java
    Replies: 0
    Last Post: 07-17-2009, 10:23 AM
  4. Connect applet with mysql
    By baltimore in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:39 AM
  5. Connect applet with mysql
    By Felissa in forum Java Applets
    Replies: 3
    Last Post: 07-05-2007, 07:26 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
  •