import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JDBC_Applet extends JApplet{
private JScrollPane jScrollPane;
private JTextArea taResponse;
private JPanel jPanel1,jPanel2,jPanel3,jPanel4;
private JLabel jLabel3,jLabel6,jLabel7,jLabel9,jLabel10;
private JTextField tfSql,tfUrl,tfUser,tfPassword;
private JButton btnExecute,btnConnect,btnDisconnect;
private JComboBox cbDriver;
//-----------------------------------------------------------------------------------
final static private String[] jdbcDriver = { "com.mysql.jdbc.Driver",
"com.informix.jdbc.IfxDriver", "sun.jdbc.odbc.JdbcOdbcDriver",
"com.borland.datastore.jdbc.DataStoreDriver","org.apache.derby.jdbc.EmbeddedDriver",
"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 con = 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 JDBC_Applet(){
Com_init();//窗体内容
Post_init();//下拉菜单
}
public void Post_init(){//下拉菜单的内容
for(int i= 0; i < jdbcDriver.length; i++){
cbDriver.addItem(jdbcDriver[i]);
}
}
private void Com_init() {
jScrollPane = new JScrollPane();//面板
taResponse = new JTextArea();//response:响应 文本域
// 显示sql查询结果的文本区域
jPanel2 = new JPanel();
jPanel1 = new JPanel();
jLabel6 = new JLabel();
tfSql = new JTextField();
// 输入sql语句的文本区域。
btnExecute = new JButton();
// sql执行按钮
jPanel3 = new JPanel();
jLabel3 = new JLabel();
jPanel4 = new JPanel();
cbDriver = new JComboBox();
// 数据库驱动下拉框
jLabel7 = new JLabel();
tfUrl = new JTextField();
// 连接数据库的url
jLabel9 = new JLabel();
tfUser = new JTextField();
// 录入用户名的文本框
jLabel10 = new JLabel();
tfPassword = new JTextField();
// 录入用户密码的文本框
btnConnect = new JButton();
// 连接button
btnDisconnect = new JButton();
// 释放数据库连接的button
setFont(new Font("Verdana", 0, 25));
// 设置字体
jScrollPane.setViewportView(taResponse);
getContentPane().add(jScrollPane, BorderLayout.CENTER);
getContentPane().add(jPanel2, BorderLayout.EAST);
jLabel6.setText("SQL:");
// label Sql:
jPanel1.add(jLabel6);
tfSql.setPreferredSize(new Dimension(300, 21));
jPanel1.add(tfSql);
btnExecute.setText("执行语句");
btnExecute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnExecuteActionPerformed(evt);
}
});
jPanel1.add(btnExecute);
// 将jPanel1放到上边
getContentPane().add(jPanel1, BorderLayout.SOUTH);
jPanel3.setPreferredSize(new Dimension(550, 100));
jPanel3.setMinimumSize(new Dimension(550, 100));
jPanel3.setMaximumSize(new Dimension(550, 100));
jLabel3.setText("JDBC 驱动:");
jPanel3.add(jLabel3);
// label JDBC Driver:
jPanel3.add(jPanel4);
cbDriver.setPreferredSize(new Dimension(450, 26));
cbDriver.setMinimumSize(new Dimension(100, 26));
jPanel3.add(cbDriver);
jLabel7.setText("数据库地址:");
jPanel3.add(jLabel7);
// label Database URL:
tfUrl.setPreferredSize(new Dimension(450, 21));
jPanel3.add(tfUrl);
jLabel9.setText("用户名:");
jPanel3.add(jLabel9);
// label User:
tfUser.setPreferredSize(new Dimension(100, 21));
jPanel3.add(tfUser);
jLabel10.setText("密码:");
jPanel3.add(jLabel10);
tfPassword.setPreferredSize(new Dimension(100, 21));
jPanel3.add(tfPassword);
btnConnect.setPreferredSize(new Dimension(89, 27));
btnConnect.setMaximumSize(new Dimension(89, 27));
btnConnect.setText("连接");
btnConnect.setMinimumSize(new Dimension(89, 27));
btnConnect.addActionListener(new ActionListener() {//连接按钮事件
public void actionPerformed(ActionEvent evt) {
btnConnectActionPerformed(evt);
}
});
jPanel3.add(btnConnect);
btnDisconnect.setText("断开");
btnDisconnect.addActionListener(new ActionListener() {//断开按钮事件
public void actionPerformed(ActionEvent evt) {
btnDisconnectActionPerformed(evt);
}
});
jPanel3.add(btnDisconnect);
// 放在布局管理器的北边
getContentPane().add(jPanel3, BorderLayout.NORTH);
}
//-----------------------------------------------------------------------------------
private void btnConnectActionPerformed(ActionEvent be){//连接事件细节
if(connected){
taResponse.append("数据库已连接.\n");
}else{
driver = (String)cbDriver.getSelectedItem();//把下拉菜单的内容赋值给driver
url = tfUrl.getText();//用户输入数据库地址
user = tfUser.getText();
password = tfPassword.getText();
try{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("Using JDBC driver: " + driver + "\n");
}
});
// 注册数据库驱动 通过jdbc的方式连接数据库
Class.forName(driver).newInstance();
//con = DriverManager.getConnection(url,user,password);
con = DriverManager.getConnection(url);
if(con != 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 se){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("不能连接数据库.\n");
}
});
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
//-----------------------------------------------------------------------------------
private void btnDisconnectActionPerformed(ActionEvent e){//断开事件细节
if(connected){
try{
if(con != null){
con.close();
con = null;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("数据库断开.\n");
}
});
}
}catch(SQLException se){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("数据库断开错误.\n");
}
});
se.printStackTrace();
}
connected = false;
driver = null;
url = null;
user = null;
password = null;
}else{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("数据库已断开.\n");
}
});
}
}
//-----------------------------------------------------------------------------------
private void btnExecuteActionPerformed(ActionEvent evt) {//执行SQL语句处理
if(!connected){//没有连接的情况
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("没有可连接的数据库.\n");
}
});
}else{
if(con == null){//con为空的情况
SwingUtilities.invokeLater(new Runnable() {
public void run() {
taResponse.append("连接数据库错误.\n");
}
});
}else{
try{
query = tfSql.getText();//从文本域里获得用户输入
Statement stmt = con.createStatement();//创建一个声明
SwingUtilities.invokeLater(new Runnable(){
public void run(){
taResponse.append("正在查询:" + query + "\n");
}
});
rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = 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) {
JOptionPane.showMessageDialog(null, "系统不支持的字符");
}
}
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();
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("通过JApplet连接数据库...");
JDBC_Applet applet = new JDBC_Applet();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(applet);
applet.init();
frame.setSize(400, 300);
frame.setVisible(true);
}
}