简体   繁体   中英

Retrieve data from MySQL DB using java

In here i want to obtain data from the MySQL DB & run the play method.but it execute at once.i wanted to get row one information pass that in to the ply method & get the second row information & pass it to the play method so on. please help me to resolve this. thank u in advance.

public class Player {

static Play PL = new Play();

public static void main(String[] args) {

    try {

        Statement stmt = null;
        // connect to database radio
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/radio", "root", "");
        stmt=conn.createStatement();

            String sql = "SELECT Link FROM split";
            ResultSet rs = stmt.executeQuery(sql);
            //STEP 5: Extract data from result set
                while(rs.next()){
                    //Retrieve by column name

                    int numColumns = rs.getMetaData().getColumnCount();

                    for ( int i = 1 ; i <= numColumns ; i++ ) {

                        System.out.println(numColumns);
                        String SongLocation = rs.getString(i);

                    System.out.println(SongLocation);

                    PL.play(SongLocation);

                    System.out.println("playing song");   

                    }
          }

    } catch (SQLException e1) {

        e1.printStackTrace();
    }

}

}

I think you are looping through columns by using

int numColumns = rs.getMetaData().getColumnCount();
for ( int i = 1 ; i <= numColumns ; i++ ) {
 ...

instead try using a while loop

 while (rs.next()) {
 ...

and because you need the index, inlude a local variable int i, that you just increment after each while loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM