skip to Main Content

I am trying to make a java program program can takes just one row/line from phpMyAdmin database. what I mean is in the photo enter image description here

The problem is that I couldnt figure out how to take just the first line because its always keep giving me all the id’S , dafat’s , etc. Is there any way I can get every line alone or even every data alone (jsut 1 id from the first line for example). I would appreciate who can help me with this.

 Connection con = myConnection.getconnection();
    try{
        PreparedStatement ps = con.prepareStatement("SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "+ff1+"  WHERE 1 ");
        ResultSet resultset = ps.executeQuery();
        System.out.println(resultset.getString("id"));


    }
    catch (Exception e) {

    }

3

Answers


  1. Use the LIMIT clause:

    PreparedStatement ps = con.prepareStatement(
      "SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "
        + ff1 + " LIMIT 1");
    

    If you want the first id value (the smallest one) you can combine with ORDER BY, as in:

    PreparedStatement ps = con.prepareStatement(
      "SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "
        + ff1 + " ORDER BY id LIMIT 1");
    
    Login or Signup to reply.
  2. Replace WHERE whit LIMIT

    PreparedStatement ps = con.prepareStatement("SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "+ff1+" ORDER BY id  LIMIT 1 ");
    
    Login or Signup to reply.
  3. Use Rownum in your where clause like:

    "SELECT * FROM all_objects WHERE rownum < 2"
    

    https://mfaisal1521.blogspot.com/2020/04/rownum-in-sql.html

    And also resultset.getString(“id”) start fetch result from first index of query untill we dont move our cursor to next resultset.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search