使用JDBC在KDB数据库中执行查询

3
我可以执行以下代码来选择表中的所有数据:-
package com.jdbc;
import java.sql.*;
//in kdb+3.x and above
//init table with
//\p 5001
//Employees:([]id:0 1 2;firstName:`Charlie`Arthur`Simon;lastName:`Skelton`Whitney`Garland;age:10 20 30;timestamp:.z.p+til 3)
public class Selection{

  static final String JDBC_DRIVER="jdbc";
  static final String DB_URL="jdbc:q:localhost:5001";
  static final String USER="";
  static final String PASS="";
  public static void main(String[] args){
    Connection conn=null;
    Statement stmt=null;
    try{
      Class.forName(JDBC_DRIVER);
      System.out.println("Connecting to database...");
      conn=DriverManager.getConnection(DB_URL,USER,PASS);
      System.out.println("Creating statement...");
      stmt=conn.createStatement();
      ResultSet rs=stmt.executeQuery("SELECT id, firstName, lastName, age,timestamp FROM Employees");
      while(rs.next()){
        long id=rs.getLong("id");
        long age=rs.getLong("age");
        String first=rs.getString("firstName");
        String last=rs.getString("lastName");
        Timestamp timestamp=rs.getTimestamp("timestamp");
        System.out.print("ID: "+id);
        System.out.print(", Age: "+age);
        System.out.print(", FirstName: "+first);
        System.out.println(", LastName: "+last);
        System.out.println(", Timestamp: "+timestamp);
      }
      rs.close();
      stmt.close();
      conn.close();
    }catch(SQLException se){
      se.printStackTrace();
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try{
        if(stmt!=null)
          stmt.close();
      }catch(SQLException se2){
      }
      try{
        if(conn!=null)
          conn.close();
      }catch(SQLException se){
        se.printStackTrace();
      }
    }
  }
}

但是,每当我尝试运行其他查询时,代码就会出错,例如以下代码:

package com.jdbc;
import java.sql.*;

//in kdb+3.x and above
//init table with
//\p 5001
//Employees:([]id:0 1 2;firstName:`Charlie`Arthur`Simon;lastName:`Skelton`Whitney`Garland;age:10 20 30;timestamp:.z.p+til 3)

public class Insertion {

  static final String JDBC_DRIVER="jdbc";
  static final String DB_URL="jdbc:q:localhost:5001";
  static final String USER="";
  static final String PASS="";

  public static void main(String[] args){
    Connection conn=null;
    Statement stmt=null;
    try{
      Class.forName(JDBC_DRIVER);

      System.out.println("Connecting to database...");
      conn=DriverManager.getConnection(DB_URL,USER,PASS);

      System.out.println("Creating statement...");
      stmt=conn.createStatement();
      stmt.executeUpdate("INSERT INTO Employees VALUES(9, 10, 'X', 'Y', " + new java.sql.Timestamp(0) + ")");

      stmt.close();
      conn.close();
    }catch(SQLException se){
      se.printStackTrace();
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try{
        if(stmt!=null)
          stmt.close();
      }catch(SQLException se2){
      }
      try{
        if(conn!=null)
          conn.close();
      }catch(SQLException se){
        se.printStackTrace();
      }
    }
  }
}

对于此代码,我收到了以下错误信息:-连接到数据库...创建语句...java.sql.SQLException: 在jdbc.java:22处的jdbc.q() 在jdbc.java:26的jdbc$co.ex() 在 jdbc.java:89的jdbc$st.executeUpdate() 在 Insertion.java:27中的com.jdbc.Insertion.main()
即使我尝试使用聚合函数进行选择查询,我仍然会收到类似的错误。总结一下,我只能对数据进行简单的选择而已。
有任何线索吗?
有没有关于如何在KDB数据库上使用JDBC进行复杂查询的学习链接?
我也尝试使用预编译语句(代码如下),但仍然没有运气:-
package com.jdbc;
import java.sql.*;

//in kdb+3.x and above
//init table with
//\p 5001
//Employees:([]id:0 1 2;firstName:`Charlie`Arthur`Simon;lastName:`Skelton`Whitney`Garland;age:10 20 30;timestamp:.z.p+til 3)

public class Insertion {

  static final String JDBC_DRIVER="jdbc";
  static final String DB_URL="jdbc:q:localhost:5001";
  static final String USER="";
  static final String PASS="";

  public static void main(String[] args){
    Connection conn=null;
    PreparedStatement stmt=null;
    try{
      Class.forName(JDBC_DRIVER);

      System.out.println("Connecting to database...");
      conn=DriverManager.getConnection(DB_URL,USER,PASS);

      System.out.println("Creating statement...");
      stmt=conn.prepareStatement("INSERT INTO Employees(id,firstName,lastName,age,timestamp) VALUES(?, ?, ?, ?, ?)");
  stmt.setInt(1, 10);
  stmt.setString(2, "X");
  stmt.setString(3, "Y");
  stmt.setInt(4, 5);
  stmt.setTimestamp(5, new Timestamp(0));
      stmt.executeUpdate();

      stmt.close();
      conn.close();
    }catch(SQLException se){
      se.printStackTrace();
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try{
        if(stmt!=null)
          stmt.close();
      }catch(SQLException se2){
      }
      try{
        if(conn!=null)
          conn.close();
      }catch(SQLException se){
        se.printStackTrace();
      }
    }
  }
}

3
使用预处理语句:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html - JB Nizet
1
我也使用了那个,但仍然遇到了相同的错误。 - user3236167
1
已更新帖子,包含代码。 - user3236167
1
糟糕,抱歉。我已更新了更正后的代码。我也尝试执行了一下,但错误仍然存在。 - user3236167
1
这是完整的堆栈跟踪。 - user3236167
显示剩余5条评论
2个回答

0

尝试使用本地API而不是JDBC。


0

我一直在苦恼同样的INSERT问题。偶然发现,如果将“VALUES”更改为小写的“values”,那么静态 INSERT语句就开始工作了。但是,使用PreparedStatement执行仍然没有成功。(在我的情况下,不幸的是,使用本地API不是一个选项。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接