一个连接数据库查询的问题。在线等。

zhouxiangaibiancheng 2011-12-21 05:48:00
Connection conn=DriverManager.getConnectio("jdbc:oracle:thin:@localhost:1521:jbitdb","epet","bdqn");//连接数据库
Statement ment=conn.createStatement();
ResultSet set=ment.executeQuery("select * from pet a inner join master b on b.ID=a.master_id where b.ID=1");
while(set.next()){
System.out.println(set.getInt("ID")); //请问这里怎么取值?我这个写错了的。
System.out.println(set.getString("Name"));//请问这里怎么取值?我这个写错了的。
}
...全文
79 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
int type=1;
String sql=String.format("select pet.NAME from pet inner join master on master.ID=pet.master_id where master.ID={0}",type);
System.out.println(sql);
BUG弄潮儿 2011-12-21
  • 打赏
  • 举报
回复
select * from pet a inner join master b on b.ID=a.master_id where b.ID=1
这句与在数据库里执行可以查到数据么
其他的如果没有空指针应该正确的
  • 打赏
  • 举报
回复
谢谢。楼上的。请问占位符这样可以吗?
//String sql=String.format("select pet.NAME from pet inner join master on master.ID=pet.master_id where master.ID={0}",type);
//System.out.println(sql);
皓天星辰 2011-12-21
  • 打赏
  • 举报
回复
用这个在SQL Window中查询有记录吗?
select * from pet a inner join master b on b.ID=a.master_id where b.ID=1

建议代码修改如下:
Connection conn=DriverManager.getConnectio("jdbc:oracle:thin:@localhost:1521:jbitdb","epet","bdqn");//连接数据库
Statement ment=conn.createStatement();
ResultSet set=ment.executeQuery("select id,name from pet a inner join master b on b.ID=a.master_id where b.ID=1");
while(set.next()){
System.out.println(set.getInt(1)); //请问这里怎么取值?我这个写错了的。
System.out.println(set.getString(2));//请问这里怎么取值?我这个写错了的。
}

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧