-3

Using Java database connectivity can we declare a query in which we can SELECT FROM two tables?

Here is my code:

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 syntax near ".". That's the message after compiling but it doesn't say where exactly thats my problem.

Mikel
  • 167
  • 1
  • 1
  • 8

2 Answers2

1

Take a look at:

Java MySQL using jquery

And

Visual explanation of sql joins


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

maybe something like above?

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
0

You are missing spaces in your SQL query code: Example "FROM Orders, Customers, Products, Consists" + "FROM .." => "FROM Orders, Customers, Products, ConsistsFROM .." without space between Consists and FROM