Database with Java connection.

Antonio Paulo <[email protected]> Thu, 15 Jul 2010 10:13:56 -0300
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
--------------010202000909000504030903
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I'm trying to connect to my MySql database using the class 
ConnectionFactory(attached).Unfortunatelly i have been no sucess  on it.
For your information i have used the Netbeans 6.8  IDE.  The Connector/J 
driver  belongs to it. Attached the above mentioned class and the error 
messages i received.
Thanks a lot.

Antonio Paulo.

--------------010202000909000504030903
Content-Type: text/plain;
 name="ConnectionFactory.txt"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline;
 filename="ConnectionFactory.txt"

package br.com.caelum.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

public class ConnectionFactory
{
    public Connection getConnection()
    {
        System.out.println("Conectando ao Banco de Dados");

	try
	{
	       Class.forName("com.mysql.jdbc.Driver");//trecho adicionado, carregar o                                 Driver JDBC do MySQL
                                return
DriverManager.getConnection("jdbc:mysql://localhost:3306/caelum","root","senha");
	}
	catch(Exception e)//mudei o tipo da excessão para um tipo mais genérico para ele nao dar erro, mas era possível colocar mais um catch para tratar outros tipos de excessao
	{
                    throw new RuntimeException(e);

	}//fim do try/catch.

     }//fim do método getConnection.

}//fim da classe ConnectionFactory.

________________________________________________________________________________

Após as modificações propostas obtive as mensagens de erro abaixo: 


        Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException:         com.mysql.jdbc.Driver
        at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:21)
        at br.com.caelum.jdbc.teste.TestaConexao.main(TestaConexao.java:13)
        Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:15)
        ... 1 more
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)


--------------010202000909000504030903
Content-Type: text/plain; charset=us-ascii


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