package project1;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class lyb extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>lyb</title></head>");
out.println("<body>");
request.setCharacterEncoding("GBK");
String dbUrl="jdbc:odbc:notes";
Connection conn;
Statement stmt;
try{
//装载JDBC驱动程序;
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.registerDriver(new JdbcOdbcDriver());
////以上两个语句可以互换////
//创建一个数据库连接;
conn=DriverManager.getConnection(dbUrl);
stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql="select * from liuyan";
ResultSet rs=stmt.executeQuery(sql);
int RecCnt=rs.getRow();
rs.absolute(RecCnt-20);
//执行SQL语句
//组装标准的SQL语句
out.print("<p align=center><font color=blue size=5>用户留言</font></p>");
out.print("<table border=0 align=center width=80%>");
//out.print("<tr>");
//out.print("<td>用户名</td><td>留言信息</td><td>时间</td>");
//out.print("</tr>");
while (rs.next()) {
out.println("<tr>");
out.print("<td>");
out.print(rs.getString("username"));
out.print("说:");
out.print("<td>");
out.print(rs.getString("message"));
out.print("<td>");
out.print(rs.getString("s_time"));
out.print("</td>");
out.print("</tr>");
//out.print("<br>");
//out.print("<hr width=90%>");
}
out.print("</table>");
out.print("<hr color=red width=100%>") ;
// 回收数据库连接资源
rs.close();
stmt.close();
conn.close();
}catch (Exception ex) {
out.print("<p align=center><font size=5 color=red>数据库访问出错!</font></p>"+ex.getMessage());
ex.printStackTrace();
}
out.print("<form name=\"form1\" method=\"post\">\n" +
" <p>\n" +
" \n" +
" </p>\n" +
" <p align=\"center\">\n" +
" <font size=\"6\"\n" +
" color=\"#ff4242\">\n" +
" <strong> 留言板</strong>\n" +
" </font>\n" +
" </p>\n" +
" <p align=\"center\">\n" +
" 用户名:<input type=\"text\"\n" +
" name=\"username\"\n" +
" maxlength=\"20\"\n" +
" size=\"10\"/>\n" +
" </p>\n" +
" <p align=\"center\">\n" +
" 留言内容:<textarea name=\"message\"\n" +
" cols=\"30\"\n" +
" rows=\"10\"></textarea>\n" +
" </p>\n" +
" <p align=\"center\">\n" +
" \n" +
" <input type=\"submit\" name=\"submit\" value=\"留言\"/><input type=\"reset\"\n" +
" name=\"reset\"\n" +
" value=\"重新填写\"/>\n" +
" </p>\n" +
" <p>\n" +
" \n" +
" </p>\n" +
" </form>");
//out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>lyb</title></head>");
out.println("<body>");
request.setCharacterEncoding("GBK");
//获取客户端输入的朋友的信息。
String username,message;
username=request.getParameter("username");
message=request.getParameter("message");
//frd_tele=request.getParameter("frd_tele");
//frd_email=request.getParameter("frd_email");
String dbUrl="jdbc:odbc:notes"; //addressbook是odbc数据源名称
Connection conn;
PreparedStatement pst;
try{
//装载JDBC驱动程序;
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.registerDriver(new JdbcOdbcDriver());
////以上两个语句可以互换////
//创建一个数据库连接;
conn=DriverManager.getConnection(dbUrl);
//组装标准的SQL语句
pst=conn.prepareStatement("insert into liuyan (username,message,s_time) values (?,?,?)");
pst.setString(1,username);
pst.setString(2,message);
// pst.setString(3,frd_tele);
// pst.setString(4,frd_email);
java.util.Date Date=new Date();
java.sql.Timestamp s_time=new java.sql.Timestamp(Date.getTime());
pst.setTimestamp(3,s_time);
//执行SQL语句
pst.execute();
//out.print("<p align=center><font size=5>留言添加成功!</font></p>");
// 回收�