设计程序,提交要添加的学生信息,把信息添加到数据库
插入源码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'insert.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>
<html>
<head><title>提交页面</title></head>
<body>
<form action="lyh.jsp" method="post">
<table border="0" width="238"height="252">
<tr><td>学号</td><td><input type="text" name="id"></td></tr>
<tr><td>姓名</td><td><input type="text" name="name"></td></tr>
<tr><td>姓名</td><td><input type="text" name="sex"></td></tr>
<tr><td>年龄</td><td><input type="text" name="age"></td></tr>
<tr><td>体重</td><td><input type="text" name="weight"></td></tr>
<tr><td>身高</td><td><input type="text" name="hight"></td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="插入">
<input type="reset" value="取消">
</tr>
</table>
</form>
</body>
</html>
判断是否成功:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'lyh.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>
<html><head><title>添加记录</title></head>
<body>
<%
request.setCharacterEncoding("utf-8");
String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPasswd="root";
String dbName="mysql";
String url="jdbc:mysql://localhost:3306/hw?user=root&password=root";
Class.forName(driverName);
Connection conn =DriverManager.getConnection(url);
String sql="Insert into stu_info(id,name,sex,age,weight,height)values(??????)";
PreparedStatement pstmt=coon.Preparedstatement(sql);
request.setCharacterEncoding("utf-8");
int id=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
String sex=request.getParameter("sex");
int age=Integer.parseInt(request.getParameter("age"));
float weight=Float.parseFloat(request.getParameter("weight"));
float hight=Float.parseFloat(request.getParameter("hight"));
pstmt.setint(1,id);
pstmt.setString(2,name);
pstmt.setString(3,sex);
pstmt.setInt(4,age);
pstmt.setfloat(5,weight);
pstmt.setfloat(6,hight);
int n=pstmt.executeUpdate();
if(n==1){%>数据插入操作成功<br><%}
else{%>数据插入操作失败<br><%}
if(pstmt!=null){pstmt.close();}
if(coon!=null){coon.close();} %>
</body>
</html>