Solved

Display Java output in HTML

Posted on 2013-10-31
2
380 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now