0

皆さん、こんにちは。私の投稿をチェックしていただきありがとうございます。Mysql jdbc ドライバーを使用して、サーブレットをデータベースに接続しようとしています。mysql jdbc ドライバーの .jar は、フォルダーapache - tomcat-7.0.27/libにあります。MyServlet はサーブレットであり、接続を確立する必要がある同じフォルダーに SQL.java があります。

private static Connection conn = null;
Class.forName(driver).newInstance();
conn = (Connection) 
DriverManager.getConnection("jdbc:mysql://"+"localhost:3306"+"/"+ "ergasia3", "root" , "spiros");`

残念ながら、これを実行しようとすると、エラーが発生します:com.mysql.jdbc.Driver .

これが私のweb.xmlです

<web-app>
  <display-name>WebApp01</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.srk.pkg.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet.do</url-pattern>
  </servlet-mapping>
  <resource-ref>
    <description>database</description>
    <res-ref-name>jdbc/ergasia3</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref> 
</web-app>

そして今私のcontext.xml

<Context path="/ergasia3" docBase="ergasia3"
debug="5" reloadable="true" crossContext="true">
  <Resource name="jdbc/ergasia3" auth="Container"
  type="javax.sql.DataSource" 
  user="root" password="spiros"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/ergasia3" 
  maxActive="15" maxIdle="3" /> 
</Context>
4

3 に答える 3

1

.jar を Tomcat の lib に置かず、アプリの lib フォルダーに置きます。すべての外部 .jar はここに保持する必要があります。

于 2012-06-26T10:50:14.163 に答える
0

あなたの瓶をWEB-INF\libの下に置いてください

于 2012-06-26T11:24:07.733 に答える
0

ドライバーマネージャーを使用して接続を作成する代わりに、tomcatのリソースとしてデータベース接続情報が既に定義されているため、次のようにリソースを使用する方が簡単です。

// context used to create a datasource
Context dataSourceContext = new InitialContext();
// when you need a connection to the database
DataSource ds = (DataSource)dataSourceContext.lookup("java:comp/env/jdbc/ergasia3");
Connection conn = ds.getConnection();

また、Soumyadipが以前に述べたように、アプリケーションはtomcat libディレクトリではなくlibディレクトリを検索するため、外部jarとしてmysqljdbcドライバーをtomcatlibディレクトリではなくWebアプリケーションに追加する必要があります。 mysqljdbcドライバー。これをどのように行うかは、Webアプリの開発に使用しているIDEによって異なります。

于 2012-06-26T11:25:15.673 に答える