留言板(连接数据库)
简单实现留言功能
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>留言板</title> </head> <body> <form action="session0625.jsp" method="post" id="form1" name="form1"> <div align="center"> <table width="50%" border="0"> <tr > <td width="10%">您的联系邮箱:</td> <td width="40%"><input type="text" name="email"/></td> </tr> </table> <table width="50%" border="0" height="40%"> <tr><td>留言内容:</td></tr> <tr><td><input type="text" style="width: 50%;height: 100%" name="liuyan"/></td></tr> <tr><td><input type="submit" value="提交"/></td></tr> </table> </div> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>session</title> </head> <body> <% String email=new String(request.getParameter("email").getBytes("ISO-8859-1"),"UTF-8"); session.setAttribute("email",email); String liuyan=new String(request.getParameter("liuyan").getBytes("ISO-8859-1"),"UTF-8"); session.setAttribute("liuyan",liuyan); %> <form action="result0625.jsp" method="post"> <table> <tr><td>确定添加留言?</td></tr> <tr> <td><input type="submit" value="确定"/></td> <td><input type="reset" value="取消"/></td> </tr> </table> </form> <%-- 您的邮箱地址是: <%=email %> <br> 您的留言内容是: <%=liuyan %> --%> </body> </html>
连接数据库
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>留言板</title> </head> <body> <% String email=(String)session.getAttribute("email"); String liuyan=(String)session.getAttribute("liuyan"); Connection con=null; try { //连接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); String strUrl="jdbc:oracle:thin:@localhost:1521:ORCL"; con=DriverManager.getConnection(strUrl,"test","test");
//添加到数据库 PreparedStatement ps=con.prepareStatement("insert into text values(?,?)"); ps.setString(1, email); ps.setString(2, liuyan); ps.executeUpdate(); out.print("留言添加成功!"+"<br>"); //显示所有留言 Statement st=con.createStatement(); //查询数据库,并遍历显示 ResultSet rs=st.executeQuery("select * from text"); while(rs.next()) { String youxiang=rs.getString("email"); String text=rs.getString("neirong"); out.print("邮箱为"+youxiang+"的留言内容为"+text+"<br>"); } %> <% ps.close(); st.close(); rs.close(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } finally { if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } %> <%-- 您的邮箱地址是: <%=email %> <br> 您的留言内容是: <%=liuyan %> --%> </body> </html>
运行结果: