java链接database SQL语句求助
这几天一直困扰的一个问题 看了很多帖子 搜了资料但是人比较笨 总是想不出怎么解决 情况是是这样子的 在java程序中 有一个String artistName
然后我用一个 String artistName = JOptionPane.showInputDialog(null,"Please Input the Name of Artist:","Artist Name",JOptionPane.INFORMATION_MESSAGE); 来获取String
之后再database里面搜索时 我是这样写的
try {
c = DriverManager.getConnection("jdbc:derby://localhost:1527/myart");
s = c.createStatement();
rs = s.executeQuery("SELECT PAINTINGS.PAINTINGID, PAINTINGS.PAINTINGNAME,PAINTINGS.ARTIST, PAINTINGS.DATEOFPAINTING, PAINTINGS.GALLERY, PAINTINGS.DESCRIPTION, LOCATION.ROOMLABEL, LOCATION.WALL
FROM PAINTINGS INNER JOIN LOCATION ON PAINTINGS.PAINTINGID=LOCATION.PAINTINGID
WHERE PAINTINGS.ARTIST=artistName);
while (rs.next()) {
displayPage.append("\n" + rs.getString(1) + "\t" + rs.getString(2) + "\t\t" + rs.getString(3) +"\t" + rs.getString(4)+ "\t\t" + rs.getString(5)+ "\t" + rs.getString(6)+"\t\t" + rs.getString(7)+ "\t" + rs.getString(8));
}
rs.close();
s.close();
c.close();
} catch (Exception e1) {
e1.printStackTrace();
}
我比较笨在table那里我只会建好两个全部写进去然后join起来。。。
结果就是 每次点击artist按钮 弹出对话框要求输入一个artist name, 输入之后却不是在表里寻找我输入的内容 而是在表里搜索artistName这个String 我知道会这样的原因但就是想不到如何解决
求各位大神帮忙!!!
------解决方案--------------------try
rs = s.executeQuery("SELECT PAINTINGS.PAINTINGID, PAINTINGS.PAINTINGNAME,PAINTINGS.ARTIST, PAINTINGS.DATEOFPAINTING, PAINTINGS.GALLERY, PAINTINGS.DESCRIPTION, LOCATION.ROOMLABEL, LOCATION.WALL
FROM PAINTINGS INNER JOIN LOCATION ON PAINTINGS.PAINTINGID=LOCATION.PAINTINGID
WHERE PAINTINGS.ARTIST='"+artistName+"'");