It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes How to run java program in web-browser? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to run java program in web-browser?" Watch "How to run java program in web-browser?" New topic

How to run java program in web-browser?

Akshay Vyas

Joined: Mar 14, 2003
Posts: 1
I have this following program. I want to run this java program on web-browser. (IE or Netscape). How can I do that?
import java.awt.*;
import java.awt.event.*; // For event classes
import javax.swing.*; // For swing components
import javax.swing.table.*; // For the table classes
import java.sql.*; // For JDBC classes

public class InteractiveSQL extends JFrame implements ActionListener
public static void main(String[] args)
{ // Create the application object
InteractiveSQL theApp = new InteractiveSQL("sun.jdbc.odbc.JdbcOdbcDriver",
"jdbc dbc:addr_book",
public InteractiveSQL(String driver, String url,
String user, String password)
super("Interactive SQL"); // Call base constructor
setBounds(0,0,400,300); // Set window bounds
setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Close window operation
addWindowListener(new WindowHandler()); // Listener for window close
// Add the input for SQL statements at the top
command.setToolTipText("Key SQL command and press enter");
getContentPane().add(command, BorderLayout.NORTH);
// Add the status reporting area at the bottom
getContentPane().add(status, BorderLayout.SOUTH);
//Create the menubar from the menu items
JMenu fileMenu = new JMenu("File"); // Create file menu
fileMenu.setMnemonic('F'); // Create shortcut
fileMenu.add(clearQueryItem); // Add clear query item
fileMenu.add(exitItem); // Add exit item
menuBar.add(fileMenu); // Add menu to the menu bar
// Establish a database connection and setup the table
Class.forName(driver); // Load the driver
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
model = new ResultsModel(); // Create a table model
JTable table = new JTable(model); // Create a table from the model
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // Use scrollbars
resultsPane = new JScrollPane(table); // Create scrollpane for table
getContentPane().add(resultsPane, BorderLayout.CENTER);
catch(ClassNotFoundException cnfe)
System.err.println(cnfe); // Driver not found
catch (SQLException sqle)
System.err.println(sqle); // Error connection to database

public void actionPerformed(ActionEvent e)
Object src = e.getSource();
if (src == command)
else if (src == clearQueryItem)
else if (src == exitItem)
public void executeSQL()
String qry = command.getText();
if (qry == null)
status.setText("Total " + model.getRowCount() + "rows fetched.");
catch (SQLException sqle)
class WindowHandler extends WindowAdapter
// Handler for window closing event
public void WindowClosing(WindowEvent e)
dispose(); // Release the window resources
System.exit(0); // End the application
JTextField command = new JTextField(); // Input area for SQL
JTextArea status = new JTextArea(3,1); // Output area for status and errors
JScrollPane resultsPane;
JMenuBar menuBar = new JMenuBar(); // The menubar
JMenuItem clearQueryItem = new JMenuItem("Clear Query"); // Clear SQL item
JMenuItem exitItem = new JMenuItem("Exit"); // Exit item
Connection connection; // Connection to the database
Statement statement; // Statement object for queries
ResultsModel model; // Table model for resultset
basha khan
Ranch Hand

Joined: Jan 18, 2002
Posts: 87
use applet instead of Frame.
IE does'nt support JApplet.make sure which browser is using.
my suggestien is change ur JFrame to Panel.then u can make the code compatable for both JFrames and applets.
I agree. Here's the link:
subject: How to run java program in web-browser?
It's not a secret anymore!