gpt4 book ai didi

java - 使用 Java 将 Redis 数据库连接到 Azure 云

转载 作者:行者123 更新时间:2023-12-03 05:29:17 26 4
gpt4 key购买 nike

美好的一天,我的 Java 代码中出现此错误。我正在尝试将我的数据库链接到 azure 云。我使用了列表命令:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at anothercloud.Anothercloud.main(Anothercloud.java:63)
Java Result: 1

package anothercloud;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
*
* @author ariel
*/
public class Anothercloud {
static HashMap<Double, String> redisData = new HashMap<Double, String>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean useSsl=true;
inclass infos=new inclass();
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> value = new ArrayList<String>();
String anjiecachekey="xxxx=";

JedisShardInfo shardInfo=new JedisShardInfo("anjiecloud.redis.cache.windows.net",6380,useSsl);//use secondary connection string
shardInfo.setPassword("xxxx=");//use secondary access key
//Simple PING command
Jedis jedis=new Jedis(shardInfo.getHost());
try{
jedis.auth(anjiecachekey);
jedis.connect();
System.out.println("My Cache Response: "+jedis.ping());

if (jedis.llen("state") == 0 && jedis.llen("number") == 0) {


for(Map.Entry m: inclass.map.entrySet()){

jedis.lpush("state",(String)m.getValue());
jedis.lpush("number",m.getKey().toString());
}

}
for(String s: jedis.lrange("state", 0, 1000)){
names.add(s);

}
for(String r: jedis.lrange("number", 0, 1000)){
value.add(r);
}
for(int i =0; i < names.size(); i++){
redisData.put(Double.parseDouble(value.get(i)), names.get(i));
}


ArrayList<String> states = new ArrayList<String>();
for (Map.Entry m : redisData.entrySet()) {
states.add((String)m.getValue());
}

String[] statesArray = new String[states.size()];
states.toArray(statesArray);



JComboBox<String> stateList = new JComboBox<>(statesArray);
stateList.addItemListener(new MyHandler());

JFrame jframe = new JFrame();
JLabel item1 = new JLabel("IGR Statistics H1 2020");
jframe.add(item1);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLayout(new FlowLayout());
jframe.setSize(400,200);
jframe.setVisible(true);

jframe.add(stateList);



// get the selected item:
// String selectedBook = (String) stateList.getSelectedItem();


// check whether the server is running or not
System.out.println("Server is running: " + jedis.ping());
//getting the percentage for each state


// storing the data into redis database


for (Map.Entry m : inclass.map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());


}
}
catch(JedisConnectionException e){
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null,"You require a working internet connection");
}
}

static class MyHandler implements ItemListener{


@Override
public void itemStateChanged(ItemEvent e) {
for (Map.Entry m : redisData.entrySet()) {
if(e.getItem().toString() == m.getValue()&& e.getStateChange() == 1){

JOptionPane.showMessageDialog(null, m.getKey(), "VALUE IN BILLIONS", 1);

System.out.println(m.getKey());
break;

}



}

}

}
}

请问我该如何纠正这个错误?任何反馈将不胜感激。我对 Java 语言及其语法还很陌生。该错误似乎也来 self 的 for 循环。该错误似乎也来 self 的 for 循环。

最佳答案

如错误所述,您将看到 IndexOutOfBoundsException。这意味着您正在尝试访问大于/小于其范围的列表索引。在这种情况下,value.get(i) 抛出错误:

for (int i = 0; i < names.size(); i++)
{
redisData.put(Double.parseDouble(value.get(i)), names.get(i));
}

尝试修改如下代码:

for (int i = 0; i < names.size() - 1; i++)
{
redisData.put(Double.parseDouble(value.get(i)), names.get(i));
}

此外,我建议您重新访问 Redis 缓存的 java 实现:Intro to Jedis – the Java Redis Client Library

关于java - 使用 Java 将 Redis 数据库连接到 Azure 云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66520158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com