【发布时间】:2013-11-17 12:20:14
【问题描述】:
这是我的桌子:
table Paciente
+------------------------------------+
| Nome | RG | ID_Paciente |
+------------------------------------+
| Lucas | 10101 | 1 |
+------------------------------------+
table Telefone
+---------------------------------------------------------+
| DDD | Telefone | ID_Paciente | ID_Telefone |
+---------------------------------------------------------+
| 41 | 123456789 | 1 | 1 |
+---------------------------------------------------------+
我需要从 Telefone 表中打印 DDD 和 Telefone。
我正在尝试做这样的事情:
String sql = "select ID_Paciente from Paciente where Nome = '"+nome+"'";
PreparedStatement st = c.getConnection().prepareStatement(sql);
ResultSet res = st.executeQuery();
sql = "select DDD from Telefone where ID_Paciente = '"+res.getString(1)+"'";
st = c.getConnection().prepareStatement(sql);
res = st.executeQuery();
while(res.next())
{
System.out.println(" DDD: "+res.getString(1));
}
但是,它不起作用
有什么帮助吗?
【问题讨论】:
-
将 res.next() 放在第二个 sql 语句之前
-
定义“它不工作”。发生什么了?您得到的异常包含一条有意义的消息,旨在被阅读。另外,请重新阅读有关 ptepared 语句的教程,因为您错过了重点,即能够安全地绑定参数,而不使用字符串连接。
-
为什么不加入查询而不是触发两个不同的选择语句?
-
我很担心,如果 paciente 是我认为的那样,这不是家庭作业。