2015-07-10 41 views
0

我不知道什麼是錯,此代碼; SQL命令和路徑看起來沒問題。連接到數據庫是好的;但是當我試圖通過一個簡單的搜索;它返回那個表不存在。 我試過(相同的代碼)在不同的方法來添加數據,它工作正常;所以我不知道我做錯了什麼。嘗試連接到一個SQLite而是:SQL錯誤或丟失的數據庫(沒有這樣的表:Bank_001)

感謝您的關注。

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       First_GUI frame = new First_GUI(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

    Connection conn = null;     //Call the Connection Class from JavaSqlite Class 

/** 
* Create the frame. 
*/ 
public First_GUI() { 
    conn = Sqlite_Connection.dbconnector();  //call the connection CONN from SQLite class, method DBconnector. 
    setTitle("First GUI App"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 

    JPanel panel = new JPanel(); 
    FlowLayout flowLayout = (FlowLayout) panel.getLayout(); 
    flowLayout.setAlignment(FlowLayout.LEFT); 
    contentPane.add(panel, BorderLayout.NORTH); 

    JLabel lblEnterTheName = new JLabel("Enter the Name :"); 
    lblEnterTheName.setFont(new Font("Tahoma", Font.PLAIN, 14)); 
    lblEnterTheName.setVerticalAlignment(SwingConstants.TOP); 
    lblEnterTheName.setHorizontalAlignment(SwingConstants.LEFT); 
    panel.add(lblEnterTheName); 

    NameField = new JTextField(); 
    panel.add(NameField); 
    NameField.setColumns(10); 

    JButton Search = new JButton("Search"); 
    Search.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
    // **************************************************************** 
    // Here is the PROBLEM. 
      String sql = "SELECT * FROM Bank_001 where AccountName like ?"; 
      try { 
       PreparedStatement pst = conn.prepareStatement(sql); 
       pst.setString(1, NameField.getText()); 
       ResultSet rs = pst.executeQuery(); 

       rs.close(); 
       pst.close(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
       JOptionPane.showMessageDialog(null, e); 
      } 



     } 
    }); 
    panel.add(Search); 

    JScrollPane scrollPane = new JScrollPane(); 
    contentPane.add(scrollPane, BorderLayout.CENTER); 

    JTextPane textPane = new JTextPane(); 
    textPane.setFont(new Font("Tahoma", Font.PLAIN, 14)); 
    scrollPane.setViewportView(textPane); 


} 

}

enter image description here

enter image description here

+0

表Bank_001是否確實存在於您的數據庫中? – firelynx

+0

是的! Table Bank_001有4列7行。 –

+0

您的代碼使用的數據庫用戶是否可以訪問表Bank_001? – firelynx

回答

0

您可以顯示此代碼?

conn = Sqlite_Connection.dbconnector(); 

你在新的SQLiteDataSource上使用setURL()嗎?

假設使用setURL,它的構造是否正確? 類似於:

"jdbc:sqlite:sqlite-test.db" 
+1

看來你沒有足夠的積分就一個問題發表評論,因爲這是不到一個評論的答案,要求更多的澄清 – Rexford

+0

達拉嗨,我有它已經完成,現在的正常工作,這是一個在代碼中間出現問題,另一個論壇的用戶幫助我解決了這個問題。我會在這裏粘貼整個代碼。並感謝提問。 –

+0

很酷的保羅,是的@Rexford是一個點的事情 – dara

相關問題