Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

jdbc - Java, looping through result set

In Java, I have a query like this:

String querystring1= "SELECT rlink_id, COUNT(*)"
                   + "FROM dbo.Locate  "
                   + "GROUP BY rlink_id ";

The table rlink_id has this data:

Sid        lid
 3           2
 4           4
 7           3
 9           1

How do I extract these values with a Java ResultSet?

Here is what I have so far:

String show[] = {rs4.getString(1)};
String actuate[] = {rs4.getString(2)};
asString = Arrays.toString(actuate);
question from:https://stackoverflow.com/questions/7643576/java-looping-through-result-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
List<String> sids = new ArrayList<String>();
List<String> lids = new ArrayList<String>();

String query = "SELECT rlink_id, COUNT(*)"
             + "FROM dbo.Locate  "
             + "GROUP BY rlink_id ";

Statement stmt = yourconnection.createStatement();
try {
    ResultSet rs4 = stmt.executeQuery(query);

    while (rs4.next()) {
        sids.add(rs4.getString(1));
        lids.add(rs4.getString(2));
    }
} finally {
    stmt.close();
}

String show[] = sids.toArray(sids.size());
String actuate[] = lids.toArray(lids.size());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...