2016-04-09 8 views
3

ich in der Lage bin den folgenden Code auszuführen, alle Daten aus der Tabelle zu wählen: -Ausführen von Abfragen in KDB Datenbank JDBC mit

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(); 
     } 
    } 
    } 
} 

Aber wenn ich versuche, andere Abfragen ausführen, dann gibt der Code mir Fehler zum Beispiel für den folgenden Code: -

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(); 
     } 
    } 
    } 
} 

für diesen Code, erhalte ich folgende Fehlermeldung: -

zu Datenbank verbinden ... erstellen ing-Anweisung ... java.sql.SQLException: bei jdbc.q (jdbc.java:22) unter jdbc $ co.ex (jdbc.java:26) um jdbc $ st.executeUpdate (jdbc.java:89) bei com.jdbc.Insertion.main (Insertion.java:27)

Evn wenn ich Auswahlabfragen mit Aggregationsfunktionen versuchen, verwende ich ähnliche Fehler zu erhalten. Zusammenfassend kann ich nur eine einfache Auswahl von Daten und nichts anderes tun.

Irgendwelche Hinweise?

Gibt es Studien, Links zur Verwendung von JDBC für komplexe Abfragen in der KDB-Datenbank?

habe ich versucht zu Prepared Statements (Code ist wie folgt), aber immer noch, kein Glück: -

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

Verwenden Sie vorbereitete Anweisungen: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html –

+1

Ich habe das auch verwendet , immer noch der gleiche Fehler aufgetreten. –

+0

Dann poste deinen neuen Code. Tipp: Wenn es kein '?' Wenn Sie in Ihrer SQL-Abfrage setXxx() nicht auf Ihrer vorbereiteten Anweisung aufrufen, machen Sie es nicht richtig. –

Antwort

0

Ich habe mit dem gleichen INSERT Problem gekämpft. Rein zufällig, festgestellt, dass, wenn Sie "Werte" in die Kleinbuchstaben "Werte" ändern, dann statische INSERT-Anweisung zu arbeiten beginnt. Habe immer noch kein Glück mit PreparedStatement Ausführung. (In meinem Fall, die native API leider keine Option zu verwenden.)