从数据库 table 中获取值,其中 id = ? java
get value from database table where id = ? java
我有一个table
Table name is PARKTABLE
现在我想在 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' />
它在输入文本字段中打印 placeOne 而不是它的值。
我想从数据库中打印 placeOne 值红色或绿色。
我哪里错了?
需要进行三处更改
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
如下更改您的输入文本:
<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);
}
所以你的输入类型可以打印默认值,即使你的查询return没有数据。
Scary 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 注入,就像您在 Statement 上所做的那样,但是当其他程序员看到它时它更具可读性和清洁性。我用 while 在 ResultSet
.
中检索了所有结果
我有一个table Table name is PARKTABLE
现在我想在 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' />
它在输入文本字段中打印 placeOne 而不是它的值。 我想从数据库中打印 placeOne 值红色或绿色。 我哪里错了?
需要进行三处更改
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
如下更改您的输入文本:
<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);
}
所以你的输入类型可以打印默认值,即使你的查询return没有数据。
Scary 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 注入,就像您在 Statement 上所做的那样,但是当其他程序员看到它时它更具可读性和清洁性。我用 while 在 ResultSet
.