2014-01-12 159 views
-3

使用Java數據庫連接我們可以聲明一個查詢我們可以在哪個表中查詢SELECT FROM兩個表?從兩個表中選擇?

這裏是我的代碼: 「」

public class Invoice { 
    public static void main(String[] args) { 
    int order_codej; 
    java.sql.Date date1; 
    java.sql.Date date2; 
    int cust_codej; 
    int quantity; 
    int sum; 
    double pricej; 
    String appellation; 
    String name; 
    String sname; 
    Scanner input = new Scanner (System.in); 
    System.out.print("Please insert order code: "); 
    order_codej = input.nextInt(); 
    String url = "jdbc:odbc:part3"; 
    Connection dbcon ; 
    Statement stmt; 
    ResultSet rs; 
    try { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    } catch(java.lang.ClassNotFoundException e) { 
     System.out.print("ClassNotFoundException: "); 
     System.out.println(e.getMessage());} 
    try { 
     dbcon = DriverManager.getConnection(url,"x", "x"); 
     stmt = dbcon.createStatement(); 
     rs = stmt.executeQuery("SELECT order_code, sent_date, order_date, Orders.cust_code," + 
           "name, surname, price, appellation, product_sum, Consists.quantity" + 
           "FROM Orders, Customers, Products, Consists" + 
           "WHERE Orders.cust_code= Customers.cust_code AND" + 
           "Orders.order_code = Consists.order_code AND" + 
           "Consists.product_code = Products.product_code" + 
           "order_code =" + order_codej ); 
     while (rs.next()) { 
     order_codej = rs.getInt("order_code"); 
     date1 = rs.getDate("sent_date"); 
     date2 = rs.getDate("order_date"); 
     cust_codej = rs.getInt("cust_code"); 
     quantity = rs.getInt("quantity"); 
     sum = rs.getInt("product_sum"); 
     pricej = rs.getFloat("price"); 
     appellation = rs.getString("appellation"); 
     name = rs.getString("name"); 
     sname= rs.getString("surname"); 
     } 
    rs.close(); 
    stmt.close(); 
    dbcon.close(); 
} 
catch(SQLException e) 
{ 
System.out.print("SQLException: "); 
System.out.println(e.getMessage()); 
} 
    } 
} 

Inncorrect語法附近。這是編譯後的消息,但它並沒有說明究竟在哪裏,我的問題。

+0

請添加一些代碼請 –

+0

這也可能是一個糟糕的工程模式的症狀。如果你不能修改它(例如在遺留數據庫上工作),至少應該嘗試創建一個視圖。 –

+0

曾聽說過加入? –

回答

0

你缺少你的SQL查詢代碼空間: 示例 「從接單,客戶,產品,下設」 + 「FROM ..」 => 「從接單,客戶,產品,ConsistsFROM ..」 無空間在Consists之間和從

+0

SQLException:[Microsoft] [ODBC SQL Server驅動程序]無效的描述符索引爲什麼這是? – Mikel