<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Connection connection = null;
try {
// JDBC 드라이버 로드
Class.forName("com.redis.Client");
// RediSQL에 연결
connection = DriverManager.getConnection("jdbc:redis://localhost:6379");
// SQL 쿼리 실행
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT 1");
// 결과 처리
while (resultSet.next()) {
// 처리 로직 작성
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
연결 실패 후
@Autowired
private Jedis jedis;
public static void main(String[] args) {
SpringApplication.run(RedisExampleApplication.class, args);
}
@Override
public void run(String... args) {
// Redis에 데이터 쓰기
jedis.set("city", "daegu");
// Redis에서 데이터 읽기
String value = jedis.get("city");
System.out.println("Value from Redis: " + value);
}
기본적인 jedis 명령문 →
try (Jedis jedis = new Jedis("localhost", 6379)) {
// RediSQL에 SQL 쿼리 실행
String result = jedis.sendCommand("REDISQL.EXEC", "SELECT * FROM your_table");
// 결과 처리
System.out.println(result);