目前共有4篇帖子。 內容轉換:不轉換▼
 
點擊 回復
564 3
JSP原生支持Prepared Statement查詢
一派護法 十九級
1樓 發表于:2015-6-12 13:08
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %><%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "php", "密碼");
if (request.getMethod() == "POST") {
    String name = request.getParameter("ItemName");
    String description = request.getParameter("ItemDescription");
    String price = request.getParameter("ItemPrice");
    
    String sql = "INSERT INTO Items VALUES (NULL, ?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, name);
    stmt.setString(2, description);
    stmt.setString(3, price);
    stmt.executeUpdate();
    response.sendRedirect("items.jsp");
}
%><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Items</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.min.js"></script>
<script>
function checkForm() {
    if ($("#ItemName").val() == "") {
        $("#ItemName").focus();
        return false;
    } else if ($("#ItemDescription").val() == "") {
        $("#ItemDescription").focus();
        return false;
    } else {
        return true;
    }
}
</script>
</head>

<body>
<%
String sql = "SELECT * FROM Items ORDER BY ItemID";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
%>
<ul class="Items">
  <% while (rs.next()) { %><li>
    <h3><%=rs.getString("ItemName")%></h3>
    <p><%=rs.getString("ItemDescription")%></p>
    <p>Price: <%=rs.getString("ItemPrice")%></p>
  </li><%
  }
  stmt.close();
  rs.close();
  %>
</ul>
<form name="NewItem" method="post" action="" onSubmit="return checkForm()">
  <p>
    <label for="ItemName">Name: </label><br>
    <input type="text" name="ItemName" id="ItemName">
  </p>
  <p>
    <label for="ItemDescription">Description:</label><br>
    <textarea name="ItemDescription" rows="4" id="ItemDescription"></textarea>
  </p>
  <p>
    <label for="ItemPrice">Price: </label><br>
    <input name="ItemPrice" type="text" id="ItemPrice" value="10.0">
  </p>
  <p><input type="submit" name="button" id="button" value="Submit"></p>
</form>
</body>
</html>
<%
conn.close();
%>
一派護法 十九級
2樓 發表于:2015-6-12 13:10
數據表:
CREATE TABLE `Items` (
 `ItemID` int(11) NOT NULL AUTO_INCREMENT,
 `ItemName` varchar(50) NOT NULL,
 `ItemDescription` varchar(200) NOT NULL,
 `ItemPrice` float NOT NULL,
 PRIMARY KEY (`ItemID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
一派護法 十九級
3樓 發表于:2015-6-12 15:21
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Items</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.min.js"></script>
<script>
function checkForm() {
    if ($("#ItemName").val() == "") {
        $("#ItemName").focus();
        return false;
    } else if ($("#ItemDescription").val() == "") {
        $("#ItemDescription").focus();
        return false;
    } else {
        return true;
    }
}
</script>
</head>

<body>

<ul class="Items">
  <li>
    <h3>Android Development</h3>
    <p>It's about Android.</p>
    <p>Price: 45.3</p>
  </li><li>
    <h3>Arslanbar</h3>
    <p>A good website</p>
    <p>Price: 10</p>
  </li><li>
    <h3>Purasbar</h3>
    <p>A deprecated website</p>
    <p>Price: 43.8</p>
  </li>
</ul>
<form name="NewItem" method="post" action="" onSubmit="return checkForm()">
  <p>
    <label for="ItemName">Name: </label><br>
    <input type="text" name="ItemName" id="ItemName">
  </p>
  <p>
    <label for="ItemDescription">Description:</label><br>
    <textarea name="ItemDescription" rows="4" id="ItemDescription"></textarea>
  </p>
  <p>
    <label for="ItemPrice">Price: </label><br>
    <input name="ItemPrice" type="text" id="ItemPrice" value="10.0">
  </p>
  <p><input type="submit" name="button" id="button" value="Submit"></p>
</form>
</body>
</html>

一派護法 十九級
4樓 發表于:2016-9-9 23:04
stmt.executeUpdate();后面最好加上stmt.close();

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:564 回複數:3
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-9-9 23:04
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。