Java 当键入一个Jtextfield时,将数据库中的数据提取到Jtextfield中
大家好,我有一个连接到oracle数据库的Swing应用程序,我希望在JTextField中键入一个值后,JFrame上的其他JTextField将加载来自数据库的后续数据,但我似乎没有实现这一点。我尝试了下面的代码,但没有得到任何结果Java 当键入一个Jtextfield时,将数据库中的数据提取到Jtextfield中,java,swing,autocomplete,oracle11g,Java,Swing,Autocomplete,Oracle11g,大家好,我有一个连接到oracle数据库的Swing应用程序,我希望在JTextField中键入一个值后,JFrame上的其他JTextField将加载来自数据库的后续数据,但我似乎没有实现这一点。我尝试了下面的代码,但没有得到任何结果 txtNo.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent ke) { Connection conn = null;
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){
}
}
});
您知道数据库连接是否正常工作吗?例如,您能否在侦听器之外运行数据库部分并使其正常工作 如果是这样,我建议使用
ActionListener
或FocusListener
。KeyListener(有时是必要的)通常很笨拙-通常有更好的方法(请参阅和解释):
您知道数据库连接是否正常工作吗?例如,您能否在侦听器之外运行数据库部分并使其正常工作 如果是这样,我建议使用
ActionListener
或FocusListener
。KeyListener(有时是必要的)通常很笨拙-通常有更好的方法(请参阅和解释):
- 用
DocumentListener
- EDT上的数据库连接是个坏主意(太慢)。有关更多信息,请参阅指南
- 您容易受到SQL注入的攻击
- 避免使用空的
语句,否则您不知道出现了什么问题。如果您不选择适当的错误处理,至少要记录堆栈跟踪(只需打印它,或者使用catch
)记录器
- 用
DocumentListener
- EDT上的数据库连接是个坏主意(太慢)。有关更多信息,请参阅指南
- 您容易受到SQL注入的攻击
- 避免使用空的
语句,否则您不知道出现了什么问题。如果您不选择适当的错误处理,至少要记录堆栈跟踪(只需打印它,或者使用catch
)记录器
- 谢谢你们的帮助。按照Nick的建议,我使用FocusListener得到了我想要的:
txtNo.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
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().trim() + "'";
ResultSet rs = st.executeQuery(load);
while(rs.next()){
txtName.setText(rs.getString("SPARE_DESC"));
}
}catch(Exception ae){
}
}
});
谢谢你们的帮助。按照Nick的建议,我使用FocusListener得到了我想要的:
txtNo.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
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().trim() + "'";
ResultSet rs = st.executeQuery(load);
while(rs.next()){
txtName.setText(rs.getString("SPARE_DESC"));
}
}catch(Exception ae){
}
}
});
罗宾和尼克都是对的 下面是一个如何实现他们正在讨论的内容的示例
public class TestForm02 {
public static void main(String[] args) {
new TestForm02();
}
public TestForm02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyForm());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class MyForm extends JPanel {
private JTextField txtNo;
private JTextField txtName;
private String partToLoad;
private Timer loadTimer;
public MyForm() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
txtName = new JTextField(12);
txtNo = new JTextField(12);
txtName.setEnabled(false);
add(new JLabel("Parts #:"), gbc);
gbc.gridx++;
add(txtNo, gbc);
gbc.gridy++;
gbc.gridx = 0;
add(new JLabel("Description:"), gbc);
gbc.gridx++;
add(txtName, gbc);
txtNo.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
loadParts();
}
});
txtNo.getDocument().addDocumentListener(new DocumentListener() {
protected void update() {
loadTimer.restart();
}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
@Override
public void changedUpdate(DocumentEvent e) {
update();
}
});
loadTimer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadParts();
}
});
loadTimer.setRepeats(false);
loadTimer.setCoalesce(true);
}
protected void loadParts() {
String text = txtNo.getText();
// Don't want to trigger this twice...
if (text == null ? partToLoad != null : !text.equals(partToLoad)) {
partToLoad = text;
txtNo.setEnabled(false);
txtName.setEnabled(false);
BackgroundWorker worker = new BackgroundWorker();
worker.execute();
}
}
protected class BackgroundWorker extends SwingWorker<String, String> {
@Override
protected String doInBackground() throws Exception {
// Do you're database load here. Rather then updating the text
// field, assign it to variable and return it from here
String desc = "Some description"; // load me :D
// Inserted delay for simulation...
Thread.sleep(2000);
return desc;
}
@Override
protected void done() {
try {
String value = get();
txtName.setText(value);
txtName.setEnabled(true);
txtNo.setEnabled(true);
} catch (InterruptedException exp) {
exp.printStackTrace(); // Log these some where useful
} catch (ExecutionException exp) {
exp.printStackTrace(); // Log these some where useful
}
}
}
}
}
公共类TestForm02{
公共静态void main(字符串[]args){
新TestForm02();
}
公共测试表单02(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
JFrame=新JFrame();
frame.setLayout(新的BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的MyForm());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
受保护的类MyForm扩展了JPanel{
私有JTextField txtNo;
私有JTextField txtName;
私有字符串部分加载;
专用定时器;
公共MyForm(){
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
gbc.插图=新插图(2,2,2,2);
gbc.anchor=GridBagConstraints.WEST;
txtName=新的JTextField(12);
txtNo=新的JTextField(12);
txtName.setEnabled(false);
添加(新的JLabel(“零件:”),gbc);
gbc.gridx++;
添加(txtNo,gbc);
gbc.gridy++;
gbc.gridx=0;
添加(新JLabel(“说明:”),gbc);
gbc.gridx++;
添加(txtName,gbc);
txtNo.addFocusListener(新的FocusAdapter(){
@凌驾
公共无效焦点丢失(焦点事件e){
加载部件();
}
});
txtNo.getDocument().addDocumentListener(新的DocumentListener()){
受保护的无效更新(){
loadTimer.restart();
}
@凌驾
公共作废插入更新(文档事件e){
更新();
}
@凌驾
公共作废移除更新(文档事件e){
更新();
}
@凌驾
公共作废更改日期(记录事件e){
更新();
}
});
loadTimer=新计时器(250,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
加载部件();
}
});
loadTimer.setRepeats(假);
loadTimer.setCoalesce(真);
}
受保护的无效加载部件(){
String text=txtNo.getText();
//我不想触发这两次。。。
如果(text==null?partToLoad!=null:!text.equals(partToLoad)){
partToLoad=文本;
txtNo.setEnabled(false);
txtName.setEnabled(false);
BackgroundWorker工人=新的BackgroundWorker();
worker.execute();
}
}
受保护类BackgroundWorker扩展SwingWorker{
@凌驾
受保护的字符串doInBackground()引发异常{
//你在这里加载数据库吗