goinception是一个sql审核工具,官方提供了python和go语言的调用demo,今天尝试用java调用却提示“Statement.executeQuery() cannot issue statements that do not produce result”,反复确认sql没写错。后来想可能是版本问题,测试一番果然如此。
我一开始使用的mysql-connector-java版本是8.0.31,驱动类为Class.forName("com.mysql.cj.jdbc.Driver");
后来改为8.0.11,驱动类为Class. forName ("com.mysql.jdbc.Driver");
,测试发现8.0.31可能是不支持,调用总是报错。
下面给出一个完整调用demo:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
package cn.f.test;
import java.io.IOException;
import java.sql.*;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
// Class.forName("com.mysql.cj.jdbc.Driver");
Class. forName ("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:4000", "", "");
String sql = "/*--user=ltest2;--password=fdsfdsaur90f37;--host=127.0.0.1;--check=1;--port=3306;*/\n" +
"inception_magic_start;\n" +
"use dbtest;\n" +
"select id from email;\n" +
"inception_magic_commit;";
ResultSet rs = conn.createStatement().executeQuery(sql);
ResultSetMetaData m = rs.getMetaData();
int columns = m.getColumnCount();
//显示列,表格的表头
for (int i = 1; i <= columns; i++) {
System.out.print(m.getColumnName(i));
System.out.print("\t\t");
}
System.out.println();
//显示表格内容
while (rs.next()) {
for (int i = 1; i <= columns; i++) {
System.out.print(rs.getString(i));
System.out.print("\t\t");
}
System.out.println();
}
}
}