java
  • sql
  • jsp
  • derby
  • 2016-11-16 74 views 0 likes 
    0

    我有一個表 Table name is PARKTABLE從數據庫表中獲取值,其中id =? java的

    現在我想在jsp中對這些值 我的代碼是在這裏

    <jsp:useBean id="loginBean" scope="session" class="vustudent.Login" /> 
         <input type="text" name="takei" value='<jsp:getProperty name="loginBean" property="loginid" />' /> 
         <% 
          String dbId = request.getParameter("takei"); 
          Class.forName("org.apache.derby.jdbc.ClientDriver"); 
          String url= "jdbc:derby://localhost:1527/sample;create=true; user=app; password=app"; 
          Connection con = DriverManager.getConnection(url); 
          Statement st= con.createStatement(); 
          String query = "SELECT * FROM PARKTABLE WHERE ID =\'"+ dbId + "\' "; 
          ResultSet rs = st.executeQuery(query); 
          // iterate through the java resultset 
         if (rs.next()) 
         { 
         String placeOne = rs.getString("Place1"); 
         String placeTwo = rs.getString("Place2"); 
         System.out.println("place1" +placeOne); 
         System.out.println("place1" +placeTwo); 
         } 
         %> 
         </br> 
         <input type="text" name="pl1value" value='placeOne' /> 
    

    在其價值的輸入文本字段istead打印placeOne。 我想從數據庫打印位置值紅色或綠色。 我錯了?

    回答

    2

    更改您的輸入如下文字:

    <input type="text" name="pl1value" value='<%=placeOne%>' /> 
    

    ,然後更改如下您Java代碼:

    String placeOne = ""; 
    String placeTwo = ""; 
    
    if (rs.next()) 
         { 
         placeOne = rs.getString("Place1"); 
         placeTwo = rs.getString("Place2"); 
         System.out.println("place1" +placeOne); 
         System.out.println("place1" +placeTwo); 
         } 
    

    所以你的輸入類型可以打印默認值,即使您的查詢不返回任何數據。

    +0

    我認爲它應該是工作。但我沒有聲明id =主鍵。可能是由於這個問題。這可能是原因或不? –

    +0

    獲取您的字符串查詢值並在數據庫中運行,然後讓我知道查詢是否正常。但它與主鍵無關。這是可選的,但是在一張表中有一個pk是好的 –

    +0

    當我寫 字符串查詢=「選擇*從PARKTABLE WHERE ID ='126678'」; 其工作正常,並在輸入字段中獲得紅色值,但當我寫 字符串查詢=「選擇*從PARKTABLE WHERE ID ='dbId'」; 它返回空的輸入框。 –

    2

    三個轉變是必要的

    String placeOne = null; // declare here 
        String placeTwo = null; 
        if (rs.next()) 
        { 
        placeOne = rs.getString("Place1"); // set value here 
        placeTwo = rs.getString("Place2"); 
        System.out.println("place1" +placeOne); 
        System.out.println("place1" +placeTwo); 
        } 
        %> 
        </br> 
        <input type="text" name="pl1value" value=''<%=placeOne%>' /> // print here 
    
    +0

    不, 它不能正常工作 現在p1value = null –

    +0

    @FatimaEman,是的,它的工作!但是你需要了解你的程序邏輯,並且你需要確保你的查詢是否正確。 –

    +0

    我沒有使id =主鍵。這可能是原因? –

    0

    由於可怕的Wombat和Ye Win的帖子,他們在邏輯上是正確的。如果我是你,我實際上會使用PreparedStatement而不是Statement。我將使用while而不是if。看到我的代碼如下。

     String dbId = request.getParameter("takei"); 
         Class.forName("org.apache.derby.jdbc.ClientDriver"); 
         String url= "jdbc:derby://localhost:1527/sample;create=true; user=app; password=app"; 
         Connection con = DriverManager.getConnection(url); 
         String query = "SELECT * FROM PARKTABLE WHERE ID = ?"; 
         PreparedStatement st= con.prepareStatement(query); 
         st.setString(0,dbId); 
         ResultSet rs = st.executeQuery(); 
    
         String placeOne = null; 
         String placeTwo = null; 
    
         while (rs.next()) { 
          String placeOne = rs.getString("Place1"); 
          String placeTwo = rs.getString("Place2"); 
          System.out.println("place1" +placeOne); 
          System.out.println("place1" +placeTwo); 
         } 
    

    我利用PreparedStatement防止SQL注入是你一個人在你的聲明,但是當其他程序員看到它,它是更具可讀性和清潔。我一邊用ResultSet檢索所有結果。

    相關問題