ResultSet ==> Array

RMI_17

Erfahrenes Mitglied
Hallo Leute,

Bitte helft mir....

Kann man einen ResultSet in einen StringArray schreiben? Wenn ja, wie?

Mfg Rmi
 
Hallo!

Schau mal hier:
Code:
/**
 * 
 */
package de.tutorials;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

/**
 * @author Tom
 * 
 */
public class MySQLExample {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        MysqlDataSource mds = new MysqlDataSource();
        mds.setUser("root");
        mds.setPassword("");
        mds.setServerName("localhost");
        mds.setPort(3306);
        mds.setDatabaseName("test");

        Connection con = mds.getConnection();

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT id,name FROM person");
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        
        List rows = new ArrayList();
        while(rs.next()){
            String[] row = new String[columnCount];
            for(int i = 1;i<=columnCount;i++){
                row[i-1]=rs.getString(i);
            }
            rows.add(row);
        }

        rs.close();
        stmt.close();
        con.close();
        
        String[][] rowData = (String[][])rows.toArray(new String[rows.size()][columnCount]);
        //...
    }
}

Btw. die Arbeit mit CachedRowSets macht viel mehr Spass...

Gruss Tom
 
Zurück