jsp页面表单的数据怎么提交到后台的oracle数据库,并用servelet接受数据??有例子更好

 我来答
浅颜淡笑1
2011-07-12
知道答主
回答量:28
采纳率:0%
帮助的人:23.3万
展开全部
首先:jsp的form表单
<form action="servlet/Student" method="post">
<table>
<tr>
<td>课程名称:</td>
<td><input type="text" name="courseName"/><span style="color:red;">*</span><html:errors property="userName"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>

其次:servlet 的内容:
public class Student extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过request.getParameter("name属性值");来获取表单中的值
Connection conn=null;
String userName =request.getParameter("userName");
try {
Class.forName("com.mysql.jdbc.Driver");//注册驱动

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/t59", "aa", "aa");//参数分别为url username password

PreparedStatement pStatement =conn.prepareStatement("insert into course values (null,?)");
pStatement.setString(1, userName);

pStatement.executeUpdate();//执行增删改时用
//对于查询用ResultSet rSet=pStatement.executeQuery();;接着进行遍历
} catch (Exception e) {
e.printStackTrace();
}

}

}

注意:
1.form表单中的action值:是根据web.xml里面的来确定
2.获取表单中的值采用:request.getParameter("name属性值");
3.要加数据库驱动包
4.该例子是采用mysql做的
5,sql语句中的占位符“?”,设置值时索引从1开始

希望对你有帮助
xcg9593
2011-07-12 · TA获得超过280个赞
知道小有建树答主
回答量:533
采纳率:0%
帮助的人:270万
展开全部
String s = request.getParamenter("youdata");
利用jdbc执行语句:update 表 set 列=s 。。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式