JSP不会将我的数据插入数据库

时间:2010-06-09 12:40:22

标签: jsp jdbc

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.sql.Blob"%>
<%@ page import="java.sql.PreparedStatement"%>
<%@ page import="java.sql.BatchUpdateException"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%> 

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'p.jsp' starting page</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>

  <body>
    <% 

int activityId = Integer.parseInt(request.getParameter("activityId"));
String name = request.getParameter("name");
String activityType = request.getParameter("activityType");
int parentId = Integer.parseInt(request.getParameter("parentId"));
String description = request.getParameter("description");
%>
 <%
Connection conn=null;
PreparedStatement pstatement = null;
 try{
 Class.forName("com.mysql.jdbc.Driver");
 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/pol","root","1234");
 Statement st=conn.createStatement();

String queryString = "INSERT INTO activity(activityId,name,activityType,parentId,description)" + " VALUES (?, ?, ?, ?, ?)";

 pstatement = conn.prepareStatement(queryString);

pstatement.setInt(1, activityId);
pstatement.setString(2, name);
pstatement.setString(3, activityType);
pstatement.setInt(4, parentId);
pstatement.setString(5, description);

 }
catch (Exception ex) {
   out.println("Unable to connect to batabase.");
}

finally {
pstatement.close();
conn.close();

}

%>

  </body>
</html>

2 个答案:

答案 0 :(得分:2)

你永远不会打电话给pstatement.executeUpdate();

答案 1 :(得分:0)

您缺少执行更新:ps.executeUpdate();