Forums Register Login

How to run java program in web-browser?

+Pie Number of slices to send: Send
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",
"guest",
"guest");
}
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");
command.addActionListener(this);
getContentPane().add(command, BorderLayout.NORTH);
// Add the status reporting area at the bottom
status.setLineWrap(true);
status.setWrapStyleWord(true);
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
clearQueryItem.addActionListener(this);
exitItem.addActionListener(this);
fileMenu.add(clearQueryItem); // Add clear query item
fileMenu.add(exitItem); // Add exit item
menuBar.add(fileMenu); // Add menu to the menu bar
setJMenuBar(menuBar);
// Establish a database connection and setup the table
try
{
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
}
pack();
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if (src == command)
executeSQL();
else if (src == clearQueryItem)
command.setText("");
else if (src == exitItem)
{
dispose();
System.exit(0);
}
}
public void executeSQL()
{
String qry = command.getText();
if (qry == null)
return;
try
{
model.setResultSet(statement.executeQuery(qry));
status.setText("Total " + model.getRowCount() + "rows fetched.");
}
catch (SQLException sqle)
{
status.setText(sqle.getMessage());
}
}
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
}
+Pie Number of slices to send: Send
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.
basha
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6050 times.
Similar Threads
Creating one class from another
sql query result into a JTable
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
Swing GUI Interface Question
JDBC (ResultsModel)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:05:05.