<!--add_message.jsp--> <%@ page import="java.sql.*" %> <HTML> <HEAD> <TITLE>add message into table </TITLE> </HEAD> <BODY> <% String name=request.getParameter("name"); String mail=request.getParameter("mail"); String title=request.getParameter("title"); String content=request.getParameter("content"); if(name==null) name=""; if(title==null) title=""; if(content==null) content=""; if(mail==null) mail=""; if(name.length()==0) out.println("留言人姓名不能为空!"); else if(title.length()==0) out.println("留言主题不能为空!"); else if(content.length()==0) out.println("留言内容不能为空!"); else { java.util.Date date=new java.util.Date(); String datetime=new Timestamp(date.getTime()).toString(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:user_db"; Connection con=DriverManager.getConnection(url,"insher","insher"); PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)"); stm.setString(1,title); stm.setString(2,name); if(mail.length()==0) stm.setString(3,null); else stm.setString(3,mail); stm.setString(4,datetime); stm.setString(5,content); try { stm.executeQuery(); } catch(Exception e) {} con.close(); } catch (Exception e) {} %> <jsp:forward page="view_message.jsp" /> <% } %> </body> </html>
<!--add_message.jsp-- <%@ page import="java.sql.*" %> <HTML> <HEAD> <TITLE>add message into table </TITLE> </HEAD> <BODY> <% String name=request.getParameter("name"); String mail=request.getParameter("mail"); String title=request.getParameter("title"); String content=request.getParameter("content"); if(name==null) name=""; if(title==null) title=""; if(content==null) content=""; if(mail==null) mail=""; if(name.length()==0) out.println("留言人姓名不能为空!"); else if(title.length()==0) out.println("留言主题不能为空!"); else if(content.length()==0) out.println("留言内容不能为空!"); else { java.util.Date date=new java.util.Date(); String datetime=new Timestamp(date.getTime()).toString();
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:user_db"; Connection con=DriverManager.getConnection(url,"insher","insher"); PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)"); stm.setString(1,title); stm.setString(2,name); if(mail.length()==0) stm.setString(3,null); else stm.setString(3,mail); stm.setString(4,datetime); stm.setString(5,content); try { stm.executeQuery(); } catch(Exception e) {} con.close(); } catch (Exception e) {} %> <jsp:forward page="view_message.jsp" /> |