隨筆 - 4  文章 - 10  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          最近看到網(wǎng)上不少朋友說用JDBC連不上MySQL.有的說用IDE做沒問題,但不用IDE部署,自己部署就不成功.想知道為什么?
              確實(shí),我記得我那時也是搞不懂,也很郁悶.其實(shí)問題很簡單,一、要注意web.xml,這可不是看看而已,寫配置文件是j2ee的一件大事。二、注意驅(qū)動放置的位置。三、注意項(xiàng)目的結(jié)構(gòu),你不要把jsp文件丟到WEB-INF文件夾下面去了。
              tomcat版本: tomcat-5.0.28;
              mysql版本: mysql-4.1.13-win32;

              廢話少說, 我來演示:
              1、啟動mysql。
              Snap1.jpg

              Snap4.jpg

              2、建數(shù)據(jù)庫,建表,我這都不演示了,請參考相關(guān)文章。

              3、在tomcat中的webapps文件中建一個SQL文件夾,在SQL文件夾中再建一個WEB-INF文件夾,再在WEB-INF文件夾中建一個classes文件夾和web.xml文件。

              4、web.xml代碼如下:
             
              <?xml version="1.0" ?>
              <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"         "http://java.sun.com/dtd/web-app_2_3.dtd">

              <!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
              <web-app>
                  <welcome-file-list>
                      <welcome-file>mysql.jsp</welcome-file>
                  </welcome-file-list>
              </web-app>

              5、在SQL文件夾中建一個mysql.jsp。代碼如下:

              <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
              <html>
              <body>
              以下是從MySQL數(shù)據(jù)庫讀取的數(shù)據(jù):<hr>
              <table border=1>
              <tr><td>ID</td><td>書名</td><td>出版社 </td><td>價格</td></tr>

              <%

                 Class.forName("com.mysql.jdbc.Driver").newInstance();
                 Connection     con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/BookDB?useUnicode=true&characterEncoding=GBK","t14cwf","cwf");
                 Statement stmt=con.createStatement();
                  ResultSet rst=stmt.executeQuery("select * from book");
                  while(rst.next())
                  {
                      out.println("<tr>");
                      out.println("<td>"+rst.getString("bookId")+"</td>");
                      out.println("<td>"+rst.getString("bookName")+"</td>");
                      out.println("<td>"+rst.getString("publisher")+"</td>");
                      out.println("<td>"+rst.getFloat("price")+"</td>");
                      out.println("</tr>");
                  }
                  //關(guān)閉連接、釋放資源
                  rst.close();
                  stmt.close();
                  con.close();
               %>
              </table>
              </body>
              </html>

              6、將mysql-connector-java-3.1.10-bin.jar放到tomcat\common\lib中。

              7、啟動tomcat.
              Snap2.jpg

              8、在瀏覽器中瀏覽:
              Snap8.jpg

          posted on 2007-08-13 15:43 冬天出走的豬 閱讀(237) 評論(0)  編輯  收藏 所屬分類: Database
          主站蜘蛛池模板: 沁阳市| 神农架林区| 安远县| 镇远县| 赣州市| 涿鹿县| 绿春县| 公安县| 乐东| 泗阳县| 商丘市| 卢龙县| 化德县| 山西省| 金山区| 宁阳县| 奉新县| 福鼎市| 南昌市| 忻州市| 南投市| 都匀市| 新乡市| 泰来县| 崇礼县| 年辖:市辖区| 裕民县| 方城县| 宁武县| 苍梧县| 福州市| 周至县| 都昌县| 青海省| 克什克腾旗| 长武县| 介休市| 崇明县| 恩平市| 区。| 措美县|