当我想从databaseMetaData获取表时,我收到此错误:
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)
这是我的代码,我的jdbc驱动程序是mysql-connector-java-6.0.5-bin.jar。发生了什么事?
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();
}
}
答案 0 :(得分:11)
https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-properties-changed.html在“默认值已更改的属性:”
下说
nullNamePatternMatchesAll
现在默认为false
因此,解决此问题的一种方法是将?nullNamePatternMatchesAll=true
附加到连接字符串(如果?
不是连接字符串中的第一个属性,则将&
替换为ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrayList);
holder.spinner.setAdapter(itemsAdapter);
itemsAdapter.notifyDataSetChanged();
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = holder.spinner.getSelectedItem().toString();
System.out.println("selected = " + selected);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
。
答案 1 :(得分:5)
在您的JDBC URL中添加此
nullNamePatternMatchesAll=true
jdbc:mysql://<Host>/<DB>?nullNamePatternMatchesAll=true
答案 2 :(得分:0)
您将null
值传递给dbMetaData.getTables()
,其中一个(最后一个)应该是表名模式(对所有人都尝试"%"
)并且正如消息所示,它不能是null
或空。
似乎可以根据(较旧的)source code进行配置,但我想你应该明确参数,以确定。
答案 3 :(得分:0)
试试这个:
DatabaseMetaData dbMetaData = conn.getMetaData();
ResultSet rsTables = dbMetaData.getTables(null, null, "%", null);
while (rsTables .next()) {
System.out.print(rsTables.getString(3) + " ");
}