Re: solving paths on the jdbc archive

Douglas Nelson <[email protected]> Sun, 19 Apr 2009 06:37:17 -0400
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
This is the classpath I set:

classpath=.;c:\;c:\download\java\commons-logging-1.0.4.jar;c:\jarfiles\log4j-1.2.8.jar;c:\jarfiles\mysql-connector-java-5.1.5-bin.jar;

this is the code I use to connect to a MySql database
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// start getConnection
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   /**
    * Creates a new connection to the database
    * @return the connection object
    */
   private Connection getConnection() throws Exception{
      logger.info("getConnection called");
      //------------------------
      // Load up the jdbc driver
      //------------------------
      try {
         Class.forName("com.mysql.jdbc.Driver").newInstance();
      } // end try
      catch (Exception any) {
         logger.error("Unable to load driver. [" + any.getMessage() + "]");
         throw(any);
      } // end catch

      return(DriverManager.getConnection("jdbc:mysql://" + mysql_host + 
":" + mysql_port +"/" + mysql_database + "?user=" + mysql_user + 
"&password=" + mysql_password));

   } // end getConnection



Michel Maria-Sube wrote:
> hello,
>
> I'm working on an application connecting with mysql5, considering that $CLASSPATH is correctly set and piece of code for connection is as follows:
>
> 	public void connectDB( String DBname, String user, String passwd ) {
> 		java.sql.Connection conn = null;
> 		String	url = null;
>
> 		try {
> 			Class.forName("com.mysql.jdbc.Driver").newInstance();
> 		}
> 		catch (ClassNotFoundException cnfe) {
> 			System.out.println("\nClassNotFoundException occured");
> 			System.out.println("connection with the DB is not possible");
> 			cnfe.printStackTrace();
> 			System.exit(ERROR);
> 		}
> 		catch (InstantiationException ie) {
> 			System.out.println("InstantiationException occured");
> 			System.out.println("connection with the DB is not possible");
> 			System.exit(ERROR);
> 		}
> 		catch (IllegalAccessException iae) {
> 			System.out.println("IllegalAccessException occured");
> 			System.out.println("connection with the DB is not possible");
> 			System.exit(ERROR);
> 		}
> 		try {
> 			url = new String("jdbc:mysql://localhost/"+DBname+"?user="+user+"&password="+passwd);
> .......................
>
> Execution gives following exception:
>
> ClassNotFoundException occured
> connection with the DB is not possible
> 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 MainFrame.connectDB(MainFrame.java:290)
>         at MainFrame.createMainScreen(MainFrame.java:260)
>         at MainFrame.<init>(MainFrame.java:68)
>         at MainFrame.main(MainFrame.java:1049)
>
> This behavior disapears if I update the jar archive for executable with classes content in the jdbc archive but of course I don't want to do that; does anybody could indicate me why java is anable to solve correctly paths in the jdbc archive?
>
> Thank you in advance
> Regards
> M.
>
>   

-- 
<http://sfp.central.sun.com/> 	* Douglas Nelson *
Senior Technical Consultant

*Need Sun JES Help?*
Software Field Practice
Phone +1 877-234-2879/x51438
Mobile 919-259-3719
Email [email protected]
<http://blogs.sfbay/roller/page/sfp>


-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]