import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class MyJDBCTest extends Frame implements ActionListener{
TextField tf=new TextField(120);
Button bn=new Button("退出");
TextArea myTextArea;
Connection conn;
public MyJDBCTest() {
super("Java与Oracle连接");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setLayout(new FlowLayout());
add(new Label("输入要执行的语句:"));
add(tf);
Button b1=new Button("查询");
add(b1);
b1.addActionListener(this);
Button b2=new Button("插入");
add(b2);
b2.addActionListener(this);
Button b3=new Button("更新");
add(b3);
b3.addActionListener(this);
Button b4=new Button("删除");
add(b4);
b4.addActionListener(this);
Button b7=new Button("创建表");
add(b7);
b7.addActionListener(this);
add(bn);
Button b6=new Button("删除表");
add(b6);
b6.addActionListener(this);
add(bn);
Button b5=new Button("清空执行语句");
add(b5);
b5.addActionListener(this);
add(bn);
bn.addActionListener(this);
myTextArea = new TextArea(40,130);
add(myTextArea);
resize(1200,700);
show();
}
public void connectDB() throws Exception{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection( "jdbc:odbc:myoracle","scott","tiger");
}
catch (Exception e)
{
System.out.println("=====Exception : DBOper connectDB() exception: " + e.getMessage());
}
}
public Connection getConnection() throws Exception
{
return conn;
}
void displayResults(ResultSet results) throws SQLException {
ResultSetMetaData resultsMetaData = results.getMetaData();
int cols = resultsMetaData.getColumnCount();
myTextArea.setText("");
while(results.next()) {
for(int i=1;i<=cols;i++) {
if(i>0)
myTextArea.appendText("\t");
try{
myTextArea.appendText(results.getString(i));
}
catch(NullPointerException e){
}
}
myTextArea.appendText("\n");
}
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand()=="退出")
System.exit(0);
else if (e.getActionCommand()=="清空执行语句")
tf.setText("");
else
{ String queryString=tf.getText();
try{
connectDB();
}
catch (Exception et){
System.out.println("=====Exception : DBOper connectDB() exception: " + et.getMessage());
}
try {
Statement myStmt = getConnection().createStatement();
if (e.getActionCommand()=="插入"||e.getActionCommand()=="更新"||e.getActionCommand()=="删除")
{
myStmt.executeUpdate(queryString);
myTextArea.appendText("数据已经"+e.getActionCommand()+"请重新查询结果\n");
}
else if ( e.getActionCommand()=="创建表"||e.getActionCommand()=="删除表")
{
myStmt.executeUpdate(queryString);
myTextArea.appendText(e.getActionCommand()+"成功\n");
}
else
{
myTextArea.appendText("数据正在查询中……请等待!");
try {
(new Thread()).sleep(3000);
}
catch (InterruptedException es){}
ResultSet myResults = myStmt.executeQuery(queryString);
displayResults(myResults);
myStmt.close();
myResults.close();
}
}
catch (Exception eg){
System.out.println("=====Exception : DBOper exception: " + eg.getMessage());
}
}
}
public static void main(String argv[]){
MyJDBCTest myJDBC =new MyJDBCTest();
try
{
myJDBC.getConnection().close();
}
catch (Exception e){
System.out.println("=====Exception : DBOper getConnection() exception: " + e.getMessage());
}
}
}