Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Table name pattern can not be NULL or empty" in java

Tags:

java

mysql

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();
    }
}
like image 850
US1549 Avatar asked Feb 07 '17 08:02

US1549


2 Answers

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 now false 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).

like image 52
nafg Avatar answered Nov 03 '22 08:11

nafg


In your JDBC URL add this

nullNamePatternMatchesAll=true

jdbc:mysql://<Host>/<DB>?nullNamePatternMatchesAll=true
like image 5
ketankk Avatar answered Nov 03 '22 08:11

ketankk