-
首先创建一个JdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
-
使用queryForObject方法执行一个SQL查询,该查询返回一个值
String value = jdbcTemplate.queryForObject("SELECT @@GLOBAL.max_connections", String.class);
-
使用该值进行操作
System.out.println("Max Connections: " + value);
完整的代码示例如下:
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class MySqlVariableExample {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void printMaxConnections() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String value = jdbcTemplate.queryForObject("SELECT @@GLOBAL.max_connections", String.class);
System.out.println("Max Connections: " + value);
}
}