java
  • sql
  • jsp
  • derby
  • 2016-11-16 75 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检索所有结果。

    相关问题