查看:1679|回复:0
JSP与SQL SERVER的留言本发表时间:2010-04-29 13:35:42

大虾

0分享0主题0帖子

学徒一级

(初出茅庐)

 

<%@ page import="java.sql.*" %>





add message into table





<%

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)

{}

%>



<%

}

%>











message board



















访客留言板












































姓名:
E-mail:
主题:
留言:












查看留言










message board



















访客留言板












































姓名:
E-mail:
主题:
留言:












查看留言








<%@ page import="java.sql.*" %>





show the message in the table





所有访客留言






<%

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url="jdbc:odbc:user_db";

Connection con=DriverManager.getConnection(url,"insher","insher");

Statement stm=con.createStatement();

ResultSet result=stm.executeQuery("select count(*) from message");

int message_count=0;

if(result.next())

{

message_count=result.getInt(1);

result.close();

}

if(message_count>0)

{

result=stm.executeQuery("select * from message order by time desc");

while(result.next())

{

String title=result.getString("title");

String name=result.getString("name");

String mail=result.getString("mail");

Timestamp time=result.getTimestamp("time");

Date date_time=new Date(time.getTime());

Time time_time=new Time(time.getTime());

String content=result.getString("content");

%>



















主题: <%=title%>
留言人: <%=name%>E-mail:

<%

out.println(""+mail+"");

%>

留言时间:

<%

out.println(""+date_time+" "+time_time+"");

%>



<%

out.println("("+message_count+")");

%>

<%=content%>



<%

out.println("
");

message_count--;

}

result.close();

con.close();

}

else

{

out.println("目前还没有任何留言!");

con.close();

}

} catch(Exception e)

{out.println(e);}

%>

我要留言









<%@ page import="java.sql.*" %>





show the message in the table





所有访客留言






<%

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url="jdbc:odbc:user_db";

Connection con=DriverManager.getConnection(url,"insher","insher");

Statement stm=con.createStatement();

ResultSet result=stm.executeQuery("select count(*) from message");

int message_count=0;

if(result.next())

{

message_count=result.getInt(1);

result.close();

}

if(message_count>0)

{

result=stm.executeQuery("select * from message order by time desc");

while(result.next())

{

String title=result.getString("title");

String name=result.getString("name");

String mail=result.getString("mail");

Timestamp time=result.getTimestamp("time");

Date date_time=new Date(time.getTime());

Time time_time=new Time(time.getTime());

String content=result.getString("content");

%>



















主题: <%=title%>
留言人: <%=name%>E-mail:

<%

out.println(""+mail+"");

%>

留言时间:

<%

out.println(""+date_time+time_time+"");

%>



<%

out.println("("+message_count+")");

%>

<%=content%>



<%

out.println("
");

message_count--;

}

result.close();

con.close();

}

else

{

out.println("目前还没有任何留言!");

con.close();

}

} catch(Exception e)

{out.println(e);}

%>

我要留言






您需要登录以后才可以回帖    登录|注册