【问题标题】:JDBI json response o MySql databaseJDBI json 响应 o MySql 数据库
【发布时间】:2015-03-23 22:35:02
【问题描述】:

我正在尝试使用端点在 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 连接器文件。

【问题讨论】:

    标签: mysql tomcat7 jdbi


    【解决方案1】:

    这对我有用

    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;
        }
    }
    

    在 webinf/webxml

    <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"       
    />
    

    【讨论】:

    • 很高兴你把它整理好了。不要忘记投票接受答案!
    【解决方案2】:

    您应该在依赖项中包含 mysql 驱动程序。

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    

    【讨论】:

    • thnx 是数据源问题
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多