gpt4 book ai didi

java - 在restful web服务中访问mysql jdbc驱动程序

转载 作者:行者123 更新时间:2023-12-01 12:19:42 25 4
gpt4 key购买 nike

我有一个非常简单的 Restful 网络服务,我想在其中访问数据库中的数据。不幸的是我总是遇到这个异常:

SCHWERWIEGEND: Servlet.service() for servlet [Jersey REST Service] in context with path [/test_projects] threw exception [org.glassfish.jersey.server.ContainerException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test_projects] with root cause java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test_projects

我的 Web 服务中的资源类如下所示。

package ws;

import java.sql.SQLException;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

import model.Person;

import com.google.gson.Gson;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Path("/persons")
public class PersonsResource {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/zhaw_projects";

static final String USER = "root";
static final String PASS = "";

@GET
// produce json
@Produces("application/json")
public String getPersonJSON() throws SQLException {

// build connection
Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);

// create statement
Statement statement = connection.createStatement();

// select query
String personsQuery = "SELECT * FROM persons";

// execute query
ResultSet rs = statement.executeQuery(personsQuery);

// a list of persons
List<Person> persons = new ArrayList<Person>();

// add persons
while (rs.next()) {

persons.add(new Person(rs.getInt("personID"), rs
.getString("firstName"), rs.getString("lastName"), rs
.getString("token"), rs.getString("email")));
}

// new json string with the persons list
Gson gson = new Gson();
String jsonString = gson.toJson(persons);

return jsonString;
}
}

我的 web.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>ws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

我使用 apache tomcat 作为 Web 服务器。我的 JDBC 驱动程序当前位于 WebContent/WEB_INF/lib 目录中。我需要将 jdbc 驱动程序放在哪里才能使其工作?

最佳答案

我明白了。我不得不把

Class.forName(JDBC_DRIVER);

到我的代码。

关于java - 在restful web服务中访问mysql jdbc驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26780404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com