我必须创建一个将数据库连接到java gui的地址簿应用程序。我现在正在处理我的搜索按钮,几乎得到它,但无法弄清楚如何连接所有四个表。我需要按姓氏搜索,然后我希望它显示该人的所有相关信息。
到目前为止,这是我所拥有的,它似乎正在工作,但当我尝试INNER JOIN emailAddresses和phoneNumbers时,我收到语法错误。
resultSet =
statement.executeQuery( "SELECT * FROM names INNER JOIN addresses ON names.personID = addresses.personID WHERE lastName LIKE '%" + last + "%' ");
resultSet.next();
jTextField1.setText(resultSet.getString("firstName"));
jTextField2.setText(resultSet.getString("lastName"));
jTextField3.setText(resultSet.getString("address1"));
jTextField4.setText(resultSet.getString("address2"));
jTextField5.setText(resultSet.getString("city"));
jTextField6.setText(resultSet.getString("state"));
jTextField7.setText(resultSet.getString("zipcode"));
jTextField8.setText(resultSet.getString("phoneNumber"));
jTextField9.setText(resultSet.getString("emailAddress"));
答案 0 :(得分:4)
您需要添加括号并使用*
而不是%
作为通配符。
我没有经过测试,但这应该有效:
"SELECT *
FROM ((names INNER JOIN addresses ON names.personID = addresses.personID)
INNER JOIN emailadresses ON names.personID = emailadresses.PersonID)
INNER JOIN PhoneNumbers ON names.PersonID = PhoneNumbers.PersonID
WHERE lastName LIKE '*" & last & "*'"
说它应该有效 - 记得以任何姓氏逃避撇号 Escaping ' in Access SQL
答案 1 :(得分:1)
尝试此SQL命令,连接四个表与连接一个表或连接更多表相同:
SELECT firstName, lastName, address1, address2, city, state, zipcode, phoneNumber, emailAddress
FROM names
INNER JOIN addresses
ON names.personID = addresses.personID
INNER JOIN emailadresses
ON names.personID = emailadresses.PersonID
INNER JOIN PhoneNumbers
ON names.PersonID = PhoneNumbers.PersonID
WHERE lastName LIKE '%parameter%'
答案 2 :(得分:0)
SELECT * FROM names A
INNER JOIN address B ON A.personID = B.personID
INNER JOIN emailAddress C ON A.personID = C.personID
INNER JOIN phoneNumbers D ON A.personID = D.personID