作爲我的任務的一部分,我被要求從數據庫中檢索特定的數據。Java中的SQL從哪裏選擇
但是,當我嘗試插入我的SQL語句時,我的結果不會回來。
我使用已經在前一個類中分配的變量pcode,我知道這就好像我刪除單引號和輸入postcode(這是列名),它返回數據庫中的所有結果,但是我無法讓它與我的pcode一起工作。以下是代碼。
public static void pcodeSearch(String pcode){
try {
Connection c = DriverManager.getConnection("jdbc:sqlite:C:/Users/Colin/sale");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM sales WHERE postcode = '" + pcode + "';");
while(rs.next()) {
System.out.println(rs.getString("street"));
}
}
catch (SQLException se) {
se.printStackTrace();
}
這是代碼的作品,這將返回來自我所創建的數據庫中的所有街道名稱,我從來沒有讓它通過充分磨合他們的成千上萬的結果,但它顯然是建立與數據庫的連接在表銷售搜索和從科拉姆返回信息「街頭」
public static void pcodeSearch(String pcode){
try
{
Connection c = DriverManager.getConnection("jdbc:sqlite:C:/Users/Colin/sale");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM sales;
while(rs.next())
{
System.out.println(rs.getString("street"));
}
}
catch (SQLException se)
{
se.printStackTrace();
}
這是我的主類的代碼,希望這可以幫助上下文
public static void main(String[] args) {
JFrame s = new JFrame("House Sales");
JButton inst = new JButton("Instructions");
JButton close = new JButton("Close Application");
JButton go = new JButton("Search");
JTextField pc = new JTextField(pcode);
inst.setPreferredSize(new Dimension(400,80));
close.setPreferredSize(new Dimension(400,80));
go.setPreferredSize(new Dimension(400,80));
pc.setPreferredSize(new Dimension(400,80));
inst.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(s, "Please enter a full or partial postcode to obtain the sold prices for that area");
}
});
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s.setVisible(false);
}
});
go.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
pcode = pc.getText();
sqlConnector.pcodeSearch(pcode);
//JOptionPane.showMessageDialog(s, pcode);
}
});
Container c = s.getContentPane();
c.setLayout(new FlowLayout());
c.add(inst);
c.add(close);
c.add(pc);
c.add(go);
//c.add(t1);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.pack();
s.setVisible(true);
}
我們可以看到表創建查詢嗎?也是手動執行而不使用WHERE和WHERE的結果。 –
我也會嘗試調試executeQuery行並檢查pcode變量保存的是什麼。也許它擁有一個值不存在於數據庫.. –
嗨在Maciej,pcode變量取自我的主類GUI中的文本字段,所以它會變化,我已檢查它是正確分配字符串使用一個showMessageDialog,它返回我輸入的內容,數據庫本身是正確鏈接的,因爲我已經成功地使用沒有包含where語句的代碼檢索街道(街道名稱)。 – cozbouk