前提・実現したいこと
EclipseのTomcatサーバーでMySQLDBに接続できません。
発生している問題・エラーメッセージ
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
該当のソースコード
ソースコードは正常であると思われます。
Javaファイルで、【MySQL接続用のコード】を
public static void main(String[] args)
にいれて、Javaで実行すれば、正常にDBの内容が表示されます。
st.executeQuery("select * from country");
しかしながら、「サーバで実行」すると、エラーメッセージになります。
サーバで実行の場合、
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
の中が実行されるものと理解しています。この中にも、上記と全く同じ【MySQL接続用のコード】を入れています。
おそらく、サーバ側(Tomcat)の問題であると思われますが、どうしていいのかわかりません。
Eclipse 4とEclipse 3どちらでやっても同じです。
なにか設定で見落としているところがあるのでしょうか?
package test3;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test33 extends HttpServlet {
public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/world","root","root"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from country"); while(rs.next()) { String id = rs.getString(1); String location = rs.getString(2); String tid = rs.getString(3); String time = rs.getString(4); System.out.println ("ABBREVIATION:"+id+" FULLNAME:"+location+" REGION:"+tid+" SUBREGION:"+time); } rs.close(); st.close(); conn.close(); System.exit(0); } catch (ClassNotFoundException e) { System.out.println("ドライバを読み込めませんでした "+ e); } catch (SQLException e) { System.out.println("データベース接続エラー"+ e); } } public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<html>"); writer.println("<head>"); writer.println("<title>Sample Application Servlet Page</title>"); writer.println("</head>"); writer.println("<body bgcolor=white>"); writer.println("<table border=\"0\">"); writer.println("<tr>"); writer.println("<td>"); writer.println("<img src=\"images/tomcat.gif\">"); writer.println("</td>"); writer.println("<td>"); writer.println("<h1>Sample Application Servlet</h1>"); writer.println("This is the output of a servlet that is part of"); writer.println("the Hello, World application.ssss"); writer.println("</td>"); writer.println("</tr>"); writer.println("</table>"); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/world","root","root"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from country"); while(rs.next()) { String id = rs.getString(1); String location = rs.getString(2); String tid = rs.getString(3); String time = rs.getString(4); System.out.println ("ABBREVIATION:"+id+" FULLNAME:"+location+" REGION:"+tid+" SUBREGION:"+time); } rs.close(); st.close(); conn.close(); System.exit(0); } catch (ClassNotFoundException e) { System.out.println("ドライバを読み込めませんでした "+ e); } catch (SQLException e) { System.out.println("データベース接続エラー"+ e); } } public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { }
}
試したこと
JAVAアプリケーションで実行した場合、コンソールに下記のように出ますので、public static void main(String[] args) は正常であると思われます。
ABBREVIATION:VAT FULLNAME:Holy See (Vatican City State) REGION:Europe SUBREGION:Southern Europe
ABBREVIATION:VCT FULLNAME:Saint Vincent and the Grenadines REGION:North America SUBREGION:Caribbean
ABBREVIATION:VEN FULLNAME:Venezuela REGION:South America SUBREGION:South America
ABBREVIATION:VGB FULLNAME:Virgin Islands, British REGION:North America SUBREGION:Caribbean
ABBREVIATION:VIR FULLNAME:Virgin Islands, U.S. REGION:North America SUBREGION:Caribbean
ABBREVIATION:VNM FULLNAME:Vietnam REGION:Asia SUBREGION:Southeast Asia
ABBREVIATION:VUT FULLNAME:Vanuatu REGION:Oceania SUBREGION:Melanesia
ABBREVIATION:WLF FULLNAME:Wallis and Futuna REGION:Oceania SUBREGION:Polynesia
ABBREVIATION:WSM FULLNAME:Samoa REGION:Oceania SUBREGION:Polynesia
ABBREVIATION:YEM FULLNAME:Yemen REGION:Asia SUBREGION:Middle East
ABBREVIATION:YUG FULLNAME:Yugoslavia REGION:Europe SUBREGION:Southern Europe
ABBREVIATION:ZAF FULLNAME:South Africa REGION:Africa SUBREGION:Southern Africa
ABBREVIATION:ZMB FULLNAME:Zambia REGION:Africa SUBREGION:Eastern Africa
ABBREVIATION:ZWE FULLNAME:Zimbabwe REGION:Africa SUBREGION:Eastern Africa
次に、Tomcatサーバで実行した場合、public void doGet (HttpServletRequest request, HttpServletResponse response)が実行されると思いますし、実際にWebページはwriter.printlnで出力したHTMLが表示されるのですが、その後にあるDBの中身の結果は表示されず、
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
とコンソールに出てしまいます。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。