Solved

Display Java output in HTML

Posted on 2013-10-31
2
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
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)

733 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