を使用して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 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: at jdbc.q(jdbc.java:22)jdbc $ co.ex(jdbc.java:26) jdbc $ st.executeUpdate(jdbc.java:89) com.jdbc.Insertion.main(Insertion.java:27)
EVN私は集計関数で選択クエリを試しに、私は、同様のエラーを取得するために使用します。要約すると、私はデータの単純な選択だけを行うことができ、他には何もできません。
手がかりはありますか?
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();
}
}
}
}
準備済みの文を使用してください:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html –
これも使用しました、同じエラーが発生しました。 –
新しいコードを投稿してください。ヒント: '?'がない場合あなたのSQLクエリでは、準備されたステートメントでsetXxx()を呼び出さない場合、あなたはそれを正しくやっていません。 –