当前页面: 开发资料首页 → Java 专题 → applet中使用JDBC访问数据库
applet中使用JDBC访问数据库
摘要: applet中使用JDBC访问数据库
最近,在这里看到了几篇关于在applet中使用JDBC访问数据库的帖子,觉得大侠们有的回复很绝对,说applet不能访问数据库,不敢苟同,于是参照Sun的网上教程"[url=http://cn.sun.com/developers/onlineTraining/Programming/BasicJava1/data.html]Java程序设计语言基础: 实践指南,第1部分[/url]",做了一些实验,总结出来与大家分享,也请多多指教。
实验环境
DB Server #1: Solairs 8 + Oracle 8.1.7 + apache
DB Server #2: Solairs 8 + Oracle 8.1.7
Client: Windows2000 SP4 + J2SE1.4.1 + Oracle JDBC Driver
实验中使用的数据表的结构
[code:1:8c94ca30e9]
TABLE DBA (
TEXT varchar2(100),
primary key (TEXT)
)
[/code:1:8c94ca30e9]
applet的源代码
[code:1:8c94ca30e9]
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import java.sql.*;
import java.net.*;
import java.io.*;
public class DbaAppl extends Applet implements ActionListener {
JLabel text, clicked;
JButton button, clickButton;
JTextField textField;
private boolean _clickMeMode = true;
private Connection c;
final static private String _driver = "oracle.jdbc.driver.OracleDriver";
final static private String _url = "jdbc:oracle:thin:username/password@(description=(address_list=(address=(protocol=tcp)(host=developer)(port=1521)))(source_route=yes)(connect_data=(sid=ansid)))";
public void init(){
setBackground(Color.white);
text = new JLabel("Text to save to file:");
clicked = new JLabel("Text retrieved from file:");
button = new JButton("Click Me");
button.addActionListener(this);
clickButton = new JButton("Click Again");
clickButton.addActionListener(this);
textField = new JTextField(20);
setLayout(new BorderLayout());
setBackground(Color.white);
add(BorderLayout.NORTH, text);
add(BorderLayout.CENTER, textField);
add(BorderLayout.SOUTH, button);
}
public void start(){
System.out.println("Applet starting.");
}
public void stop(){
System.out.println("Applet stopping.");
}
public void destroy(){
System.out.println("Destroy method called.");
}
public void actionPerformed(ActionEvent event){
try{
Class.forName (_driver);
c = DriverManager.getConnection(_url);
}catch (java.lang.ClassNotFoundException e){
System.out.println("Cannot find driver");
System.out.println(e.getMessage());
}catch (java.sql.SQLException e){
System.out.println("Cannot get connection");
System.out.println(e.getMessage());
}
Object source = event.getSource();
if(source == button){
if(_clickMeMode){
JTextArea displayText = new JTextArea();
try{
//Write to database
String theText = textField.getText();
Statement stmt = c.createStatement();
String updateString = "INSERT INTO dba VALUES ('" + theText + "')";
int count = stmt.executeUpdate(updateString);
//Read from database
ResultSet results = stmt.executeQuery("SELECT TEXT FROM dba ");
while(results.next()){
String s = results.getString("TEXT");
displayText.append(s + "\n");
}
stmt.close();
}catch(java.sql.SQLException e){
System.out.println("Cannot create SQL statement");
System.out.println(e.getMessage());
}
//Display text read from database
text.setText("Text retrieved from file:");
button.setText("Click Again");
_clickMeMode = false;
//Display text read from database
} else {
text.setText("Text to save to file:");
textField.setText("");
button.setText("Click Me");
_clickMeMode = true;
}
}
}
}
[/code:1:8c94ca30e9]
运行此applet的html文件DbaAppl.html
[code:1:8c94ca30e9]
<body>