英文:
Method for each String
问题
Doing sql request, to get some info from my db
public String chat_id() {
String userid = null;
try {
PreparedStatement st = null;
String query = "select chat_id FROM id";
st = con.prepareStatement(query);
rs = st.executeQuery();
while (rs.next()) {
userid = rs.getString("chat_id");
System.out.println("userid : " + userid);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userid;
}
Other class, other method, return info from db to String, but, in my database not one value for my request,
public void sendMsg() {
Login lgc = new Login();
String chat = lgc.chat_id();
String text = "test";
SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(chat);
//sendMessage.setReplyToMessageId(message.getMessageId());
sendMessage.setText(text);
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
There is a question: Method sendMsg needs to be run for several values of String chat_id. At this moment it's only run for the last value from the database. What should I do?
英文:
Doing sql request, to get some info from my db
public String chat_id() {
String userid = null;
try {
PreparedStatement st = null;
String query = "select chat_id FROM id";
st = con.prepareStatement(query);
rs = st.executeQuery();
while (rs.next()) {
userid = rs.getString("chat_id");
System.out.println("userid : " + userid);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userid;
}
Other class, other method, reurnt info from db to String, but, in my data base not one value for my requst,
public void sendMsg(){
Login lgc= new Login();
String chat = lgc.chat_id();
String text = "test";
SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(chat);
//sendMessage.setReplyToMessageId(message.getMessageId());
sendMessage.setText(text);
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
There is a question.: Method sendMsg need to be run for several values of String chat_id. At this moment it`s run for last value from db. what shell I do?
答案1
得分: 0
你可以将它们存储到一个列表中,然后在另一个类中适当地读取,如下所示:
public List<String> chat_id() {
String userid = null;
List<String> userids = new ArrayList<>();
try {
PreparedStatement st = null;
String query = "select chat_id FROM id";
st = con.prepareStatement(query);
rs = st.executeQuery();
while (rs.next()) {
userid = rs.getString("chat_id");
userids.add(userid);
System.out.println("userid : " + userid);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userids;
}
你可以这样从列表中获取值:
for(int i = 0; i < userids.size(); i++){
String userid = userids.get(i);
}
英文:
You can store them into a list, then read them appropriately in the other class like so:
public List<String> chat_id() {
String userid = null;
List<String> userids = new ArrayList<>();
try {
PreparedStatement st = null;
String query = "select chat_id FROM id";
st = con.prepareStatement(query);
rs = st.executeQuery();
while (rs.next()) {
userid = rs.getString("chat_id");
userids.add(userid);
System.out.println("userid : " + userid);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userids;
}
You can get the values from the list like so:
for(int i = 0; i < userids.size; i++){
String userid = userids.get(i);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论