2015-07-10 67 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

相关问题