Reputation: 53
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.
Upvotes: 0
Views: 2050
Reputation: 53
The answer is very simple. Thanks, @Ali choopani:
Scanner s = new Scanner(System.in);
System.out.println("enter your character: ");
String searchTerm = s.nextLine();
Upvotes: 3