info.jsp
<%@ page pageEncoding="utf-8"%>
<!-- 2. 制作一个表单,提交部门编号,部门名称,部门位置,
表单提交给a.jsp a.jsp中获取到表单提交的3项数据,入库!(记着解决中文乱码)。 -->
<form action="a.jsp" method="post">
<table boder="1" >
<tr>
<td>部门编号:</td>
<td><input name="deptno"/></td>
</tr>
<tr>
<td>部门名称:</td>
<td><input name="dname"/></td>
</tr>
<tr>
<td>部门位置:</td>
<td><input name="dlocation"/></td>
</tr>
</table>
<button type="submit">提交</button>
</form>
a.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String dlocation = request.getParameter("dlocation");
%>
<%-- <% String dname = %><%=request.getParameter("dname") %><br/>
<% String dlocation = %><%=request.getParameter("dlocation") %><br/> --%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn=DriverManager.getConnection(url,"scott","ORCL");
String sql = "insert into dept values(?,?,?)";
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setInt(1,Integer.valueOf(deptno));
stmt.setString(2,dname);
stmt.setString(3,dlocation);
int rowcount=stmt.executeUpdate();
out.print("影响的行数:"+rowcount);
stmt.close();
conn.close();
%>
<button type="submit" οnclick="f1()">查询数据库的所有部门</button>
<script>
function f1(){
location="deptinfo.jsp";
}
</script>
deptinfo.jsp:
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page pageEncoding="utf-8"%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn=DriverManager.getConnection(url,"scott","ORCL");
PreparedStatement stmt=conn.prepareStatement("select * from dept");
ResultSet rs=stmt.executeQuery();
out.print("<TABLE BORDER='1' width='55%' cellpadding='3' align=center>");
out.print("<TR><TD>"+"<b>部门编号</b>"+"</TD>");
out.print("<TD>"+"<b>部门名称</b>"+"</TD>");
out.print("<TD>"+"<b>部门位置</b>"+"</TD>");
while(rs.next()){
out.print("<TR><TD>"+rs.getInt(1)+"</TD>");
out.print("<TD>"+rs.getString(2)+"</TD>");
out.print("<TD>"+rs.getString(3)+"</TD></TR>");
}
out.print("</TABLE>");
rs.close();
stmt.close();
conn.close();
%>