答案:首先: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开始
希望对你有帮助
其他:String s = request.getParamenter("youdata");
利用jdbc执行语句:update 表 set 列=s 。。。。
上一个:java的前提,是用在jsp进行网站编程居多还是用在开发游戏软件居多?
下一个:JSP定时执行事件