mysql - JDBI json 响应 o MySql 数据库

标签 mysql tomcat7 jdbi

我正在尝试使用端点在 eclipse 中使用 tomcat 7 作为服务器来查询 mysql 数据库,但它总是给我这个错误,有人用 jdbi 解决了这个问题吗

类型异常报告

消息 java.sql.SQLException:找不到合适的驱动程序 jdbc:mysql://127.0.0.1/demo

The code:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;



import org.json.JSONException;
import org.json.JSONObject;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;

@Path("/jdbiservice")
public class JdbiService {
      @Path("{f}")
      @GET
      @Produces("application/json")
      public Response convertFtoCfromInput(@PathParam("f") int f) throws JSONException {
        DBI dbi = new DBI("jdbc:mysql://127.0.0.1/demo", "user", "pass");       
        Handle h = dbi.open();

        BatchExample b = h.attach(BatchExample.class);
        Something s =b.findById(f);
        h.close();  
        JSONObject jsonObject = new JSONObject(s);
        String result =  jsonObject.toString();
        return Response.status(200).entity(result).build();
      }  

}

您好,在 eclipse 项目路径和 tomcat lib 文件夹中有 jar 连接器文件。

最佳答案

这对我有用

package com.crunchify.restjersey;

import java.util.List;

import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import org.json.*;
import org.skife.jdbi.v2.*;


@Path("/sensorservice")
public class SensorService {

     @Path("{id}")
     @DELETE
     public Response deleteSensorById(@PathParam("id") int id) {
     ///...
            try {

                DBI dbi = new DBI(SensorService.getDataSource());
                Handle h = dbi.open();

                SensorInterface si = h.attach(SensorInterface.class);
                si.deleteById(id);;

                h.close();  
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         String result = "Deleted";
         return Response.status(200).entity(result).build();
    }

    private static DataSource getDataSource (){
        DataSource ds = null;
        InitialContext contex;
        try {
            contex = new InitialContext();

            ds = ( DataSource) contex.lookup("java:comp/env/jdbc/jndiname");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ds;
    }
}

在 web inf/web xml

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

关于 tomcat 上下文文件

<Resource
    name = "jdbc/jndiname"
    auth = "Container"
    type = "javax.sql.DataSource"
    maxActive ="100"
    maxIdle = "30"
    maxWait = "10000"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://localhost:3306/schema"
    username = "user"
    password = "pass"       
/>

关于mysql - JDBI json 响应 o MySql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29221714/

相关文章:

mysql - sql查询以获取每月计数

mysql - 将一列中的字符串拆分为同一表中的三列并插入到新表中

mysql - 从 MySQL 获取数据库到 SQLite

tomcat - 我已经从 go daddy 购买了一个域名,我需要在我的本地计算机上托管这个域,我在上面运行了 tomcat 7

MySQL - 优化查询以使用临时删除和使用文件排序

spring-boot - Spring 数据休息 : "Unable to configure LocalContainerEntityManagerFactoryBean from @EntityScan"

java.net.SocketException : Connection reset on Tomcat Only

java - JDBI 结果集映射器从查询结果集中创建对象列表?

postgresql - Spring Boot 2.1.4 + JDBI + HikariCP + PostgreSQL 发生错误后连接未释放到池

java - 使用 jdbi3 启动 Sprinboot 时出现 AbstractMethod 错误