How to recognize Mysql instances using Java and allow the user to choose the database?

Asked

Viewed 432 times

3

I am developing a project that is requested that it is possible to access different benches of MySQL which are allocated on machines and also on servers.

In a way that allows the selection of a database by the user who will execute the routines in this selected database. If someone knows how it is done to get the list of databases and tables of these on the machine and how this process is done in an external server link.

I guess I’ll have to take all the schema of benches, but I’m not clear what file and how I’m going to do it in the server case. For all intents and purposes I’m developing in Java this project.

  • you know beforehand the bank name, password, login, etc? or you want to make a program that searches the user’s computer behind banks that you do not know the name and address?

  • 1

    Yes he has to sift through the metadata to find out the BD names and table names and their fields, the password and login will be requested normally.

  • I suggest searching the doors TCP/IP open that these banks usually leave

1 answer

3


Code responsible in bringing up a list of Bancos de Dados of a Server MYSQL and soon after chosen a Banco show their Tabelas.

package javaapplication1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JavaApplication1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {        
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("---------------------------------------------------------------");
        try (Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/","root","senha")) {                        
            ResultSet result = conexao.createStatement().executeQuery("show databases;");
            System.out.println("Bancos de Dados");
            while (result.next()){
                System.out.println(result.getString(1));
            }            
            conexao.close();            
        }
        System.out.println("");        
        System.out.println("---------------------------------------------------------------");
        try (Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/generics","root","senha")) {                        
            ResultSet result = conexao.createStatement().executeQuery("show tables;");
            System.out.println("Tabelas de Banco de Dado Selecionado (Banco: generics)");
            while (result.next()){
                System.out.println(result.getString(1));
            }            
            conexao.close();            
        }        
    }
}

Notice that the first getConnection I don’t pass the name of Banco so I can bring up the list of all the Bancos existing on that Server (command show databases):

DriverManager.getConnection("jdbc:mysql://localhost/","root","senha")

Next time getConnection I pass the Banco Generics and he will bring me the tables contained in this Banco (commando show tables):

DriverManager.getConnection("jdbc:mysql://localhost/generics","root","senha")

Result obtained: inserir a descrição da imagem aqui

In this case it went to a localhost bank (location of the machine), but if you put the ip externo of Servidor MYSQL also has the same result ...

Browser other questions tagged

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