Example of Applet JDBC

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Example of Applet JDBC

This is a example of Java program.

[edit] Java Source Code

  • Package: matthews.mark
  • File: AppletJDBCDrop.java
package matthews.mark;
 
/*
 
 MySQL and Java Developer's Guide
 
 Mark Matthews, Jim Cole, Joseph D. Gradecki
 Publisher Wiley,
 Published February 2003, 
 ISBN 0471269239
 
 */
 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
 
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
 
public class AppletJDBCDrop extends JApplet implements ActionListener {
 
    private Connection connection;
 
    private JList tableList;
 
    private JButton dropButton;
 
    public void init() {
	Connection connection;
	try {
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    connection = DriverManager
		    .getConnection("jdbc:mysql://192.168.1.25/accounts?user=spider&password=spider");
	} catch (Exception connectException) {
	    connectException.printStackTrace();
	}
 
	Container c = getContentPane();
	tableList = new JList();
	loadTables();
	c.add(new JScrollPane(tableList), BorderLayout.NORTH);
 
	dropButton = new JButton("Drop Table");
	dropButton.addActionListener(this);
	c.add(dropButton, BorderLayout.SOUTH);
    }
 
    public void actionPerformed(ActionEvent e) {
	try {
	    Statement statement = connection.createStatement();
	    ResultSet rs = statement.executeQuery("DROP TABLE "
		    + tableList.getSelectedValue());
	} catch (SQLException actionException) {
	}
    }
 
    private void loadTables() {
	Vector v = new Vector();
	try {
	    Statement statement = connection.createStatement();
	    ResultSet rs = statement.executeQuery("SHOW TABLES");
 
	    while (rs.next()) {
		v.addElement(rs.getString(1));
	    }
	    rs.close();
	} catch (SQLException e) {
	}
	v.addElement("acc_acc");
	v.addElement("acc_add");
	v.addElement("junk");
	tableList.setListData(v);
    }
}
 
/*
 * 
 * <html> <applet code="Drop.class" width=200 height=200> </applet> </html>
 */

[edit] What Result You Can Get

Run the program, you will get:




[edit] Required External Library for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools