java - SQLException: 错误: "\" 处或附近的语法错误
问题描述
我想将 ma 数据库中的表列表转换为 String。我已经连接数据库,但是当我尝试使用 Postgres "\dt" 函数时,getTables() 会抛出org.postgresql.util.PSQLException: ERROR: syntax error at or near "\".
什么问题?JDK 将简单的反斜杠报告为错误(视为\dt
正则表达式)。
import java.sql.*;
import java.util.ArrayList;
public class DataConnect {
Statement statement = null;
Connection connection = null;
DataConnect() {
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "", "");
statement = connection.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.append("Driver is missing");
end();
} catch (SQLException e) {
e.printStackTrace();
System.out.append("Wrong order");
end();
}
}
ArrayList<String> getTables() {
ArrayList tables = new ArrayList();
try {
ResultSet rs = statement.executeQuery("\\dt");
while (rs.next()) {
String databaseName = rs.getString("Name");
}
return tables;
} catch (Exception e) {
e.printStackTrace();
end();
return null;
}
}
}
解决方案
推荐阅读
- python - 我在 Kivy 中遇到一个属性错误我使用了所有按钮并且我添加了 init 和 Super 有什么问题请专家解释我
- spring-cloud-gateway - Spring Cloud Gateway 传递客户端证书信息
- python - 为什么网格旅行者的时间复杂度为 2^(n+m)?
- javascript - 如何在javascript中将数组中的数组foreach到表中
- google-sheets - 如何为 Excel 中的每一列制作具有特定数据类型格式的数据透视表?
- postgresql - Postgres - 在子查询中引用聚合字段别名
- mongodb - 如何查找重复的文档和过滤元素
- dask - Dask - 重新索引并写回镶木地板 - 内存错误
- r - 在栅格堆栈 qplot 上叠加多边形图层
- windows - 如何查看有多少用户在共享驱动器中打开了文件?