当一个JTextField是类型化的提取从数据库到数据的JTextField [英] Fetch data from database into Jtextfield when One Jtextfield is Typed
问题描述
大家好我有一个连接到Oracle数据库的Swing应用程序,我希望它这样,一旦我键入一个值到JTextField中,在JFrame的其他JTextFields将被加载从数据库中后续的数据,但我不知道似乎实现这一目标。我曾尝试以下code,但它获取什么。
txtNo.addKeyListener(新KeyAdapter(){
公共无效的keyTyped(KeyEvent的KE){
康涅狄格州的连接= NULL;
尝试{
的Class.forName(oracle.jdbc.driver.OracleDriver); 康恩=的DriverManager.getConnection(JDBC:神谕:薄:@localhost:1521:XE,用户名,密码);
声明ST = conn.createStatement();
字符串负荷='+ txtNo.getText()+从哪里Store_info_table = PART_NUMBER选择*';
结果集RS = st.executeQuery(负载);
而(rs.next()){
txtName.setText(rs.getString(SPARE_DESC));
}
}赶上(例外AE){ }
}
});
你知道,如果你的数据库连接是否正常?例如,你可以监听外部运行数据库部分和它的作品?
如果是这样,我会建议使用的ActionListener
或的FocusListener
来代替。 KeyListeners(虽然有时是必要的)一般笨拙 - 通常有一种更好的方法(见的Java Swing:使用的ActionMap 一>并交代):
进口java.awt.event中。*;
进口的javax.swing *。公共类例子扩展盒{ JLabel的txtName的=新的JLabel(没什么进入了); 公共温度(){
超(BoxLayout.Y_AXIS);
//添加的FocusListener第一场
最终的JTextField txtNo =新的JTextField(20);
txtNo.addFocusListener(新CustomFocusListener(txtNo));
加(txtNo); //添加的TextListener第一场
最终的JTextField txtNo2 =新的JTextField(20);
txtNo2.addFocusListener(新CustomFocusListener(txtNo2));
加(txtNo2); //添加的TextListener第一场
最终的JTextField txtNo3 =新的JTextField(20);
txtNo3.addFocusListener(新CustomFocusListener(txtNo3));
加(txtNo3); 添加(新的JButton(有所作为)); 加(txtName的); }
公共静态无效的主要(字串[] args){
最终的JFrame帧=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新实例());
frame.pack();
frame.setLocationRelativeTo(NULL);
frame.setVisible(真);
} / **
*您定制focust听众,做你的所有SQL工作
* /
公共类CustomFocusListener扩展FocusAdapter {
JTextField的领域; 公共CustomFocusListener(JTextField的领域){
this.field =领域;
} @覆盖
公共无效指定者(FocusEvent e)在{
//假设你的数据库连接的作品,这给你一个榜样
txtName.setText(field.getText());
/ *连接康恩= NULL;
尝试{
的Class.forName(oracle.jdbc.driver.OracleDriver); 康恩=的DriverManager.getConnection(JDBC:神谕:薄:@localhost:1521:XE,用户名,密码);
声明ST = conn.createStatement();
字符串负荷='+ field.getText()+从哪里Store_info_table = PART_NUMBER选择*';
结果集RS = st.executeQuery(负载);
而(rs.next()){
txtName.setText(rs.getString(SPARE_DESC));
}
}赶上(例外AE){ } * /
}
}
}
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 ActionListener
or a FocusListener
instead. KeyListeners (while sometimes necessary) are generally clumsy - there's usually a better approach (see Java Swing: Using ActionMap for and explaination):
import java.awt.event.*;
import javax.swing.*;
public class Example extends Box{
JLabel txtName = new JLabel("Nothing Entered");
public temp(){
super(BoxLayout.Y_AXIS);
// Add FocusListener to first field
final JTextField txtNo = new JTextField(20);
txtNo.addFocusListener(new CustomFocusListener(txtNo));
add(txtNo);
// Add TextListener to first field
final JTextField txtNo2 = new JTextField(20);
txtNo2.addFocusListener(new CustomFocusListener(txtNo2));
add(txtNo2);
// Add TextListener to first field
final JTextField txtNo3 = new JTextField(20);
txtNo3.addFocusListener(new CustomFocusListener(txtNo3));
add(txtNo3);
add(new JButton("Do Something"));
add(txtName);
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Example());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* Your custom focust listener that does all your SQL Work
*/
public class CustomFocusListener extends FocusAdapter{
JTextField field;
public CustomFocusListener(JTextField field){
this.field = field;
}
@Override
public void focusLost(FocusEvent e) {
//Assuming your database connection works, this gives you an example to follow
txtName.setText(field.getText());
/*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 = '" + field.getText() + "'";
ResultSet rs = st.executeQuery(load);
while(rs.next()){
txtName.setText(rs.getString("SPARE_DESC"));
}
}catch(Exception ae){
}*/
}
}
}
这篇关于当一个JTextField是类型化的提取从数据库到数据的JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!