gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:56 25 4
gpt4 key购买 nike

我能够执行以下代码来从表中选择所有数据:-

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com