Solved

Display Java output in HTML

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

19 Experts available now in Live!

Get 1:1 Help Now