Question:
What is the best way to display database results on a webpage using java?
Gary Grumble
2011-01-10 13:40:28 UTC
Trying to help out a friend with a small project.

What he wants to do is to query a database and then display the results on a webpage. Then from that webpage, to be able to select from the result list and download some docs associated with the selection.

He wants to do it in Java. Is there any sample code that I can follow to do this? I have googled but not found anything really great on how to build the webpage in java. Is there a better way to do this?

Thanks!

g
Three answers:
MANOJ
2011-01-10 14:11:11 UTC
You should learn about JSP (Java Server Pages).



Here is the simple jsp page code.........





<%@page language="java" import="java.sql.*"%>

<% Driver DriverRecordset1 =

(Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance();

Connection Conn = DriverManager.getConnection("jdbc : mysql ://localhost:3306/database",

"username","password");

Statement StatementRecordset1 = Conn.createStatement();

ResultSet Recordset1 = StatementRecordset1.executeQuery("select * from mytable");

boolean Recordset1_isEmpty = !Recordset1.next();

boolean Recordset1_hasData = !Recordset1_isEmpty;

int Recordset1_numRows = 0;

%>

<%

int Repeat1__numRows = 10 ;

int Repeat1__index = 0 ;

Recordset1_numRows += Repeat1__numRows;

%>



<% while ((Recordset1_hasData)&&(Repeat1__numRows-- != 0))

{

%>













<%Repeat1__index++;

Recordset1_hasData = Recordset1.next();}

%>

<%= (String) Recordset1.getObject("Field1Name") %> <%= (String) Recordset1.getObject("Field2Name") %> <%= (String) Recordset1.getObject("Field3Name") %> <%= (String) Recordset1.getObject("Field4Name") %>


<%

Recordset1.close();

Conn.close();

%>



http://javacodespot.blogspot.com/
?
2016-10-28 05:02:00 UTC
Make your get properly of entry to database an ODBC records source and then use the java skinny ODBC driving force to attach with it with information from records source call (DSN). In living house windows, bypass to regulate Panel > Administrative techniques > ODBC records sources and create a sparkling get properly of entry to records source.
Neunerball
2011-01-10 18:35:45 UTC
Check the chapter for Java Server Pages in this tutorial http://download.oracle.com/javaee/5/tutorial/doc/



In case you don't know, for the Database access, you use JDBC-API (Java DataBase Connectivity API)

http://download.oracle.com/javase/tutorial/jdbc/basics/index.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...