用JApplet作连接数据库工具

20. Database 专栏收录该内容
18 篇文章 0 订阅

 

 

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.sql.ResultSetMetaData;


public class JDBC_JApplet extends JApplet{

	final static private String[] jdbcDriver = { "com.mysql.jdbc.Driver",
		"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" ,"org.apache.derby.jdbc.EmbeddedDriver"};

private boolean connected = false;

// 判断是否连接成功
private Connection connection = null;

private ResultSet rs = null;

private String query = null;

// 查询的sql语句

private String rsLine = null;

private String driver = null;

// 数据库驱动程序

private String url = null;

// 数据库连接url

private String user = null;

// 数据库用户名

private String password = null;

// 数据库用户密码
public JDBC_JApplet() {
	Com_init();
	Post_Init();
}

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

private void Com_init() {

	jScrollPane1 = new javax.swing.JScrollPane();
	taResponse = new javax.swing.JTextArea();
	// 显示sql查询结果的文本区域
	jPanel2 = new javax.swing.JPanel();
	jPanel1 = new javax.swing.JPanel();
	jLabel6 = new javax.swing.JLabel();
	tfSql = new javax.swing.JTextField();
	// 输入sql语句的文本区域。
	btnExecute = new javax.swing.JButton();
	// sql执行按钮
	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();
	// 连接数据库的url
	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();
	// 连接button
	btnDisconnect = new javax.swing.JButton();
	// 释放数据库连接的button
	setFont(new Font("Verdana", 0, 25));
	
	// 设置字体
	jScrollPane1.setViewportView(taResponse);

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

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

	jLabel6.setText("SQL:");
	// label Sql:
	jPanel1.add(jLabel6);

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

	btnExecute.setText("执行语句");
	btnExecute.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			btnExecuteActionPerformed(evt);
		}
	});
	jPanel1.add(btnExecute);
	// 将jPanel1放到上边
	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 驱动:");
	jPanel3.add(jLabel3);
	// label JDBC Driver:
	jPanel3.add(jPanel4);

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

	jLabel7.setText("数据库地址:");
	jPanel3.add(jLabel7);
	// label Database URL:
	tfUrl.setPreferredSize(new java.awt.Dimension(450, 21));
	jPanel3.add(tfUrl);

	jLabel9.setText("用户名:");
	jPanel3.add(jLabel9);
	// label User:

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

	jLabel10.setText("密码:");
	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("连接");
	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("断开");
	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);
}
	
//执行查询的sql语句
	private void btnExecuteActionPerformed(java.awt.event.ActionEvent evt) {
		if (!connected) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					taResponse.append("没有可连接的数据库.\n");
				}
			});
		} else {
			if (connection == null) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						taResponse.append("连接数据库错误.\n");
					}
				});
			} else {
				try {
					query = tfSql.getText();
					Statement stmt = connection.createStatement();
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							taResponse.append("正在查询: " + query + "\n");
						}
					});
					rs = stmt.executeQuery(query);
					// 使用元数据
					ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();

					// 还使用了元数据来判断有多少个字段
					int count = rsmd.getColumnCount();
					int i;
					rsLine = "\n 表的结构如下:\n";
					for (int it = 1; it <= count; it++) {
						rsLine += rsmd.getColumnName(it) + " ";
					}
					rsLine += "\n";
					while (rs.next()) {
						for (i = 1; i <= count; i++) {
							String s;
							try {
								s = new String(rs.getString(i).getBytes(
										"ISO-8859-1"), "GBK");
								rsLine += s;
							} catch (UnsupportedEncodingException e) {

							}

						}
						rsLine += "\n";
					}
					rsLine += "\n";
					stmt.close();
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							taResponse.append(rsLine);
						}
					});
				} catch (SQLException e) {
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							taResponse.append("查询失败.\n");
						}
					});
					e.printStackTrace();
				}
			}
		}
	}
	protected void btnDisconnectActionPerformed(ActionEvent evt) {
		if (connected) {
			try {
				if (connection != null) {
					connection.close();
					connection = null;
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							taResponse.append("数据库断开.\n");
						}
					});
				}
			} catch (SQLException e) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						taResponse.append("数据库断开错误.\n");
					}
				});
				e.printStackTrace();
			}
			connected = false;
			driver = null;
			url = null;
			user = null;
			password = null;
		} else {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					taResponse.append("数据库已断开.\n");
				}
			});
		}
	
	}

	protected void btnConnectActionPerformed(ActionEvent evt) {
		if(connected){
			taResponse.append("数据库已经连接!.\n");
		}else{
			driver = (String) cbDriver.getSelectedItem();
			url = tfUrl.getText();
			user = tfUser.getText();
			password = tfPassword.getText();
			try{
				SwingUtilities.invokeLater(new Runnable(){
					public void run(){
						taResponse.append("使用的JDBC驱动为:" + driver + "\n");
					}
				});
				Class.forName(driver).newInstance();
				connection = DriverManager.getConnection(url,user,password);
				
				if (connection != null) {
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							taResponse.append("数据库 " + url + " 连接上了.\n");
						}
					});
					connected = true;
				}
			}catch (ClassNotFoundException e) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						taResponse.append("不能加载驱动.\n");
					}
				});
				e.printStackTrace();
			} catch (SQLException e) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						taResponse.append("不能连接数据库.\n");
					}
				});
				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;

public static void main(String[] args) {
	JFrame frame = new JFrame("通过JApplet连接数据库...");
	JDBC_Applet hwj = new JDBC_Applet();
	frame.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	});
	frame.getContentPane().add(hwj);
	hwj.init();
	frame.setSize(400, 300);
	frame.setVisible(true);
}

}


 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值