[JSP ] 在xampp和tomcat用jsp連接mysql(已解決)

看板java作者 (gotdream)時間14年前 (2011/12/19 11:55), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
以解決 沒有加入 mysql-connector-java-5.0.8-bin.jar 的CLASSPATH 加入後就可以連接到資料庫了!^^ 加入path,classpath 如http://0rz.tw/tRUTl 此篇 出現新的錯誤訊息? 實在是不曉得為何 冒昧請教各位... 我的path是 classpath是 運行javac可以出現help訊息... 如下 C:\Documents and Settings\Administrator>echo %classpath% .;C:\Program Files\Java\jdk1.7.0_01\lib;C:\Program Files\Java\jdk1.7.0_01\lib\to ols.jar; C:\Documents and Settings\Administrator>echo %path% C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;d:\AppServ\Apache2 ;d:\AppServ\php5;d:\AppServ\MySQL\bin;C:\Program Files\Java\jdk1.7.0_01\bin; C:\Documents and Settings\Administrator>javac Usage: javac <options> <source files> where possible options include: -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -nowarn Generate no warnings ...... 新錯誤訊息如下(以重開mysql.apache.tomcat) description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) root cause javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver .... 小弟是新手 冒昧請教(此環境簡易的jsp網頁沒有連mysql的jsp可以成功顯示) 我是 1.安裝xampp version 1.7.7 2.安裝http://download.oracle.com/otn-pub/java/jdk/7u1-b08/jdk-7u1-windows-i586.exe 3.啟動xampp control panel即可按下apache,mysql,tomcat啟動成功 然後小弟模仿此頁 http://www.9lessons.info/2008/09/jsp-login-page-to-connect-mysql-atabase.html 的裡面的結構網頁 先建立mysql裡面一個 叫test的database 裡面在放一個 user table 建立起那示範網頁的user_id,password,... 然後將test資料夾(內有login.html,login.jsp,reg.html,reg.jsp,welcome.jsp) 放入c:\xampp\tomcat\webapps\ROOT\ 形成 c:\xampp\tomcat\webapps\ROOT\test 然後再瀏覽器裡輸入 http://localhost:8080/test/login.html 當輸入帳號 密碼後按送出到login.jsp 卻會出現 type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /jspmysql/login.jsp at line 7 4: String userid=request.getParameter("user"); 5: session.putValue("userid",userid); 6: String pwd=request.getParameter("pwd"); 7: Class.forName("com.mysql.jdbc.Driver"); 8: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","r1234"); 9: Statement st= con.createStatement(); 10: ResultSet rs=st.executeQuery("select * from user where user_id='"+userid+"'"); 一直告訴我 Class.forName("com.mysql.jdbc.Driver");是錯誤的 改成 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 也一樣 (其實很像問現在到底是該用哪一個敘述? 這兩個功能有差嗎?) 是我步驟有錯嗎?(我這裡的mysql帳號是設定root密碼是r1234) 怎都連不到mysql資料庫... 請教大家 感謝!(這樣子不得其門而入...>__<困擾)... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.17.52.209 ※ 編輯: gotdreamed 來自: 163.17.52.209 (12/19 12:01)

12/19 12:01, , 1F
你classpath裡有driver嘛? com.mysql.jdb.Driver是對的沒錯
12/19 12:01, 1F
※ 編輯: gotdreamed 來自: 163.17.52.209 (12/19 13:01)

12/19 13:15, , 2F
我跟http://0rz.tw/tRUTl 設定path,classpath完後錯誤
12/19 13:15, 2F
※ 編輯: gotdreamed 來自: 163.17.52.209 (12/19 13:20) ※ 編輯: gotdreamed 來自: 163.17.52.209 (12/19 13:23)

12/19 13:41, , 3F
我是指driver有沒有在classpath,不是jdk
12/19 13:41, 3F

12/19 21:58, , 4F
感謝!查了一下!真的是這問題!解決了我初學的問題^^
12/19 21:58, 4F
※ 編輯: gotdreamed 來自: 180.176.176.142 (12/19 21:59)
文章代碼(AID): #1ExhMwaj (java)