“每个字符串的方法”

huangapple go评论76阅读模式
英文:

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&lt;String&gt; chat_id() {
     String userid = null;
     List&lt;String&gt; userids = new ArrayList&lt;&gt;();
     try {
        
         PreparedStatement st = null;
         String query = &quot;select chat_id FROM id&quot;;
         st = con.prepareStatement(query);
         rs = st.executeQuery();
         while (rs.next()) {
             userid = rs.getString(&quot;chat_id&quot;);
             userids.add(userid);
             System.out.println(&quot;userid : &quot; + userid);
         }
     } catch (SQLException e) {
         e.printStackTrace();
     }
     return userids;

 }

You can get the values from the list like so:

for(int i = 0; i &lt; userids.size; i++){
    String userid = userids.get(i);
}

huangapple
  • 本文由 发表于 2020年10月9日 05:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64271056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定