ITeye博客

简单BBS(1)

justin8335 · 2007-07-12 22:04 · Markdown

JSP+Servlet+Mysql开发

数据库创建:

   为了管理和日后修改部署的方便,我们在应用目录下建一个SQl目录,用于存放建数据库的.sql文件。

   在MYsql的命令行窗口中,我们导入.Sql文件

mysql >  \. E:\\workspace\\BBS\\SQL\\bbs.sql

   JDBC~MYsql 连接数据库语句

java 代码

  1. Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  2. String url ="jdbc:mysql://localhost/bbs?user=root&password=admin&useUnicode=true&characterEncoding=8859_1";
  3. conn= DriverManager.getConnection(url);

 利用递归的方法实现BBS的树状结构。

java 代码

java 代码

  1. <%!
  2. String str = "";
  3. private void tree(Connection conn,int id,int level){
  4. Statement stmt =null;
  5. ResultSet rs = null;
  6. String preStr = "";
  7. for(int i=0;i<level;i++)
  8. {
  9. preStr += "------";
  10. }
  11. try{
  12. String sql = "select * from article where pid = "+id+"";
  13. stmt = conn.createStatement();
  14. rs = stmt.executeQuery(sql);
  15. while(rs.next())
  16. {
  17. str += "<tr><td>"+rs.getInt("id")+"</td><td>"+
  18. preStr+rs.getString("title")+"</td></tr>" ;
  19. if(rs.getInt("isleaf")!=0)
  20. {
  21. tree(conn,rs.getInt("id"),level+1);
  22. }
  23. }
  24. }catch(SQLException e)
  25. {
  26. e.printStackTrace();
  27. }finally
  28. { try{
  29. if(rs!=null){
  30. rs.close();
  31. rs = null;
  32. }
  33. if(stmt!=null){
  34. stmt.close();
  35. stmt= null;
  36. }
  37. }catch(SQLException e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. %>
  44. <%
  45. response.setContentType("text/html;charset=GBK");
  46. Class.forName("com.mysql.jdbc.Driver");
  47. String url ="jdbc:mysql://localhost/bbs?user=root&password=admin&useUnicode=true&characterEncoding=gbk";
  48. Connection  conn= DriverManager.getConnection(url);
  49. Statement stmt = conn.createStatement();
  50. String sql = "select * from article where pid = 0";
  51. ResultSet rs = stmt.executeQuery(sql);
  52. while(rs.next())
  53. {
  54. //表头
  55. str += "<tr><td>"+rs.getInt("id")+"</td><td>"+
  56. rs.getString("title")+"</td></tr>";
  57. if(rs.getInt("isleaf")!=0)
  58. {
  59. tree(conn,rs.getInt("id"),1);
  60. }
  61. }
  62. rs.close();
  63. stmt.close();
  64. conn.close();
  65. %>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。