How do I connect a database to a JAVA application

Asked

Viewed 2,111 times

3

I have an app Java and a database MySQL connected in this application on my machine. The application will be distributed to other machines on the same network, but I can’t connect the banco de dados for other machines. The connection works right locally, however, when I try to run the application on another machine on the same network, the connection fails. In Mysql I can access the database remotely. This is the connection code in the application Java (I’m using NetBeans).

    String connectionUrl = "jdbc:mysql://NOMEDOSERVER:3306/DB";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection(connectionUrl, "root", "SENHA");

2 answers

1

The only thing wrong with your code is the unnecessary call to newInstance() (just use Class.forName("com.mysql.jdbc.Driver"); just). Other than that there is nothing wrong with your code.

Are you sure that other machines can see the database server with the given name? To test this use ping. Also, make sure there are no restrictions due to firewalls, Acls and network routing.

  • Yes, there is a ping response, but I don’t know if the problem is that I am using my machine as a database server.

1

I don’t know how familiar you are with JDBC but this is a class that makes a connection to the mysql database, which is your case. Remember that you need a connection jar too...again do not know what the problem you are going through so develop it.

Fabricadeconexao class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class FabricaDeConexao {
  private static final String user = "root";
  private static final String password = "SENHA";
  private static final String url = "jdbc:mysql://localhost:3306/DB";
  private static Connection con = null;

  public static Connection getConexao() {

      try {

          Class.forName("com.mysql.jdbc.Driver");
          con = DriverManager.getConnection(url, user, password);
          return con;

      } catch (Exception e) {
          System.out.println(e.getMessage());
          return null;
      }
  }

  public static void fechar() throws SQLException {
      if (con != null) {
          con.close();

      }
  }

}

Test class:

import java.sql.Connection;
import java.sql.SQLException;


public class TestaConexao {
  public static void main(String[] args) throws SQLException {
      Connection conexao = new FabricaDeConexao().getConexao();
      System.out.println("Conexão aberta!");
      conexao.close();
      
  }

}
  • The database connection works properly on my machine. The application runs normal on my machine, where the database is also. However, when I try to access the application on another machine of the same network, the bank does not connect. By Mysql I can access the database remotely.

  • @Simone Isolate the problem by first trying to access the home screen of the remote Tomcat, that is, try to load the root page of Tomcat http://<ip>:8080/. Only then try to include the database as the previous tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.