JSP虚拟主机,jsp空间,java空间,java虚拟空间JSP虚拟主机,jsp空间,java空间,java虚拟空间

一个Applet(JApplet)访问数据库的例子



作者:    文章来源:
发布日期:2007年02月23日
用JApplet做的Applet和一般的Applet有些不同。主要是因为Swing库类的单线程原则,所以当JApplet的界面生成后,由其它线程(一般是消息分发线程)试图改变界面(如setText())可能会导致问题发生。这是可以借助SwingUtilities.invokeLater()或SwingUtilities.invokeAndWait()来解决。下面是一个用JApplet访问数据库的例子。

DBApplet.java
======================================================================
import java.sql.*;
import javax.swing.*;

public class DBApplet extends javax.swing.JApplet {

final static private String[] jdbcDriver = {
"com.informix.jdbc.IfxDriver",
"sun.jdbc.odbc.JdbcOdbcDriver",
"com.borland.datastore.jdbc.DataStoreDriver",
"com.sybase.jdbc.SybDriver",
"oracle.jdbc.driver.OracleDriver",
"COM.ibm.db2.jdbc.net.DB2Driver",
"interbase.interclient.Driver",
"weblogic.jdbc.mssqlserver4.Driver"
};

private boolean connected = false;
private Connection connection = null;
private ResultSet rs = null;
private String query = null;
private String rsLine = null;
private String driver = null;
private String url = null;
private String user = null;
private String password = null;

public DBApplet() {
initComponents ();
postInit();
}

private void postInit() {
for (int i = 0; i < jdbcDriver.length; i++) {
cbDriver.addItem(jdbcDriver[i]);
}
}

private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
taResponse = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
tfSql = new javax.swing.JTextField();
btnExecute = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jPanel4 = new javax.swing.JPanel();
cbDriver = new javax.swing.JComboBox();
jLabel7 = new javax.swing.JLabel();
tfUrl = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
tfUser = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
tfPassword = new javax.swing.JTextField();
btnConnect = new javax.swing.JButton();
btnDisconnect = new javax.swing.JButton();
setFont(new java.awt.Font ("Verdana", 0, 12));

jScrollPane1.setViewportView(taResponse);

getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

getContentPane().add(jPanel2, java.awt.BorderLayout.EAST);

jLabel6.setText("SQL:");
jPanel1.add(jLabel6);

tfSql.setPreferredSize(new java.awt.Dimension(300, 21));
jPanel1.add(tfSql);

btnExecute.setText("Execute Query");
btnExecute.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExecuteActionPerformed(evt);
}
});
jPanel1.add(btnExecute);

getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

jPanel3.setPreferredSize(new java.awt.Dimension(550, 100));
jPanel3.setMinimumSize(new java.awt.Dimension(550, 100));
jPanel3.setMaximumSize(new java.awt.Dimension(550, 100));

jLabel3.setText("JDBC Driver:");
jPanel3.add(jLabel3);

jPanel3.add(jPanel4);

cbDriver.setPreferredSize(new java.awt.Dimension(450, 26));
cbDriver.setMinimumSize(new java.awt.Dimension(100, 26));
jPanel3.add(cbDriver);

jLabel7.setText("Database URL:");
jPanel3.add(jLabel7);

tfUrl.setPreferredSize(new java.awt.Dimension(450, 21));
jPanel3.add(tfUrl);

jLabel9.setText("User:");
jPanel3.add(jLabel9);

tfUser.setPreferredSize(new java.awt.Dimension(100, 21));
jPanel3.add(tfUser);

jLabel10.setText("Password:");
jPanel3.add(jLabel10);

tfPassword.setPreferredSize(new java.awt.Dimension(100, 21));
jPanel3.add(tfPassword);

btnConnect.setPreferredSize(new java.awt.Dimension(89, 27));
btnConnect.setMaximumSize(new java.awt.Dimension(89, 27));
btnConnect.setText("Connect");
btnConnect.setMinimumSize(new java.awt.Dimension(89, 27));
btnConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConnectActionPerformed(evt);
}
});
jPanel3.add(btnConnect);

btnDisconnect.setText("Disconnect");
btnDisconnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDisconnectActionPerformed(evt);
}
});
jPanel3.add(btnDisconnect);


getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);

}

private void btnExecuteActionPerformed(java.awt.event.ActionEvent evt) {
if (!connected) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("No database connected. ");
}
}
);
} else {
if (connection == null) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Database connection error. ");
}
}
);
} else {
try {
query = tfSql.getText();
Statement stmt = connection.createStatement();
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Executing query: " + query + " ");
}
}
);
rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
int i;
rsLine = " ";
while (rs.next()) {
for (i = 1; i <= count; i++) {
rsLine += rs.getString(i) + " ";
}
rsLine += " ";
}
rsLine += " ";
stmt.close();
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append(rsLine);
}
}
);
} catch (SQLException e) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Query failed. ");
}
}
);
e.printStackTrace();
}
}
}
}

private void btnDisconnectActionPerformed(java.awt.event.ActionEvent evt) {
if (connected) {
try {
if (connection != null) {
connection.close();
connection = null;
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Database disconnected. ");
}
}
);
}
} catch (SQLException e) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Database disconnecting error. ");
}
}
);
e.printStackTrace();
}
connected = false;
driver = null;
url = null;
user = null;
password = null;
} else {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Database already disconnected. ");
}
}
);
}
}

private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
if (connected) {
taResponse.append("Database already connected. ");
} else {

driver = (String) cbDriver.getSelectedItem();
url = tfUrl.getText();
user = tfUser.getText();
password = tfPassword.getText();
try {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Using JDBC driver: " + driver + " ");
}
}
);
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, user, password);
if (connection != null) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Database " + url + " connected. ");
}
}
);
connected = true;
}
} catch (ClassNotFoundException e) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Cannot load the driver. ");
}
}
);
e.printStackTrace();
} catch (SQLException e) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
taResponse.append("Cannot connect to the database. ");
}
}
);
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea taResponse;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField tfSql;
private javax.swing.JButton btnExecute;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JComboBox cbDriver;
private javax.swing.JLabel jLabel7;
private javax.swing.JTextField tfUrl;
private javax.swing.JLabel jLabel9;
private javax.swing.JTextField tfUser;
private javax.swing.JLabel jLabel10;
private javax.swing.JTextField tfPassword;
private javax.swing.JButton btnConnect;
private javax.swing.JButton btnDisconnect;
}

dbapplet.html
======================================================================


DB applet



DB applet


The Applet tag in this file contains a CODEBASE entry that must be set
to point to a directory containing the Java classes from the Thin JDBC
distribution *and* the compiled JdbcApplet.class.



width="550" height="500" align="baseline"
codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">






align="baseline" code="DBApplet.class" codebase="." archive="classes111.zip"
pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
<br /> </COMMENT> <br /> No Java 2 SDK, Standard Edition v 1.3 support for APPLET!! <br />







我用的是数据库是Oracle,大家可以根据自己情况改改。

Copyright © 2002-2012 JSPCN.net. All rights reserved.
JSP中文网    备案号:粤ICP备09171188号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号