Code responsible for bringing a list of Bancos de Dados
of a MYSQL
Server and immediately after choosing a Banco
show its 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();
}
}
}
Note that the first getConnection
I do not pass the Banco
name so that I can bring up the list of all Bancos
on that Server ( show databases
command):
DriverManager.getConnection("jdbc:mysql://localhost/","root","senha")
In the next getConnection
I pass Banco
generics and it will bring me the tables contained in this Banco
(command show tables
):
DriverManager.getConnection("jdbc:mysql://localhost/generics","root","senha")
Result obtained:
In this case it was for a localhost bank, but if you put ip externo
of Servidor MYSQL
it also has the same result ...