Hi Guys I have a Swing Application that is connected to an oracle database, I want it such that Once I type a Value into the JTextField, the other JTextfields on the JFrame are loaded with subsequent data from the database but I do not seem to achieve this. I have tried the Following code but it fetched nothing.
txtNo.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "Username", "Password");
Statement st = conn.createStatement();
String load = "Select * from Store_info_table where PART_NUMBER = '" + txtNo.getText() + "'";
ResultSet rs = st.executeQuery(load);
while(rs.next()){
txtName.setText(rs.getString("SPARE_DESC"));
}
}catch(Exception ae){
}
}
});
Do you know if you’re database connection is working? For example, can you run the database portion outside of the listener and it works?
If so, I would suggest using
ActionListeneror aFocusListenerinstead. KeyListeners (while sometimes necessary) are generally clumsy – there’s usually a better approach (see Java Swing: Using ActionMap for and explaination):