⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applet1.java~10~

📁 Java对数据库的操作
💻 JAVA~10~
字号:
package test;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.sql.*;
import com.borland.dx.sql.dataset.Database;
import com.borland.dx.sql.dataset.*;
import test.conn.db;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */


public class Applet1 extends Applet {
    //
    db conn=new db();

    //
    boolean isStandalone = false;
    BorderLayout borderLayout1 = new BorderLayout();
    JTextField jTextField1 = new JTextField();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JTextArea jTextArea1 = new JTextArea();
    public String JDriver="sun.jdbc.odbc.JdbcOdbcDriver";
    public String conURL="jdbc:odbc:test";
    public Connection con;
    public Statement s;
    public String query;
    public ResultSet rs;
    Database database1 = new Database();

    //Get a parameter value
    public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
                (getParameter(key) != null ? getParameter(key) : def);
    }

    //Construct the applet
    public Applet1() {
    }

    //Initialize the applet
    public void init() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Component initialization
    private void jbInit() throws Exception {
        this.setLayout(null);
        jTextField1.setText("jTextField1");
        jTextField1.setBounds(new Rectangle(66, 30, 224, 22));
        jButton1.setBounds(new Rectangle(82, 67, 56, 24));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new Applet1_jButton1_actionAdapter(this));
        jButton2.setBounds(new Rectangle(156, 66, 64, 26));
        jButton2.setText("jButton2");
        jButton2.addActionListener(new Applet1_jButton2_actionAdapter(this));
        jTextArea1.setText("jTextArea1");
        jTextArea1.setBounds(new Rectangle(63, 117, 276, 170));
        database1.setConnection(new ConnectionDescriptor("jdbc:odbc:test", "",
                "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
        this.add(jTextField1);
        this.add(jButton1);
        this.add(jButton2);
        this.add(jTextArea1);
        try
        {
            Class.forName(JDriver);
        }
        catch(java.lang.ClassNotFoundException e){
            System.out.println("ForName:"+e.getMessage());
        }
        this.opendb();
    }
    public boolean opendb(){
        boolean bz;
        bz=true;
        try{
            con=DriverManager.getConnection(conURL);
            s=con.createStatement();
        }
        catch(SQLException e){
            System.out.println("SQLException:"+e.getMessage());
            bz=false;
        }
        return bz;

    }
    public void addsql(String dbsql){
        this.query=dbsql;
    }
    public boolean updatedb(){
        boolean bz;
        bz=true;
        try{
            s.executeUpdate(query);
        }
        catch(SQLException e){
            System.out.println("SQLException:"+e.getMessage());
            bz=false;
        }
        return bz;
    }
    public boolean querydb(){
        boolean bz;
        bz=true;
        try{
            rs=s.executeQuery(query);
        }
        catch(SQLException e){
            System.out.println("SQLException:"+e.getMessage());
            bz=false;
        }
        return bz;
    }
    public boolean closedb(){
        boolean bz;
        bz=true;
        try{
            s.close();
            con.close();
        }
        catch(SQLException e){
            System.out.println("SQLException:"+e.getMessage());
            bz=false;
        }
        return bz;
    }


    //Get Applet information
    public String getAppletInfo() {
        return "Applet Information";
    }

    //Get parameter info
    public String[][] getParameterInfo() {
        return null;
    }

    public void jButton1_actionPerformed(ActionEvent e) {
           this.closedb();
           this.opendb();
           this.addsql(jTextField1.getText());
           this.updatedb();
           this.closedb();

    }

    public void jButton2_actionPerformed(ActionEvent e) {
        this.closedb();
        this.opendb();
        this.addsql(jTextField1.getText());
        this.querydb();
        try{
            jTextArea1.setText("");
            while(this.rs.next()){
                jTextArea1.append(rs.getString("id")+"\t"+rs.getString("name")+"\t"+rs.getInt("score")+"\n");
            }
        }
        catch(SQLException f){
            System.out.println("SQLException:"+f.getMessage());
        }
        this.closedb();
    }
}


class Applet1_jButton2_actionAdapter implements ActionListener {
    private Applet1 adaptee;
    Applet1_jButton2_actionAdapter(Applet1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class Applet1_jButton1_actionAdapter implements ActionListener {
    private Applet1 adaptee;
    Applet1_jButton1_actionAdapter(Applet1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -