Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display Java output in HTML

Posted on 2013-10-31
2
Medium Priority
?
458 Views
Last Modified: 2013-11-04
I have embedded the following code into an html page. How do I display the results in HTML?

<html>
<body>
<script type="text/javascript">

public class TestSQL
{
    public static void main(String[] args)
    {
        try
        {  
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;" +
            "databaseName=dwhomesdborlando;user=xxx;password=xxx;";
            Connection con = DriverManager.getConnection(connectionUrl);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT last_name, first_name, phone FROM employees");

            while (rs.next())
            {
                String x = rs.getString("last_name") + ", " + rs.getString("first_name") + " - " + rs.getString("phone");
                System.out.println(x);
            }
            g
        } catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString());
        } catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString());
        }
    }
}
</script>
</body>
</html>
0
Comment
Question by:mfinocc
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39615253
As you know Java is not JavaScript
You post Java inside an HTML page, it will not run
Java need to be compiled, and you need a server like Tomcat to run it

You can try to simply use JSP which will let you to embed Java code directly in the page (no need to compile), the servlet container (Tomcat, JBoss,...) will compile your page on the fly

So if your page is a JSP file and you're using a servlet container, you may use something like that :
<%@ page import="java.sql.*" %> 
<html> 
<head></head> 
<body>
<% 
	try 
	{  
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		String connectionUrl = "jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;" + "databaseName=dwhomesdborlando;user=xxx;password=xxx;";
		Connection con = DriverManager.getConnection(connectionUrl);
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT last_name, first_name, phone FROM employees");	
		while (rs.next()) 
		{
			String x = rs.getString("last_name") + ", " + rs.getString("first_name") + " - " + rs.getString("phone");
			System.out.println(x);
		}
	}
	catch (SQLException e)
	{
		System.out.println("SQL Exception: "+ e.toString());
	}
	catch (ClassNotFoundException cE)
	{
		System.out.println("Class Not Found Exception: "+ cE.toString());
	}
%>
</body> 
</html> 

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question