I'd like to retrieve all tables from a database, in order to count their rows and eventually count all elements currently in the database. I'm trying to do this using Java's DatabaseMetaData.
This is the contents of my constructor, which is responsible for establishing the connection to the database:
String driver = "com.mysql.cj.jdbc.Driver";
String fullUrl = "jdbc:mysql://"+ url + "/" + db;
try {
Class.forName(driver);
setConn(DriverManager.getConnection(fullUrl, username, password));
}
The connection is made to a WAMP server, and is fully functional based on many attempts before this. Could someone help me with this?
This is the method I'm implementing to count all elements in the database, and the countRowsInTable method is already implemented and working.
public int countElementsInDB(String dbName) {
DatabaseOperations db = new DatabaseOperations("localhost:3306", dbName, user, password);
try {
DatabaseMetaData databaseMetaData = conn.getMetaData();
String[] types = {"TABLE"};
ResultSet rs1 = databaseMetaData.getTables(null, null, "%", types);
int counter = 0;
while(rs1.next()){
counter += countRowsInTable(rs1.getString("TABLE_NAME"));
}
return counter;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
The code is working, but is in fact returning all tables from all databases present in the server, even though the URL I'm using indicates the database in question here. How do I get it to only return tables from the database I'm targeting?