import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.sql.*;
@WebServlet(name="TinyBBS",urlPatterns={"/TinyBBS"})
public class TinyBBS extends HttpServlet {
String host="localhost";
String db = "nittaDB";
String user = "nitta";
String pass = "PasswordOfNitta";
String tbl = "article";
String url=null;
Connection con=null;
Statement st=null;
String selectSql = "insert into "+tbl
+" (cdate,title,uname,contents) values(now(),?,?,?)";
PreparedStatement pst=null;
String insertSql = "insert into "+tbl
+" (cdate,title,uname,contents) values(now(),?,?,?)";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out= response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<html><body><h2>TinyBBS</h2>");
try {
Class.forName("org.mariadb.jdbc.Driver");
url = "jdbc:mysql://"+host+"/"+db
+"?useUnicode=true&characterEncoding=utf8";
con = DriverManager.getConnection(url,user,pass);
st = con.createStatement();
pst = con.prepareStatement(insertSql);
} catch (Exception e) {
e.printStackTrace();
try{
Thread.sleep(60000);
} catch(InterruptedException ee) {}
System.exit(1);
}
String titleParam = request.getParameter("title");
String unameParam = request.getParameter("uname");
String contentsParam = request.getParameter("contents");
if (titleParam!=null && unameParam!=null && contentsParam!=null) {
try {
pst.setString(1,titleParam);
pst.setString(2,unameParam);
pst.setString(3,contentsParam);
pst.executeUpdate();
sb.append("<p>posted</p>\n");
} catch (Exception e) {
e.printStackTrace();
}
}
try {
ResultSet rs = st.executeQuery("select * from "+tbl);
while (rs.next()) {
int id = rs.getInt("id");
java.util.Date cd = rs.getDate("cdate");
String ti = rs.getString("title");
String un = rs.getString("uname");
String co = rs.getString("contents");
sb.append("<p>"+id+":"+cd+":"+ti+"["+un+"]<br />\n");
sb.append(co+"</p>\n");
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (pst != null) { pst.close(); pst = null; }
if (con != null) { con.close(); con = null; }
} catch (Exception e) {}
sb.append("<form action=\""
+ request.getRequestURI() +"\">");
sb.append("title: <input type=\"text\" name=\"title\">\n");
sb.append("name: <input type=\"text\" name=\"uname\"><br />\n");
sb.append("contents:<br />\n");
sb.append("<textarea name=\"contents\" cols=\"70\" rows=\"7\">\n");
sb.append("</textarea><br />\n");
sb.append("<input type=\"submit\" value=\"Send\">\n");
sb.append("<input type=\"reset\" value=\"Reet\">\n");
sb.append("</form>\n");
out.print(sb.toString());
}
}
|