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.
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.
Olá arviana da uma olhada nesse link:
http://www.guj.com.br/java.tutorial.artigo.7.1.guj
espero ter ajudado
[]'s
exemplo de conexão :
[code]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());
}
}
}[/code]
exemplo de usar a conexão
[code]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();
}
}
}[/code]
bravox
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
Como eu configuro o MySQL?
Estou tendo dificuldades.
Um Abraço.