Apache Olingo的jdbc连接器

时间:2017-07-26 11:56:20

标签: java mysql apache jdbc olingo

我使用Apache Olingo开发了一个JAVA项目,其中我试图从MySQL数据库中获取数据但是我收到了以下错误

有例外! com.mysql.jdbc.Driver

在Apache olingo中要为此导入哪个库。下面是我的Java类

private EntityCollection getData(EdmEntitySet edmEntitySet){

           EntityCollection productsCollection = new EntityCollection();
           // check for which EdmEntitySet the data is requested
           if(DemoEdmProvider.ES_PRODUCTS_NAME.equals(edmEntitySet.getName())) {
               List<org.apache.olingo.commons.api.data.Entity> productList = productsCollection.getEntities();


               try
                {
                  // create our mysql database connection
                  //String myDriver = "org.gjt.mm.mysql.Driver";
                   String myDriver ="com.mysql.jdbc.Driver";
                  String myUrl = "jdbc:mysql://localhost:3306/testDB";
                  Class.forName(myDriver);
                  Connection conn = DriverManager.getConnection(myUrl, "root", "test1234");

                  // our SQL SELECT query. 
                  // if you only need a few columns, specify them by name instead of using "*"
                  String query = "SELECT * FROM Employee";

                  // create the java statement
                  Statement st = conn.createStatement();

                  // execute the query, and get a java resultset
                  ResultSet rs = st.executeQuery(query);

                  // iterate through the java resultset
                  while (rs.next())
                  {

                    String NAME = rs.getString("NAME");
                    String CITY = rs.getString("CITY");
                    int AGE = rs.getInt("AGE");

                    // print the results
                    final Entity e1 = new Entity()
                              .addProperty(new Property(null, "NAME", ValueType.PRIMITIVE, NAME))
                              .addProperty(new Property(null, "CITY", ValueType.PRIMITIVE, CITY))
                              .addProperty(new Property(null, "AGE", ValueType.PRIMITIVE,
                                 AGE));
                          e1.setId(createId("Products", 1));
                          productList.add(e1);
                  }
                  st.close();
                }
                catch (Exception e)
                {
                  System.err.println("Got an exception! ");
                  System.err.println(e.getMessage());
                }
           }

1 个答案:

答案 0 :(得分:1)

您可以添加mysql连接器Jar文件 链接是:https://dev.mysql.com/downloads/connector/j/

下载并导入您的项目 - &gt;右键单击 - &gt;导入 - &gt;文件系统 - &gt;浏览 - &gt;您在哪里存储连接器