2013-10-03 1 views
0

jdbc 연결을 사용하여 데이터베이스의 제품 세부 사항을 저장하는 간단한 테이블을 만들고 있습니다. 내가 (단일 행 내에서 즉, 여러 열) 특정 열에서 각 제품의 상세 정보를 얻고 지금jdbc를 사용하여 테이블에서 열을 나누는 방법

<table> 

    <td> 

    <img border="3" 
     src="image path" height="200" width="200" /> 

     <p align="center"></p> // these tag contains specific product details from db 
     <p align="center"></p> 

     </td> 
     </table> 

을하지만, 내 문제는이 행, & 확대에 계속 있다는 것입니다 :

내 표는 다음과 같습니다 이 행 (여러 열로 구성)이 자동으로 5 개의 각 제품마다 다음과 같이 말하도록합니다. 난 단지 flipkart처럼 내 제품 4 XN 형식의 세부 사항을 마련 할 간단한 단어에서

아래와 같이

http://www.flipkart.com/mobiles/android-phones~type/pr?sid=tyy,4io&otracker=hp_nmenu_sub_electronics_0_Android%20phones

하면 다음 다음 LG 첫 번째 제품 소니 experia을 볼 수 있습니다 삼성 갤럭시 제품 후 , 새로운 행은 다시 4 제품을 시작한 다음 새로운 행을 시작합니다. 이 방법으로 jdbc 연결을 사용하여 데이터베이스에서 오는 내 제품을 표시해야합니다.

누구나 내게 이것이 가능한 최선의 접근 방법을 제안 할 수 있습니까? & 4 번째 열 이후에 열을 끊을 수있는 옵션이 있습니까? & 다음 새 행을 시작 하시겠습니까? 아래 그림과 같이

난 그냥 데모, JSP에서 데이터베이스 내용을 표시하고있다 :

 <% 
     //1. Retrieve all products from database 

     //1a. Load the Driver 
     Class.forName("com.mysql.jdbc.Driver"); 
     //1b. Get a connection to the database 
     Connection con = DriverManager.getConnection("url", "un", "pwd 
     PreparedStatement ps = con.prepareStatement("SELECT * FROM table"); 
     //1d. Execute and retrieve our result 
     ResultSet rs = ps.executeQuery(); 

     //2. Base on the results returned, construct a table 
    %> 
    </p> 

    <table border="0">  
    <%  

    if(rs.next()) {   
    rs.beforeFirst(); // for making sure you dont miss the first record.  
     while(rs.next())  
      {      // opening while loop brackets.  

    %>   



     <td> 
    <div style=""><img border="3" 
     src="<%=rs.getString("image") %>" height="200" width="200" /></div> 


     <p align="center"><%=rs.getString("title")%></p> 
     <p align="center"><%=rs.getString("price")%></p> 

     </td> 


     <%   
      } //closing while loop bracket  
     }   
     else {   
      //if no record is found, simply display a no record message   
    %>   
     Nothing.   
     <%   
     }   
     %>  

     </table>  
+0

''와'을'여기에 도움이 될 수 있습니다. – GriffeyDog

+0

액체 레이아웃의 경우 테이블 대신 div를 사용합니다. 화면 너비에 따라 다음 행으로 이동합니다. – AC1

+0

@ AC1 나는 액체 레이아웃을 위해 테이블 ​​대신 div를 사용했지만 제품은 다른 제품보다 아래에 하나씩옵니다. 나는 다음에 무엇을 사용해야합니까? – puneetjava

답변

1

사용 <tr> 태그를 행 테이블을 구성 할 수 있습니다. (그게 바로 그 의미입니다.)

데이터베이스에서 읽은 제품 (N)의 수를 기록하십시오. N이 4의 배수 (n % 4 == 0)이면 <tr>을 닫고 새 것을 시작하십시오.

그래서 결과가 보이는 같은 : 업데이트 된 질문에 따라

<table> 
    <tr> 
    <td>...</td> 
    <td>...</td> 
    <td>...</td> 
    <td>...</td> 
    </tr> 
    <tr> 
    <td>...</td> 
    <td>...</td> 
    <td>...</td> 
    <td>...</td> 
    </tr> 
</table> 

UPDATE :

가 여기에 귀하의 코드를 기반으로 내 비틀기입니다. 정말 JSP 내에서 SQL 호출 (또는 실제로 모든 비 프레젠테이션 계층 코드)을 주장하지는 않지만 다른 질문에 대한 문제입니다.

<% 
    //1. Retrieve all products from database 

    //1a. Load the Driver 
    Class.forName("com.mysql.jdbc.Driver"); 
    //1b. Get a connection to the database 
    Connection con = DriverManager.getConnection("url", "un", "pwd 
    PreparedStatement ps = con.prepareStatement("SELECT * FROM table"); 
    //1d. Execute and retrieve our result 
    ResultSet rs = ps.executeQuery(); 

    //2. Base on the results returned, construct a table 
%> 
</p> 

<table border="0"> 
    <tr> 
    <%  

    int i = 0; 
    if(rs.next()) { 
    rs.beforeFirst(); // for making sure you dont miss the first record.  
    while(rs.next())  
     {      // opening while loop brackets. 
    %> 

    <td> 
     <div style=""><img border="3" 
      src="<%=rs.getString("image") %>" height="200" width="200" /></div> 


     <p align="center"><%=rs.getString("title")%></p> 
     <p align="center"><%=rs.getString("price")%></p> 

    </td> 

    <% 
     i++; 
     if ((i % 4) == 0) { 
    %> 
     </tr> 
    <% 
     } 

     if (!rs.isLast()) { // don't open a new row if this is the last result 
    %> 
     <tr> 
    <% 
     } // closing isLast check 
    %> 

    <%   
     } //closing while loop bracket  
    }   
    else {   
     //if no record is found, simply display a no record message   
%>   
    Nothing.   
    <%   
    }   
    %>  
    </tr> 

    </table> 

는 정말 여기 whitespacing를 해결하기 위해 인내심을 가지고 있지 않으며, 실제로이 컴파일하지 않은,하지만 희망 당신은 요점을 얻을.

+0

어쨌든 위의 테이블 구조에 따라 while 루프를 넣으면 4 열과 n 행을 얻을 수 있습니다. – puneetjava

+0

제공된 표 구조는 출력입니다. 각 ''은 테이블 행입니다. 각''에''을 넣고 네 개의 열과 N 개의 행이 있습니다. 루프 *를 테이블에 넣지 마십시오. 위의 구조를 생성하기 위해 코드에서 루프를 만듭니다. 현재 테이블을 생성하는 현재 코드는 무엇입니까? 질문을 업데이트하면 내 대답이 업데이트됩니다. – dcsohl

+0

제 업데이트 된 질문에 대해 궁금해하고 싶습니다. 결국에는 (데모 용) 내 코드를 언급했습니다. 가능하다면 가능한 해결 방법을 알려주십시오. – puneetjava

1

dcsohl의 응답 외에도 <div><span> 요소를 조합하여 사용할 수 있습니다. A <div> 요소는 블록 요소입니다. 즉, 각 <div>이 새 행으로 렌더링됩니다. <span> 요소는 인라인 요소입니다.

이 당신을 도움이 될 것입니다

<div> 
    <span></span> 
    <span></span> 
    <span></span> 
    <span></span> 
    <span></span> 
</div> 
<div> 
    <span></span> 
    <span></span> 
    <span></span> 
    <span></span> 
    <span></span> 
</div> 
<div> 
    <span></span> 
    <span></span> 
    <span></span> 
    <span></span> 
    <span></span> 
</div> 
관련 문제