【问题标题】:How to perform DML(Update, Insert, Delete) operations on google spanner from a java application?如何从 Java 应用程序对 google spanner 执行 DML(更新、插入、删除)操作?
【发布时间】:2018-03-19 23:47:23
【问题描述】:

需要你的帮助!

我正在尝试使用驱动程序类 (com.simba.cloudspanner.core.jdbc42.CloudSpanner42Driver) 执行 DML 操作,但我遇到了类似

的异常
"Caused by: shaded.com.google.cloud.spanner.SpannerException: INVALID_ARGUMENT: DML statements(INSERT, UPDATE and DELETE) are not supported." 

但 SELECT 查询工作正常。下面是我的java代码。

请告诉我如何从 java 应用程序对 spanner 执行 DML 操作。

我尝试使用Mutation.newInsertBuilderMutation.newUpdateBuilderMutation.delete 来实现使用com.google.cloud.spanner.Mutation; libraries 的 DML 操作,但是我实际上正在寻找某种实现,用户可以运行 SQL 语句来执行DML 操作。

public class SimbuDriverInsert {

   static final  String CONNECTION_URL = "jdbc:cloudspanner://localhost;Project=optimistic-leaf-197820;Instance=testspanner01;Database=students;PvtKeyPath=C:\\MuleWorkspace\\test-driver\\src\\main\\resources\\gcloudPrivateKey.json";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      Class.forName("com.simba.cloudspanner.core.jdbc42.CloudSpanner42Driver");
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(CONNECTION_URL);
      stmt = conn.createStatement();


      String sql = "INSERT INTO studentdetails (id,age,name) " +
              "VALUES (100, 30, 'Ali')";
       stmt.executeUpdate(sql);
       System.out.println("Inserted record into the table...");
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }
}//end main

}

【问题讨论】:

    标签: google-cloud-platform google-cloud-spanner


    【解决方案1】:

    Oracle 提供的官方 JDBC 驱动程序(连同 Simba)不支持 DML 和 DDL 语句。 This open source driver does support both。如果您在项目中包含此驱动程序并更改以下行

    Class.forName("com.simba.cloudspanner.core.jdbc42.CloudSpanner42Driver");

    进入

    Class.forName("nl.topicus.jdbc.CloudSpannerDriver");

    代码应该可以工作。该驱动程序使用与官方驱动程序相同的 URL 语法,但也增加了许多额外的可能性。 Have a look at the Wiki page驱动程序以获取更多信息。

    驱动可以添加为maven依赖或者downloaded from the releases page of the project

    在此处查看有关如何使用驱动程序的更多示例:http://www.googlecloudspanner.com/ 与不同的框架和工具。

    【讨论】:

    • 嗨 Knut Olav Loite,很高兴收到你的回复 :) 我尝试使用 Class.forName("nl.topicus.jdbc.CloudSpannerDriver");也。但是我在 nl.topicus.jdbc.statement.CloudSpannerStatement.executeUpdate(CloudSpannerStatement.java:579) 处遇到异常 :: java.sql.SQLFeatureNotSupportedException。而且,我的查询非常简单,例如“UPDATE studentdetails SET age = 99, name = 'Alibaba' WHERE id =100”
    • 看起来您正在调用 Statement#executeUpdate(String sql, int autoGeneratedKeys);而不是 Statement#executeUpdate(String sql);。前者不受支持,因为 Google Cloud Spanner 不支持任何自动生成的密钥。如果您只使用 SQL 字符串调用 executeUpdate,您应该不会收到此错误。
    猜你喜欢
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多