每次输入新名称时,在jtextarea中显示一行 [英] Display a row in jtextarea each time a new name is entered

查看:126
本文介绍了每次输入新名称时,在jtextarea中显示一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次输入新名称时,我想在jtextarea中显示一行。假设用户在jtextfield中写Paul,基于SQLite,它应该显示它的相应响应,它将是Smith。我有一个有几个名字的数据库,无论我写什么,说彼得,它显示史密斯它应该显示帕克。我想要的是每次用户输入一个名字时,都会显示相应的姓氏。



我尝试了什么:



我试过这段代码,但只显示数据库的第一行:

编辑:这是我使用的方法

I want to display a row in jtextarea each time a new name is entered. Suppose a user writes "Paul" in a jtextfield, based on SQLite, it should show it's corresponding response, which will be "Smith". I have a database with several names and no matter what I write, say "Peter", it shows "Smith" where it should have shown "Parker". What I want is each time a user enters a name, it's corresponding surname should appear.

What I have tried:

I have tried this code but only the first row from the db appears:
This is the method I used

public void fetch() throws Exception{	
	Connection con = null;
	con = sqliteConnection.sqliteConnector();
	PreparedStatement pst = null;
	ResultSet rs = null;
	try
	    {
		String quest=userInputField.getText().toLowerCase().trim().toString();
			String sql = "select * from user where name LIKE '% %'";
			pst = con.prepareStatement(sql);
			//pst.setString(1, quest);//getting array index out of bounds 0
			rs = pst.executeQuery();
			if(rs.next()){ 
				String ans = rs.getString("answer");
				textarea.append(ans +"\n");   
				
			}else {
            	  textarea.append("Nothing was found." +"\n");  
            }
			
			System.out.println("Succesfully retrieved data from bcsdb");
			if (pst != null) pst.close();
            System.out.println("PST was successfully closed!");
            if (rs != null) rs.close();
            System.out.println("PS was successfully closed!");
            if (con != null) con.close();
            System.out.println("Con was successfully closed!");
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}		
	}





提前致谢!



Thanks in advance!

推荐答案

您的SQL语句将匹配所有名称。您需要使用指定用户输入的名称的参数对其进行修改。
Your SQL statement will match all names. You need to modify it with a parameter that specifies the name entered by the user.


这篇关于每次输入新名称时,在jtextarea中显示一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆