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

📄 converterdatabase.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import javagently.*;

class ConverterDatabase extends Frame
  implements ActionListener, ItemListener {

  /* The ConverterDatabase Program     by J M Bishop  Dec 1998
   * -----------------------------     Display version July 1999
   *                                   GUI version July 1999
   *                     B Worrall  Database version August 2000
   * Keeps the exchange rates from one currency into
   * many others and enables currency exchanges to be
   * estimated.
   *
   * Illustrates the use of jdbc connection to a database.
   */

  private Connection conn;

  Choice    fromChoice, toChoice;
  TextField amountField;
  TextArea  resultField;
  int       amount = 1000;
  String    toCountry, fromCountry;
  Button    goButton;

  public ConverterDatabase () throws SQLException {
    // First set up GUI interface, but do not
    // setVisible.
    Panel p = new Panel (new BorderLayout());

      // left hand side panel
      Panel q = new Panel();
        q.add ("North",new Label ("From"));
        fromChoice = new Choice();
          fromChoice.addItemListener (this);
          q.add("Center",fromChoice);
        p.add ("West", q);
      // right hand side panel
      q = new Panel();
        q.add ("North",new Label ("To"));
        toChoice = new Choice();
          toChoice.addItemListener (this);
          q.add("Center",toChoice);
        p.add ("East",q);
      // Centre panel
      q = new Panel(new BorderLayout());
        Panel r = new Panel();
          r.add(new Label("Amount"));
          amountField = new TextField("1000  ");
            amountField.addActionListener(this);
            r.add(amountField);
            q.add("North",r);
          resultField = new TextArea(8,20);
            q.add ("Center",resultField);
          goButton = new Button ("Convert");
            goButton.addActionListener(this);
            q.add("South",goButton);
        p.add("Center",q);
    add(p);
    setTitle("Currency Converter");
    setSize(610,300);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    // Call readIn function to open the connection
    // to the database, and to fill the Choice lists
    // with the country names.
    readIn();
  }

  public void actionPerformed (ActionEvent e) {
    if (e.getSource() == amountField)
      amount = (int) Integer.parseInt(amountField.getText().trim());
    else
    if (e.getSource() == goButton)
      transaction();
  }

  public void itemStateChanged(ItemEvent e) {
    String s = (String) e.getItem();
    if (e.getItemSelectable() == fromChoice)
      fromCountry = s;
    else toCountry = s;
  }

  void transaction () {
    // Called when the go button is pressed
    try {
      // Create a statement to query the fromCountry
      // details for the conversion.  Then execute the
      // query, saving the resuls in rs.
      Statement s1 = conn.createStatement();
      String command = "SELECT Country,Curr,Conv FROM rates " +
        "WHERE Country = '" + fromCountry +"'";
      ResultSet rs = s1.executeQuery(command);
      rs.next();

      // As above, but for toCountry, and results stored
      // in rs2.
      Statement s2 = conn.createStatement();
      command = "SELECT Country, Curr, Conv FROM rates " +
        "WHERE Country = '" + toCountry + "'";
      ResultSet rs2 = s2.executeQuery(command);
      rs2.next();

      // Add the conversion text to the resultField.
      // Notice how database methods are used to access
      // the data, as opposed to using a Hashtable's get
      // method.
      resultField.append(amount+" "+rs.getString(1)+" "+
        rs.getString(2)+
        "\n in "+rs2.getString(1)+" "+rs2.getString(2)+"\n was "+
        Stream.format(amount/rs.getDouble(3)*
          rs2.getDouble(3),10,3)+"\n\n");
    } catch (SQLException q) {
      q.printStackTrace();
      System.exit(0);
    }
  }

  void readIn() throws SQLException {
    // called at the end of the constructor
    // Open a connection with the database
    conn = DriverManager.getConnection("jdbc:odbc:rates");

    // Do SQL query to get all data from the table
    Statement s = conn.createStatement();
    String command = "SELECT Country FROM rates";
    ResultSet rs = s.executeQuery(command);

    // Iterate through rows in the table, adding the
    // necessary country names to the Choice lists
    while (rs.next()) {
	  String temp = rs.getString(1).trim();
      toChoice.addItem(temp);
      fromChoice.addItem(temp);
    }

    // Now we can set the Frame visible
    setVisible(true);
  }

  public static void main(String[] args) {

    // Load the JDBC-ODBC Bridge driver
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (ClassNotFoundException c) {
      System.out.println("Error loading JDBC-ODBC driver.");
      System.exit(0);
    }
    try {
      new ConverterDatabase();
    } catch (SQLException s) {
      s.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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