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:
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 ...
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?
– Math
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.
– Igoto
I suggest searching the doors
TCP/IP
open that these banks usually leave– Roger Barretto