"Table name pattern cannot be NULL or empty" in java

When I want to get tables from the MetaData database, I get this error:

Exception in thread "main" java.sql.SQLException: Table name pattern can not be NULL or empty. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:545) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:505) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:479) at com.mysql.cj.jdbc.DatabaseMetaData.getTables(DatabaseMetaData.java:3836) at FindUserTables.main(FindUserTables.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

This my code, my jdbc driver is mysql-connector-java-6.0.5-bin.jar. What happened?

 import java.sql.*; public class FindUserTables { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("Driver loaded"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/javabook ?characterEncoding=utf8&useSSL=false&&serverTimezone=UTC", "root", "123456"); System.out.println("Database connected"); DatabaseMetaData dbMetaData = connection.getMetaData(); ResultSet rsTables = dbMetaData.getTables(null, null, null, new String[] {"TABLE"}); System.out.print("User tables: "); while (rsTables.next()) System.out.print(rsTables.getString("TABLE_NAME") + " "); connection.close(); } } 
+5
source share
4 answers

Try the following:

 DatabaseMetaData dbMetaData = conn.getMetaData(); ResultSet rsTables = dbMetaData.getTables(null, null, "%", null); while (rsTables .next()) { System.out.print(rsTables.getString(3) + " "); } 
0
source

https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-properties-changed.html says in the section "Properties whose values ​​were changed by default:"

nullNamePatternMatchesAll now false by default

Thus, one way to fix this is to add ?nullNamePatternMatchesAll=true to the connection string (replace ?nullNamePatternMatchesAll=true & if this is not the first property in the connection string).

+10
source

Add this to your JDBC URL

 nullNamePatternMatchesAll=true jdbc:mysql://<Host>/<DB>?nullNamePatternMatchesAll=true 
+4
source

You pass null values ​​to dbMetaData.getTables() when one of them (the second last) should be a table name pattern (try "%" for everyone), and as the message says, it cannot be null or empty.

It seems to be configured according to the (old) source code , but I think you should be explicit with the option only for Rest assured.

0
source

Source: https://habr.com/ru/post/1263906/


All Articles