java - Apache Olingo 的 jdbc 连接器

标签 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());
                }
           }

最佳答案

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

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

关于java - Apache Olingo 的 jdbc 连接器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326268/

相关文章:

php - 是否可以使用 Doctrine 2 扩展模型?

php - 动态添加自定义输入字段并将其保存到数据库

php - Apache 不加载 libphp7.so 模块

php - 安装 hhvm 后在 apache 和 fastcgi 上使用漂亮的 url

Java Unicode 字符宽度

java - 设置 JMenu 边距不起作用

java.util.Set 添加和删除方法签名区别

java - 使用@Suite Junit 时将忽略 CucumberOptions 标签

javascript - 如果列等于 0 则删除行 (mysql)

ruby-on-rails - 多个域由一个 Rails 应用程序提供支持