package com.text.jdbctest;
import java.sql.*;
public class JdbcTestLocalMysql {
public static void main(String[] args) throws ClassNotFoundException {
String storedProcedure = "/*!mycat: sql=SELECT 1 FROM person; */SET @ids:=11;call batchtest(@v_count,11); SELECT @v_count AS v_count;";
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:8066/mysql";
String user = "mysql";
String password = "nDtZ7rHYjwM5";
ResultSet resultSet = null;
Class.forName(driver);
try(Connection con = DriverManager.getConnection(url, user, password);){
try(Statement statement = con.createStatement();){
resultSet = statement.executeQuery(storedProcedure);
while (resultSet.next()){
System.out.println(resultSet.getString("v_count"));
}
}catch (Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
执行后返回:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'call batchtest(@v_count,11); SELECT @v_count AS v_count' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1198)
at com.text.jdbctest.JdbcTestLocalMysql.main(JdbcTestLocalMysql.java:23)
Mycat版本:Mycat-server-1.6.76-test(GitHub源码)
MySQL版本:mysql8.0