index.jsp 输入内容界面
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY bgcolor=pink >
<Font size=4>
<P>给我留言:
<FORM action="hu.jsp" method=post>
姓名:
<Input type="text" name="number">
<BR>QQ:
<Input type="text" name="name">
<BR>留言内容:
<textarea rows=3 name=math cols=20>kkk</textarea>
<BR><Input type="submit" name="b" value="提交留言">
</Font>
</BODY>
</HTML>
hu.jsp添加内容
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY bgcolor=pink ><Font size=4>
<% //获取提交的学号:
String number=request.getParameter("number");
if(number==null)
{number="";
}
byte b[]=number.getBytes("ISO-8859-1");
number=new String(b);
//获取提交的姓名:
String name=request.getParameter("name");
if(name==null)
{name="";
}
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
//获取提交的新的数学成绩:
String m=request.getParameter("math");
if(m==null)
{m=""; }
byte d[]=name.getBytes("ISO-8859-1");
name=new String(d);
try
{
String dbpath = application.getRealPath("hufan.mdb");//转化成物理路径
String dbname = "";
String user = "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate("INSERT INTO hufan (xingming,nianling,shuxue) VALUES"+"("+"'"+number+"',"+"'"+name+"',"+"'"+m+"'"+")"); //
out.print("添加新记录成功");
out.print("<a href=hu2.jsp target=_blank>查看留言</a>");
conn.close();
}
catch(SQLException event)
{ out.print("添加新记录错误");}
%>
</FONT>
</BODY>
</HTML>
hu2.jsp显示留言界面
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY bgcolor=pink ><Font size=4>
<%
try
{
String dbpath = application.getRealPath("hufan.mdb");//转化成物理路径
String dbname = "";
String user = "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//显示添加新记录后表中的记录:
%>
<P>添加新记录后的表:
<%
ResultSet rs=stmt.executeQuery("SELECT * FROM hufan");
while(rs.next())
{
out.print("姓名:"+rs.getString("xingming")+"<br>");
out.print("QQ:"+rs.getString("nianling")+"<br>");
out.print("内容:"+rs.getString("shuxue")+"<br>");
out.print("<hr>");
}
conn.close();
}
catch(SQLException event)
{ }
%>
</FONT>
</BODY>
</HTML>