When I want to get tables from databaseMetaData,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();
}
}
https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-properties-changed.html says under "Properties that have their default values changed:"
nullNamePatternMatchesAll
is nowfalse
by default
So one way to fix it is by appending ?nullNamePatternMatchesAll=true
to the connection string (replace the ?
with &
if it's not the first property in the connection string).
In your JDBC URL add this
nullNamePatternMatchesAll=true
jdbc:mysql://<Host>/<DB>?nullNamePatternMatchesAll=true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With