Results 1 to 8 of 8
  1. #1
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default How i know how many lines were replied from a query?`

    Using jdbc and a SQL database, I want to know how many lines are replied when I do a select... and then store this number in a database.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cotarelo View Post
    Using jdbc and a SQL database, I want to know how many lines are replied when I do a select... and then store this number in a database.
    You either have to do it yourself by incrementing a counter each time you read another row from the ResultSet or you issue another query first "select count(*) from ...". In the first case you only know the number of rows at the end, in the second case you have to issue another query to get those rows (and the tables might have been changed) ...

    kind regards,

    Jos

  3. #3
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    and how I know when I read a new row with the ResultSet? The only way I can imagine is knowing the number of elements that the table has and knowing that I can create a do while.

    Ahaa the count function is what I was looking for ...

    Thanks! :)

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    You can go for scrollable resultset.
    Point the ResultSet to last row using ResultSet method last.
    2.Using getRow() method of ResultSet get rowcount.
    Ramya:cool:

  5. #5
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Hmmmm Still I dont know really how to do what I want.

    OK here is the problem. I developed a program that receives data from a socket and queries a database with the data received: if the query gives a result, it will send the information of each column of the database trough a socket.

    The program works great if just one line of the database matches, but when is two (or more), I want that the program sends them on a socket line after line

    Here is my code:

    Java Code:
    class GestorPetition extends Thread {
    	BufferedReader entrada = null;
    	Socket s;
            JTextField databaseTextField;
            JTextField  PEPhostTextField;
            JTextField  PEPportTextField;
    
    	public GestorPetition(Socket s, JTextField databaseTextField, JTextField  PEPhostTextField, JTextField  PEPportTextField){
    		this.s = s;
                    this.databaseTextField = databaseTextField;
                    this.PEPhostTextField = PEPhostTextField;
                    this.PEPportTextField = PEPportTextField;
    	}
    
        @Override
    	public void run(){
    		try{
    			entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));
                            String url = databaseTextField.getText(); //"jdbc:mysql://localhost/Mydatabase";
                            Connection con = DriverManager.getConnection(url, "root", "1234");
    
    
    			while (true){
    				String str = entrada.readLine();
                                    if (str!=null){
    				System.out.println("Receiving: " + str);
                                    System.out.println("Checking if there is some match");
                                    Statement select2 = con.createStatement();
                                    ResultSet actualsituation = select2.executeQuery("SELECT ifx FROM mytable WHERE whenx='"+str+"';");
    
    
                                    if (actualsituation.next()){ // rs.next() is false if nothing is found
                                       System.out.println("Lets retreive the values of the match");
                                       select2.close();
    
    
        try {
          con = DriverManager.getConnection(url, "root", "1234");
          Statement select = con.createStatement();
    
          ResultSet result = select.executeQuery("SELECT * FROM mytable WHERE whenx='"+str+"';");
    
    
          System.out.println("Got results:");
          while (result.next()) { // process results one row at a time
              String Nr = result.getString(1);
              int Dp = result.getInt(2);
              int At = result.getInt(3);
              int Lv = result.getInt(4);
              String Bp = result.getString(5);
              String ifx = result.getString(6);
              String whenx = result.getString(7);
    
            System.out.println("WHEN = "+whenx);
            System.out.println("IF = "+ifx);
            System.out.println("Name = "+Nr);
            System.out.println("DP = "+Dp);
            System.out.println("AT = "+At);
            System.out.println("VL = "+Lv);
            System.out.println("BP = "+Bp);
    
    
            Socket echoSocket = null;
            PrintWriter out = null;
    
    
            try {
                echoSocket = new Socket(PEPhostTextField.getText().toString(),Integer.parseInt(PEPportTextField.getText()));
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                } catch (UnknownHostException e) {
                System.err.println("Don't know about host: "+PEPportTextField.getText());
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: "+PEPhostTextField.getText().toString());
                System.exit(1);
            }
    
    
            out.println(String.valueOf(Dp));
            out.println(String.valueOf(At));
            out.println(String.valueOf(Lv));
            out.println(String.valueOf(Bp));
            out.println(String.valueOf(ifx));
            out.println(String.valueOf(whenx));
    
          }
    
              result.close();
              con.close();
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          if (con != null) {
            try {
              con.close();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
                                        select2.close();
                                        con.close();
    
                                    } else{
                                        System.out.println("Nothing to send");
                                        select2.close();
                                        con.close();
    
                                    }
    
    			}
    
    
                            }
    		}
    		catch(Exception e){
                                e.printStackTrace();
                            }
    	}
    }

  6. #6
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Hmmm I am doing a workaround with LIMIT, OFFSET and COUNT functions in SQL

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cotarelo View Post
    Hmmm I am doing a workaround with LIMIT, OFFSET and COUNT functions in SQL
    It's up to your server how to tell the client the number of lines in the result: it either figures it out in advanve and sends to number of rows before it sends to rows or it sends a <no_more_rows> token to the client when it has send the rows. Of course this token has to be distinguishable from an actual row. Maybe it can send: 1 <row> 1 <row> ... 0. Where '1' means: another row is coming to you and '0' means 'no more rows'.

    kind regards,

    Jos

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default

    Are you opening a Socket for each loop round the resultset?
    Java Code:
    echoSocket = new Socket(PEPhostTextField.getText().toString(),Integer.parseInt(PEPportTextField.getText()));
    OK, you're not doing anything with it at the moment, but that doesn't look right to me.

    Anyway, similar to Jos' suggestion, use a form of xml, where there is a token <rows> that indicates the start of the data, inside which will be your rows, and a token </rows> which indicates they're finished.

    ETA: Just to clarify, prior to the resultset loop you send "<rows>" then after the resultset loop (inside which you send each row) you send a "</rows>".

Similar Threads

  1. add a space after 10 lines?
    By ukemasta in forum New To Java
    Replies: 10
    Last Post: 01-20-2010, 12:12 PM
  2. Help explain the FOR lines please
    By hydride in forum New To Java
    Replies: 2
    Last Post: 01-19-2010, 11:52 PM
  3. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 AM
  4. Random Lines
    By Urgle in forum New To Java
    Replies: 29
    Last Post: 11-12-2008, 03:42 PM
  5. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •