JDBI json响应o MySql数据库

时间:2015-03-23 22:35:02

标签: mysql tomcat7 jdbi

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

输入例外报告

message 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连接器文件。

2 个答案:

答案 0 :(得分: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;
    }
}

在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>

on tomcat context file

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

答案 1 :(得分:0)

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

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