Hi All,
I am new to Java. I am able to connect to SQL Server DB, fetch data. Can someone help me how do I render the data on the UI. Meaning, creating a html table kind and displaying the data.
Thanks.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi All,
I am new to Java. I am able to connect to SQL Server DB, fetch data. Can someone help me how do I render the data on the UI. Meaning, creating a html table kind and displaying the data.
Thanks.
This fully depends upon the data, and how you wish to render. You indicate 'html table' - suggesting this is more of an html question than a java question. If you wish to use something like java Swing, I recommend reading: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
That doesn't clarify things much...you could display a table in a webpage, in a program with a graphical user interface, on the command line, save it in a file that can be read by another program (eg excel)...I recommend clarifying exactly how you wish to display your data, as that will help us point you in the right direction (rather than guessing what your needs are)
Are you using Java EE with perhaps some framework(s?) to create the front end and then deploying a full web application or do you just want to output the data to a .html file?
Anyway, i think this might be the wrong section of the forum if i understood correctly and you have already handled the fetching of your data from the DB?
But what ever your plan is, i think the basic principle would be that you manage the data you dug from the db to some kind of model objects. For example if you got 10 rows from Person table, you want to make 10 Person objects that correspond to the db table and pass a list containing those Person-objects to a repeater of some kind. What that repeater is, depends on the fist question that copeg also asked: What do you want to do? .
If you want just to outstream the data to html file you might want to write your own handler. Maybe something as simple as some filewriter implementation that writes the HTML-stuff to a file
For example: You made Person -object from each of your query result rows, and packed em to an ArrayList<Person> listOfPersons.
Then you write the non-repeating part of the html file, then go throught the lsit of objects ( "repeat" ) and write each line to the file as the html table rows, and end the file.
Something in these lines:
//... something in the lines of PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("thehtmlfile.html"))); pw.println("<html><head/><body><table>"); // the upper side of the file //then loop through the person objects which have the html row in their toString method. for ( Person currentPerson : listOfPersons ) pw.println(currentPerson); // rest of the html file pw.println("</table></body></html>"); pw.close();
The Person's toString could be something likepublic String toString() { return "<tr><td>" + firstName +"</td><td>" + lastName + "</td></tr>"; }
Note that this is not a proper way of doing things by any imagination, but i hope you got the idea. Also read about the MVC design pattern.