简体   繁体   中英

Have trouble in fetching the required data from DB

I have a table like this.

AreaId  LineId  ShapePointno    x   y
1             0    0            2   3
1             0    1            2   5
1             0    2            3   8
1             1    0            2   6
1             1    1            3   2

Before, getting into the problem, I would explain on the fields. A line Id 0 belonging to AreaId1 1 has 3 shape points numbered as 0,1 and 2. Its x and y corresponds to its co-ordinate positions.

With this background, let me put the problem in hand.

I need to fetch these records and bring it to java .

While bringing in, I need to have it as a block of shape points for a line. (ie) 1st 3 rows as one block and next 2 in a different block. The reason behind this is I need to modify x & y values and the modification function expects the following parameters.

  i)AreaID, LineId, A 2D array of its shape points).

Here is what I have tried out.

 Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/task4DB?"
              + "user=root&password=123");
    PreparedStatement st = con.prepareStatement("select Distinct AreaId from TableName")
    ResultSet r1=st.executeQuery();
   while (r1.next())
    {
    int id = r1.getInt("AreaId");
    PreparedStatement st1 = con.prepareStatement("select Distinct LineId where AreaId = id")
    ResultSet r2=st1.executeQuery();
    while (r2.next())
    {
      int lineid = r2.getInt("LineId");
      PreparedStatement st2 = con.prepareStatement("select x,y  where AreaId = id and LineId = lineid")
      ResultSet r3=st2.executeQuery();
      int size = 0;
      int [][]xy = new int[r3.getCount()][r3.getCount()]
       while (r3.next())
      {

       xy[size][0] = r3.getInt("x");
       xy[size][1] = r3.getInt("y");
       size++;
      }
     callShapePointConv(id,lineId,xy);
    }
 }

I don't get the expected results rather it shows junk values/

Please help me out.

Also, is there any other better alternate available rather than three while loops?

Kindly Help me out. Thanks in advance!

I found it - At line number 14 you are executing st1.executeQuery() but it should be st2.execute query

I have commented your line and added the correct one.

Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/task4DB?"
              + "user=root&password=123");
    PreparedStatement st = con.prepareStatement("select Distinct AreaId from TableName")
    ResultSet r1=st.executeQuery();
   while (r1.next())
{
int id = r1.getInt("AreaId");
 PreparedStatement st1 = con.prepareStatement("select Distinct LineId where AreaId = id")
 ResultSet r2=st1.executeQuery();
 while (r2.next())
{
 int lineid = r2.getInt("LineId");
 PreparedStatement st2 = con.prepareStatement("select x,y  where AreaId = id and LineId = lineid")
 //ResultSet r3=st1.executeQuery();
  ResultSet r3 = st2.executeQuery();
int size = 0;
int [][]xy = new int[r3.getCount()][r3.getCount()]
 while (r3.next())
{

 xy[size][0] = r3.getInt("x");
 xy[size][1] = r3.getInt("y");
 size++;
}
callShapePointConv(id,lineId,xy);
}
}

Apart from the Java code, You can reduce complexity of database design in following manner,

Table_1:

AreaLineId(FK) ShapePointno   x     y
    0              0          2     3
    0              1          2     5
    0              2          3     8
    1              0          2     6
    1              1          3     2

Table_2 :

AreaLineId   AreaId   LineId    
   0           1        0    
   1           1        1       

Now query will be,

select x,y from table_1 where AreaLineId = id;

and another select wapper query to get id from table_2 will do the job with less complexity.

I'm just quessing here but your selects are flawed in many ways.

select Distinct LineId where AreaId = id

First, where is you FROM Table declaration. Second: Did you want to fetch the distinct list of LineId based on the previously retrieved id?

If so, your parameterized select should look like this:

select Distinct LineId from table_2 where AreaId = ?

But what you really should look up is how to use joins in select statements.

Update

You code does not make sense any more.

First you get the unique AreaIds. --> 1

Then you get the unique LineIds --> 0,1

Then you select all elements from the table, where

(1) while: AreaId = 1 
   and L
   (2) while:
          (loop 1) lineId = 0 
          (loop 2) lineId = 1

This will ultimately return the table content :)

Maybe, this can help you:

This will group the x and y, based on the areaId and lineId as lists of values.

SELECT
    AreaId,
    LineId,
    GROUP_CONCAT(table.x),
    GROUP_CONCAT(table.y)
FROM
    `table`
GROUP BY
    AreaId,
    LineId
ORDER BY
    table.ShapePointno

Result:

AreaId LineId GROUP_CONCAT(table.x)  GROUP_CONCAT(table.y)
1      0      "2,2,3"                "3,5,8"
1      1      "2,3"                  "6,2"

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