- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我能够执行以下代码来从表中选择所有数据:-
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();
}
}
}
}
对于此代码,我收到以下错误:-
Connecting to database... Creating statement... java.sql.SQLException: at jdbc.q(jdbc.java:22) at jdbc$co.ex(jdbc.java:26) at jdbc$st.executeUpdate(jdbc.java:89) at com.jdbc.Insertion.main(Insertion.java:27)
即使在我尝试使用聚合函数进行选择查询时,我也会遇到类似的错误。总而言之,我只能做简单的数据选择,不能做任何其他事情。
有什么线索吗?
是否有关于如何使用 JDBC 对 KDB 数据库进行复杂查询的研究链接?
我也尝试过使用准备好的语句(代码如下),但仍然没有成功:-
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();
}
}
}
}
最佳答案
尝试使用 native API 而不是 JDBC。
关于java - 使用 JDBC 在 KDB 数据库中执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513514/
当我们使用 -s 时,主线程将一些工作委托(delegate)给从线程。 我在带有 3 个从属线程的 session 上运行以下代码:q -s 3 select from t where date w
我有一个像这样构建的表: tab: ([]col1:();col2:()) `tab insert (`testsym; "testchararr") 我现在想选择 col2 所在的行具有值 "tes
我有一个包含许多列的表(“ibmqt”),我想添加一个新列,其中包含 bool 值,指示每一行的一列(“bid”)是否大于或等于另一列列(“询问”)。 到目前为止,我最成功的尝试是: ibmqt: u
我想改造以下两行表格生成的: tb: ([] time: 2010.01.01 2010.01.01; side:`Buy`Sell; price:100 101; size:30 50) time
kdb有垂直输出吗?在 ClickHouse 中我可以指定 FORMAT VERTICAL SELECT * FROM foo FORMAT VERTICAL; Row 1: ────── date:
我将销售数据存储在数据库中。 sales_date 字段包含销售发生的日期。我想提取按月分组的数据,以便获得一月、二月等的汇总数据。有没有一种方法可以做到这一点,而不必提取整个数据然后手动执行? 最佳
kdb有垂直输出吗?在 ClickHouse 中我可以指定 FORMAT VERTICAL SELECT * FROM foo FORMAT VERTICAL; Row 1: ────── date:
我将销售数据存储在数据库中。 sales_date 字段包含销售发生的日期。我想提取按月分组的数据,以便获得一月、二月等的汇总数据。有没有一种方法可以做到这一点,而不必提取整个数据然后手动执行? 最佳
我曾多次听到人们谈论 KDB 几乎可以在短时间内处理数百万行数据。为什么这么快?这仅仅是因为数据都组织在内存中吗? 另一件事是有替代方案吗?有大型数据库供应商提供内存数据库吗? 最佳答案 快速 Goo
如何从 kdb 中每行有多个条目的其他 2 列计算一列加权平均值? 例如,给定下表: T:([]sym:`a`b`c;size:(2 8;5 2 10;3 7);price:(1 2;1 1 10;2
我有一个功能 quotes[ticker;startDate;endDate] ,以及一个函数 indexConstituents[index;startDate;endDate]产生以下内容: da
kdb 凡人/众神!! 我有一个日期时间字符串数组,但格式不同 > leads[`firstConversion] "Wed May 20 2020 08:56:54 GMT-0700 (PDT)"
我是 KDB/Q 的新手,对获取两行(不一定相邻)之间的差异有疑问。 我只有一张表,如下所示: q)tickers:`ibm`bac`dis`gs`ibm`gs`dis`bac q)pxs:100 5
q)t:flip `name`iq!(`Dent``Prefect;98 32 34) q)t name iq ---------- Dent 98 32 Prefect
假设我只想插入第一列和其他列为空。最快的方法是什么?并且可能有不同的类型。 假设有 10 列。这是我能想到的: `tab insert (`abcd;`;"";`;"";"";`;`;`;`;`;`)
我试图理解这一点: 100+\ 1 2 3 101 103 106 效果很好。 问题 1: 当我将它括在方括号中时,我得到了一个我没有预料到的错误: (100+\) 1 2 3 'Cannot wri
我在选择以下时间跨度时遇到问题: t:([] date:2#.z.d ; time: 10D21:28:47.425287000 10D12:18:23.287989000 ) date
有什么方法可以输入两个日期,然后会创建一个包括他们自己在内的日期列表? 例如。我要 2017.01.01 2017.01.02 .... 2017.01.10 我现在做的是 2017.01.01 +
我通过 .Q.dpft[] 将一张张开的表格写入磁盘。使用 \l 系统命令加载此表后,我可以从该表中 select,例如,select column_name from splayed_table w
我想在 kdb 表中添加一个新列,它应该基于现有列通过填充非空值来添加,如下所示 q)t:([]a:`a`b`c`d`e`f`g`h;b:1 0n 3 4 0n 6 0n 8;c:0n 2 0n 0n
我是一名优秀的程序员,十分优秀!