从两个表中选择?

时间:2014-01-12 17:14:14

标签: java jquery sql

使用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语法。这是编译后的消息,但它没有说明我的问题究竟在哪里。

2 个答案:

答案 0 :(得分:1)

看看:

  

Java MySQL using jquery

  

Visual explanation of sql joins


select 
  * 
from 
  table t1, 
  table t2
where 
  t1.id = t2.id;

也许像上面那样?

答案 1 :(得分:0)

您的SQL查询代码中缺少空格: 示例“FROM订单,客户,产品,包含”+ “来自......” => “来自订单,客户,产品,包括FROM ......”,包含和FROM之间没有空格