Conexão MySQL

4 respostas
A

Não estou conseguindo conectar meu banco MySQL com uma aplicação, já alterei drivers, url e nada.

Alguém poderia mandar um exemplo de como fazer.

4 Respostas

alberto_ribeiro

Olá arviana da uma olhada nesse link:

http://www.guj.com.br/java.tutorial.artigo.7.1.guj

espero ter ajudado

[]'s

Bravox

exemplo de conexão :

public class FabricaDeConexao {
	
	 private static final String URL = "jdbc:mysql://localhost/nome_do_banco";
	 private static final String DRIVER = "com.mysql.jdbc.Driver";
	 private static final String USUARIO = "root";
	 private static final String SENHA = "";
	 
	 public static Connection getConnection() throws SQLException{
		 
		 try{
			Class.forName( DRIVER );
			
			return DriverManager.getConnection(URL,USUARIO,SENHA);
		} 
		catch (ClassNotFoundException e ) {
			
			throw new SQLException( e.getMessage());
		}
	 }
}

exemplo de usar a conexão

public class ClienteDAO{
	
    private static Connection conexao;

    public static void loadCliente(Cliente cliente){
        
    	try {
    		
    		conexao = FabricaDeConexao.getConnection();

            PreparedStatement stmt = conexao.prepareStatement(PropertiesManager.getSQl("insert.cliente"));
            
            stmt.setString(1, cliente.getNome());
            stmt.setString(2, cliente.getCpf());
            stmt.setString(3, cliente.getRg());
            stmt.setString(4, cliente.getEmail());
            stmt.setDate(5, cliente.getDataNascimento());
            stmt.setDate(6, cliente.getDataCadastro());
           
            stmt.execute();
            stmt.close();
		} 
    	catch (SQLException e) {
			
    		e.printStackTrace();
		}
    }
}

bravox

alberto_ribeiro

acho que é uma dica interessante: verifique sempre antes se a conexão está null, se tiver você da um getConection senão vc apenas retorna a conexão que está aberta.

[]'s

Alberto

fernandodrg

Como eu configuro o MySQL?
Estou tendo dificuldades.

Um Abraço.

Criado 26 de outubro de 2006
Ultima resposta 27 de out. de 2006
Respostas 4
Participantes 4