• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

class loader problem during connection of MySql driver

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My program is:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class First
{
public static void main(String args[]) throws Exception
{
Statement stmt = null;
ResultSet rs = null;
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/plantshop?user=root & password = mad");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from Plant");
while(rs.next())
{
System.out.println(rs.getInt(1));
}
}
catch(SQLException ex)
{
System.out.println(ex);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}


Run Time Error:
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:268)
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:164)
at First.main(First.java:16)


Please help me, what is the problem and how will solve?
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you included the driver in the classpath?
 
Madhawi Maurya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Freddy Wong:
Have you included the driver in the classpath?



I have included this driver in the classpath:
C:\Java\jdk1.5.0_01\bin\mysql-connector-java-5.0.3-bin
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be a jar.
Add an entry in CLASSPATH envirinment variable or if you are using and IDE then refer to its documentation to find out as how an external class library be added.
 
Madhawi Maurya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:
It should be a jar.
Add an entry in CLASSPATH envirinment variable or if you are using and IDE then refer to its documentation to find out as how an external class library be added.



I am using Eclipse. Eclipse is new for me. Is there any other method to set classpath
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Madhawi:


I am using Eclipse. Eclipse is new for me. Is there any other method to set classpath



Make a runtime of Java Applications type and go to classpath and then add external jars.

Please check with the Javarnach's naming policy.
http://www.javaranch.com/name.jsp
 
Madhawi Maurya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:


Make a runtime of Java Applications type and go to classpath and then add external jars.

Please check with the Javarnach's naming policy.
http://www.javaranch.com/name.jsp



Thanks, to solve my problem. I Added external jar file. now i got another problem. Help me.

i am getting error:
java.sql.SQLException: Access denied for user ''root''@'localhost' (using password: NO)

what is the problem?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhawi, please check your private messages for an important message from JavaRanch. You can see your private messages by clicking My Profile.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mahadewi... Your display name is still not according to the JavaRanch naming policy.

Please read the naming policy carefully and change your name immediately.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should give access to the client host in the mysql server

-Medha
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic