How to store retrieved data from mysql and display as a result for user's search in java


How to store retrieved data from mysql and display as a result for user's search in java


try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/customer";
String uName = "root";
String password = ""; // unsafe sharing password
Connection con = DriverManager.getConnection(url, uName, password);
String query = "select * from mansions";
PreparedStatement ps = con.prepareStatement(query);
//ps.executeUpdate(); it wont accept query with select keyword
ResultSet rs = ps.executeQuery();
String str = "";
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getLong(3)+rs.getString(4)+" "+rs.getString(5)+" "+rs.getString(6)+" "+rs.getString(7)+" "+rs.getInt(8)+" "+rs.getString(9)+" "+rs.getString(10));
str = rs.getString(2);
System.out.println(str);
con.close();
} catch(Exception e){
System.out.println(e);
}



I am able to retrieve 10 columns from mansions table in mysql and this project is to find available mansions in particular area. I am done with creating table data and retrieve but I have to store and display as search results for the user to get available rooms . To achieve this how can I store mysql data and display as a result? I am not sure which one would help to get expected output. though I am beginner this is core java project I am using console to get user input
ex input from user is
gender : male, members : 1 or 2 , airconditioner : yes or no, food : yes or no
output : Rooms available please check below



mansion_names contact_numbers gender food persons_allowed airconditioned rent area available_rooms
Lahir 9003218193 male no 1 or 2 no 3000 Kodampakkam 4 non ac for single or doubles
please suggest me few so that I can try on my own. thanks friends...!!





learn how to read input from console, then write a query having where conditions with placeholder, then set params in the prepared statement, execute and print. The query might have to generated dynamically based on the filter condition chosen by the client.
– gagan singh
Jun 30 at 18:34





hi gagan brother thanks for your time, so no need to store retrieved data's by writing java code , we can straight away display output by writing queries in java (apart from getting user inputs )and matching data (sql query) to display search result , am i following correct gagan bro...?
– user2926683
Jun 30 at 18:41





db is already storing all the data for you. Every search can be independent. You might want to store popular search result in memory cache when you move from a console application to a high end web application. Till then just query and display the result.
– gagan singh
Jun 30 at 18:47





thanks much bro, yes the search is independent and let me get inputs from console using scanner and let me work with queries to display output. thanks bro for your support as a beginner i need help from senior people like you until i am get familiar with it. thanks again..!!
– user2926683
Jun 30 at 18:52





It is not safe for security reasons to have the password for your database's ROOT account in your posted code.
– hev1
Jun 30 at 23:01




1 Answer
1



You should annotate your method with @ResponseBody and @PostMapping and return the search results. Then you can do an AJAX post request each time the search is performed and get the return value of the method in your javascript (done by the @ResponseBody annotation).


@ResponseBody


@PostMapping


@ResponseBody






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API