简体   繁体   中英

How to take take user input in JDBC for a Statement?

Here is my JDBC code, I'm trying to take user input for a SQL statement via the console:

import java.sql.*;

public class Aufgabe_6_2 {
    public static void main(String [] args) {
        String searchTerm = "a";
        try {
            Connection conn = DriverManager.getConnection("jdbc:mariadb://slo.swe.th-luebeck.de:3306" +
                    "/Gruppe18?user=Gruppe18&password=xxxxxx");
            PreparedStatement stmt = conn.prepareStatement("select kd_Nr, name From Kunde WHERE name LIKE ? group by kd_Nr");
            stmt.setString(1,"%" + searchTerm + "%");
            ResultSet res = stmt.executeQuery();
            while (res.next()) {
                System.out.println(res.getInt("kd_Nr") + ", " + res.getString("name"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}

In this case, I already wrote my own SQL statement, but now I want to delete my own SQL statement and take the statement from the user input.

The answer is very simple. Thanks, @Ali choopani:

Scanner s = new Scanner(System.in);
System.out.println("enter your character: ");
String searchTerm = s.nextLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM