A
A
arsden22017-05-04 16:37:39
MySQL
arsden2, 2017-05-04 16:37:39

How to do sorting in mysql?

Hello, for sorting in mysql I am executing the following code.

int i=0;
    String query = "SELECT `{field}` FROM `{table}` ORDER BY `{field}`";
    query = query.replace("{table}", txtTbl.getText());
    switch (jCBoxSort.getSelectedIndex()){
      case 0://типу
        query = query.replace("{field}", "id_vtype");
        break;

      case 1://по назві
        query = query.replace("{field}", "nazva");
        break;

      case 2://по жанру
        query = query.replace("{field}", "id_genre");
        break;

      case 3://по автору
        query = query.replace("{field}", "id_author");
        break;

      case 4://по описанию
        query = query.replace("{field}", "description");
        break;

      case 5://по id
        query = query.replace("{field}", "id");
        break;

      default:
        query = query.replace("{field}", "");

    }

    if (jRadioBtnZrost.isSelected()){//по зростанню
      query += " ASC";
    }
    else{//по спаданню
      query += " DESC";
    }
    ///query(query);
    try {
      Statement statement = SQL.getConn().createStatement();
      statement.execute(query);
      Statement statement1 = SQL.getConn().createStatement();
      ResultSet resultSet = statement.executeQuery(query);
      ResultSet resultBefore = statement1.executeQuery("SELECT id_author FROM video.video_list;");
      StringBuilder message = new StringBuilder();
      StringBuilder before = new StringBuilder();
      while(resultBefore.next()){
        before.append(resultBefore.getString(1) + " ");
      }
      while(resultSet.next()){
        message.append(resultSet.getString(1) + " ");
      }
            System.out.println("Now");
            System.out.println(message);
            System.out.println("Before");
            System.out.println(before);
      String[]data = message.toString().split(" ");
      String[]data1 = before.toString().split(" ");
      for(int d =0;d<data.length;d++) {
        statement.execute("update `video_list` set id_author=" + data[d] + " where id_author=" + data1[d]);
      }
    }catch (Exception e){
      e.printStackTrace();
    }

But it doesn't execute correctly. Here is the console output
.
SELECT `id_vtype` FROM `video_list` ORDER BY `id_vtype` DESC
Now
2122 898 124 87 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 
Before
2 1 1 2 2 1 2 1 7 1 3 3 3 3 3 3 7 3 7 7 3 3 3 3 3 3 3 2 2 1 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
arsden2, 2017-05-05
@arsden2

Decided to make through the temporary table - skidded.
For those interested, here is the code:

int i=0;
    String query = "create table new as select * from {table} order by {field}";
        String request = "drop table {table};";
    String query1 = "create table {table} as select * from new order by {field}";
    String query2 = "drop table new;";
        query = query.replace("{table}", txtTbl.getText());
    query1 = query1.replace("{table}", txtTbl.getText());
    request = request.replace("{table}", txtTbl.getText());
    switch (jCBoxSort.getSelectedIndex()){
      case 0://типу
        query = query.replace("{field}", "id_vtype");
        query1 = query1.replace("{field}", "id_vtype");
                System.out.println("Type");
                break;

      case 1://по назві
        query = query.replace("{field}", "nazva");
        query1 = query1.replace("{field}", "nazva");
                System.out.println("Nazva");
                break;

      case 2://по жанру
        query = query.replace("{field}", "id_genre");
        query1 = query1.replace("{field}", "id_genre");
        System.out.println("genre");
                break;

      case 3://по автору
        query = query.replace("{field}", "id_author");
        query1 = query1.replace("{field}", "id_author");
        System.out.println("author");
                break;

      case 4://по описанию
        query = query.replace("{field}", "description");
        query1 = query1.replace("{field}", "description");
        System.out.println("description");
                break;

      case 5://по id
        query = query.replace("{field}", "id");
        query1 = query1.replace("{field}", "id");
                System.out.println("id");
                break;

      default:
        query = query.replace("{field}", "");
        query1 = query1.replace("{field}", "");
    }

    if (jRadioBtnZrost.isSelected()){//по зростанню
      query += " ASC;";
      query1 += " ASC;";
    }
    else{//по спаданню
      query += " DESC;";
      query1 += " DESC;";
    }
    System.out.println(query);
    System.out.println(request);
    System.out.println(query1);
    System.out.println(query2);
    query(query);
    query(request);
    query(query1);
    query(query2);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question