SQLite in Java(ask user to input a String and out put all data in table)

huangapple 未分类评论53阅读模式
英文:

SQLite in Java(ask user to input a String and out put all data in table)

问题

我有一个表格,其中包含以下姓名

Joseph
Steven
Cory
Lory

我想要让用户在数据库/表格中搜索特定的字符串,并打印出他们提供的名字。

例如,如果用户输入ory,那么Cory和Lory将被打印出来。

这是我目前的代码

public void part3() {
    try {
        this.query = dbConnect.createStatement();
        
        //String userinput = sc.nextLine();
        
        ResultSet rs = query.executeQuery("SELECT * FROM studentTable WHERE first_name LIKE 'userinput'");
        
        while(rs.next()) {
            String fname = rs.getString("first_name");
            String lname = rs.getString("last_name");
            //fname.contains(userinput);
            
            System.out.println(fname + " ");
            
        }
        
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
英文:

So I have a table that has the following names

Joseph
Steven
Cory
Lory

I am trying to ask user to search for specific Strings in the names in the database/table and print out the name they provided

For example if the user inputs ory then Cory and Lory will print out
This is what I currently have

public void part3() {
			try {
				this.query = dbConnect.createStatement();
				
				//String userinput = sc.nextLine();
				
				ResultSet rs = query.executeQuery("SELECT * FROM studentTable WHERE first_name LIKE 'userinput'");
				
				while(rs.next()) {
					String fname = rs.getString("first_name");
					String lname = rs.getString("last_name");
					//fname.contains(userinput);
					
					System.out.println(fname + " ");
					
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

答案1

得分: 0

以下是您要翻译的内容:

你确定这是您想要执行的查询吗?

ResultSet rs = query.executeQuery("SELECT * FROM studentTable WHERE first_name LIKE 'userinput'");

难道不应该是这样吗?注意百分号(%)

ResultSet rs = query.executeQuery("SELECT * FROM studentTable WHERE first_name LIKE '%userinput%'");

英文:

Are you sure this is the query you want to execute ?

ResultSet rs = query.executeQuery("SELECT * FROM studentTable WHERE first_name LIKE 'userinput'");

Should it not be this instead ? pay attention to the %

ResultSet rs = query.executeQuery("SELECT * FROM studentTable WHERE first_name LIKE '%userinput%'");

huangapple
  • 本文由 发表于 2020年5月4日 10:13:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61584151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定