Solved

Display Java output in HTML

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

635 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