nwpulq

2009年4月17日

FileOutput类---载自《java软件开发》Russel Winder & Graham Roberts

import java.io.*;
/**
 * A simple output class to write values to a file of characters.
 * If any file errors occur, methods in this class will display
 * an error message and terminate the program.
 *
 * @version 1.1 1999.09.10
 * @author Graham Roberts
 * @author Russel Winder
 *
 */
public class FileOutput {
    /**
     * Instance variables to store file name.
     */
    private String filename = "";
    /**
     * Instance variables to store stream.
     */
    private BufferedWriter writer = null;
    /**
     * Construct <CODE>FileOutput</CODE> object given a file name.
     */
    public FileOutput(final String name) {
        filename = name;
        try {
            writer = new BufferedWriter(new FileWriter(filename));
        }
        catch(IOException e) {
            error("Can't open file: " + filename);
        }
    }
    /**
     * Construct <CODE>FileOutput</CODE> object given a
     * <CODE>File</CODE> object..
     */
    public FileOutput(final File file) {
        filename = file.getName();
        try {
            writer = new BufferedWriter(new FileWriter(filename));
        }
        catch(IOException e) {
            error("Can't open file: " + filename);
        }
    }
    /**
     * Close the file when finished
     */
    public final synchronized void close() {
        try {
            writer.close();
        }
        catch(IOException e) {
            error("Can't close file: " + filename);
        }
    }
    /**
     * Write an <CODE>int</CODE> value to a file.
     */
    public final synchronized void writeInteger(final int i) {
        try {
            writer.write(Integer.toString(i));
        }
        catch(IOException e) {
            error("writeInteger failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>long</CODE> value to a file.
     */
    public final synchronized void writeLong(final long l) {
        try {
            writer.write(Long.toString(l));
        }
        catch(IOException e) {
            error("writeLong failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>double</CODE> value to a file.
     */
    public final synchronized void writeDouble(final double d) {
        try {
            writer.write(Double.toString(d));
        }
        catch(IOException e) {
            error("writeDouble failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>float</CODE> value to a file.
     */
    public final synchronized void writeFlaot(final float f) {
        try {
            writer.write(Float.toString(f));
        }
        catch(IOException e) {
            error("writeFloat failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>char</CODE> value to a file.
     */
    public final synchronized void writeCharacter(final char c) {
        try {
            writer.write(c);
        }
        catch(IOException e) {
            error("writeCharacter failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>string</CODE> value to a file.
     */
    public final synchronized void writeString(final String s) {
        try {
            writer.write(s);
        }
        catch(IOException e) {
            error("writeString failed for file: " + filename);
        }
    }
    /**
     * Write a newline to a file.
     */
    public final synchronized void writeNewline() {
        try {
            writer.write('\n');
        }
        catch(IOException e) {
            error("writeNewline failed for file: " + filename);
        }
    }
    /**
     *  Deal with a file error
     */
    private void error(String msg) {
        System.err.println(msg);
        System.err.println("Unable to continue executing program.");
        System.exit(0);
    }
}

posted @ 2009-04-17 14:25 李奇 阅读(115) 评论(0) 编辑

DrawFrame类---摘自《Java软件开发》Russel Winder & Graham Roberts

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * This class provides a basic window, with a quit button, that can
 * be used to create simple GUIs.
 *
 * @version 1.1 2000.01.07
 * @author Graham Roberts
 * @author Russel Winder
 */
public class DrawFrame extends JFrame {
    /**
     * Override of the method to add a <CODE>JPanel</CODE> to the
     * <CODE>DrawFrame</CODE> so that the <CODE>JPanel</CODE> is
     * centred.
     */
    public void add(final JPanel panel) {
        getContentPane().add(panel, BorderLayout.CENTER);
    }
    /**
     * Terminate the program when the user wants to quit.
     */
    private void quit() {
        System.exit(0);
    }
    /**
     * construct a <CODE>DrawFrame</CODE>
     */
    public DrawFrame(final String title) {
        // Initialize the JFrame ensuring the titlebar is set.
        super(title);
        // Set up the quit button with it's listener.
        Button quitButton = new Button("Quit");
        quitButton.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                quit();
            }
        });
        // put all the buttons into a JPanel.
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(quitButton);
        // Create the contents of the frame. Use BorderLayout with the
        // top (Center) part being the drawing area and the bottom
        // (South) strip holding a quit button.
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        // Ensure that windows close events from the window manager are
        // caught and acted upon.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(final WindowEvent evt) {
                quit();
            }
        });
    }
    /**
     * Position a window in the centre of the screen.
     */
    public void centreOnScreen() {
        Dimension displaySize = getToolkit().getScreenSize();
        Dimension windowSize = getSize();
        int x = (displaySize.width - windowSize.width) / 2;
        int y = (displaySize.height - windowSize.height) / 2;
        if(x < 0) {
            x = 0;
        }
        if(y < 0) {
            y = 0;
        }
        setLocation(x,y);
    }
}

posted @ 2009-04-17 14:14 李奇 阅读(140) 评论(0) 编辑

2009年4月2日

DrawPanel类---摘自《Java软件开发》Russel Winder & Graham Roberts

import java.awt.*;
import javax.swing.*;
/**
 * A <CODE>JPanel</CODE> with a default size. Subclasses must
 * override the <CODE>paint</CODE> method which has signature.
 *
 * <PRE> public void paint(final Graphics g) </PRE>
 *
 * @version 1.0 1999.09.04
 * @author Graham Roberts
 * @author Russel Winder
 */
public class DrawPanel extends JPanel{
    /**
     * The width of the panel.
     */
    private int width = 300;
    /**
     * The height of the panel
     */
    private int height = 300;
    /**
     * Default constructor, uses the default size.
     */
    protected DrawPanel() {
        setPreferredSize(new Dimension(width, height));
    }
    /**
     * Constructor for a size determined by the user.
     */
    protected DrawPanel(final int w, final int h) {
        width = w;
        height = h;
        setPreferredSize(new Dimension(width, height));
    }
    /**
     * Accessor for the width of the panel.
     */
    public int getHeight() {
        return height;
    }
}

posted @ 2009-04-02 10:25 李奇 阅读(159) 评论(0) 编辑

2009年3月3日

FileInput类---载自《java软件开发》Russel Winder & Graham Roberts

import java.io.*;
/**
 *  A simple input class to read values from a file of characters.  If
 *  any file errors occur, methods in this class will display an error
 *  message and terminate the program.
 *
 *  @version 1.1 1999.09.10
 *  @author Graham Roberts
 *  @author Russel Winder
 */
public class FileInput {
  /**
   *  Instance variables to store the name of the file we are
   *  associated with.
   */
    private String filename = "";
    /**
     *  Instance variables for the filestream associated with the file
     *  that we are associated with.
     */
    private BufferedReader reader = null;
    /**
     *  Instance variables to store current state of EOF.
     */
    private boolean eof = false;
    /**
     *  Construct <CODE>FileInput</CODE> object given a file name.
     */
    public FileInput(final String fname) {
        filename = fname;
        try {
            reader = new BufferedReader(new FileReader(filename));
        }
        catch (FileNotFoundException e) {
            error("Can't open file: " + filename);
        }
    }
    /**
     *  Construct <CODE>FileInput</CODE> object given a
     *  <CODE>File</CODE> object.
     */
    public FileInput(final File file) {
        filename = file.getName();
        try {
            reader = new BufferedReader(new FileReader(file));
        }
        catch (FileNotFoundException e) {
            error("Can't open file: " + filename);
        }
    }
    /**
     *  Close the file when finished
     */
    public final synchronized void close() {
        try {
            reader.close();
        }
        catch(IOException e) {
            error("Can't close file: " + filename);
        }
    }
    /**
     *  Return true if the end of file has been reached.
     */
    public boolean eof() {
        return eof;
    }
    /**
     *  Read an <CODE>int</CODE> value from file.  The default
     *  return value is 0.
     */
    public final synchronized int readInteger() {
        String input = "";
        int value = 0;
        try {
            input = reader.readLine();
        }
        catch(IOException e) {
            error("readInteger failed for file: " + filename);
        }
        if(input == null) {
            eof = true;
        }
        else {
            try {
                value = Integer.parseInt(input);
            }
            catch(NumberFormatException e) {}
        }
        return value;
    }
    /**
     *  Read an <CODE>long</CODE> value from file.  The default
     *  return value is 0L.
     */
    public final synchronized long readLong() {
        String input = "";
        long value = 0L;
        try {
            input = reader.readLine();
        }
        catch(IOException e) {
            error("readLong failed for file: " + filename);
        }
        if(input == null) {
            eof = true;
        }
        else {
            try {
                value = Long.parseLong(input);
            }
            catch(NumberFormatException e) {}
        }
        return value;
    }
    /**
     *  Read an <CODE>double</CODE> value from file.  The default
     *  return value is 0.0.
     */
    public final synchronized double readDouble() {
        String input = "";
        double value = 0.0D;
        try {
            input = reader.readLine();
        }
        catch(IOException e) {
            error("readDouble failed for file: " + filename);
        }
        if(input == null) {
            eof = true;
        }
        else {
            try {
                value = Double.parseDouble(input);
            }
            catch(NumberFormatException e) {}
        }
        return value;
    }
    /**
     *  Read an <CODE>long</CODE> value from file.  The default
     *  return value is 0L.
     */
    public final synchronized float readFloat() {
        String input = "";
        float value = 0.0F;
        try {
            input = reader.readLine();
        }
        catch(IOException e) {
            error("readFloat failed for file: " + filename);
        }
        if(input == null) {
            eof = true;
        }
        else {
            try {
                value = Float.parseFloat(input);
            }
            catch(NumberFormatException e) {}
        }
        return value;
    }
    /**
     *  Read an <CODE>char</CODE> value from file.  The default
     *  return value is ' '(space)
     */
    public final synchronized char readCharacter() {
        char c = ' ';
        try {
            int n = reader.read();
            if (n == -1) {
                eof = true;
            }
            else {
                c = (char)n;
            }
        }
        catch (IOException e) {
            error("readCharacter failed for file: " + filename);
        }
        return c;
    }
    /**
     *  Read an <CODE>String</CODE> value from file.  The default
     *  return value is ""(the empty string).
     */
    public final synchronized String readString() {
        String s = "";
        try {
            s = reader.readLine();
        }
        catch (IOException e) {
            error("readString failed for file: " + filename);
        }
        if (s == null) {
            eof = true;
            s = "";
        }
        return s;
    }
    /**
     *  Deal with a file error, write a message and terminate.
     */
    private void error(String msg) {
        System.err.println(msg);
        System.err.println("Unable to continue executing program.");
        System.exit(0);
    }
}

posted @ 2009-03-03 15:45 李奇 阅读(127) 评论(0) 编辑

2009年2月18日

KeyboardInput类---载自《java软件开发》Russel Winder & Graham Roberts

import java.io.*;
/**
 * A simple input class to read values typed at the command line.   If
 * an error occurs during intput, any exceptions thrown are caught and
 * a default value returned.
 *
 * @version 1.1 1998.08.18
 * @author Graham Roberts
 * @author Russel Winder
 */
public class keyboardInput {
  /**
   *  The buffered stream that works the keyboard so that we can read
   *  from it sensibly
   */
  private final BufferedReader in =
          new BufferedReader(new InputStreamReader(System.in));
  /**
   *  Read an <CODE>int</CODE> value from keyboard input. The default
   *  return value is 0.
   */
  public final synchronized int readInteger() {
      String input = "";
      int value = 0;
      try {
          input = in.readLine();
      }
      catch (IOException e) { }
      if (input != null) {
          try {
              value = Integer.parseInt(input);
          }
          catch (NumberFormatException e) { }
      }
      return value;
  }
  /**
   *  Read a <CODE>long</CODE> value from keyboard input. The default
   *  return value is 0L.
   */
  public final synchronized long readLong() {
      String input = "";
      long value = 0L;
      try {
          input = in.readLine();
      }
      catch (IOException e) { }
      if (input != null) {
          try {
              value = Long.parseLong(input);
          }
          catch (NumberFormatException e) { }
      }
      return value;
  }
  /**
   *  Read a <CODE>double</CODE> value from keyboard input.  The default
   *  return value is 0.0
   */
  public final synchronized double readDouble() {
      String input = "";
      double value = 0.0D;
      try {
          input = in.readLine();
      }
      catch (IOException e) { }
      if (input != null) {
          try {
              value = Double.parseDouble(input);
          }
          catch (NumberFormatException e) { }
      }
      return value;
  }
  /**
   *  Read a <CODE>float</CODE> value from keyboard input.  The default
   *  return value is 0.0F
   */
  public final synchronized float readFloat() {
      String input = "";
      float value = 0.0F;
      try {
          input = in.readLine();
      }
      catch (IOException e) { }
      if (input != null) {
          try {
              value = Float.parseFloat(input);
          }
          catch (NumberFormatException e) { }
      }
      return value;
  }
  /**
   *  Read a <CODE>char</CODE> value from keyboard input.  The default
   *  return value is ' ' (space)
   */
  public final synchronized char readCharacter() {
      char c = ' ';
      try {
          c = (char)in.read();
      }
      catch (IOException e) { }
      return c;
  }
  /**
   *  Read a <CODE>String</CODE> value from keyboard input.  The default
   *  return value is "" (the empty string).
   */
  public final synchronized String readString() {
      String s = "";
      try {
          s = in.readLine();
      }
      catch (IOException e) { }
      if (s == null) {
          s = "";
      }
      return s;
  }
}

posted @ 2009-02-18 13:46 李奇 阅读(123) 评论(0) 编辑

2008年9月25日

C#编程sql语句反斜杠问题

折腾了一下午,外加半个晚上,终于把数据从局域网内的服务器调出来了!

问题是这样的,通过Oracle Sql Developer,我把数据表ChemicalComposition建立在隔壁小浩的Oracle服务器上,然后我这台客户机上想利用C#程序调用其中的ChemicalComposition。

关键代码片段如下

using (OracleConnection AGConnection = new OracleConnection(connectionString))

            {
                OracleCommand AGCommand = AGConnection.CreateCommand();
                AGCommand.CommandText = "SELECT * FROM ""ChemicalComposition""";
                OracleDataAdapter dataAdapter = new OracleDataAdapter(AGCommand);
                dataAdapter.Fill(AlloyGradeTable);

            } // AGConnection.Dispose called automatically.

 其实这段代码拷到网页页面上有点失真,

AGCommand.CommandText = "SELECT * FROM ""ChemicalComposition""";

在Visual Studio中是:

AGCommand.CommandText = "SELECT * FROM \"ChemicalComposition\"";

下午一直用 "SELECT * FROM ChemicalComposition"做程序中的查询语句,结果报错为:

ORA-00942: table or view does not exist

老是以为数据库连接有错,根本就没有考虑sql语句出错!

如果在Oracle Sql Developer使用SELECT * FROM ChemicalComposition,结果报错为:

An error was encountered performing the requested operation:

ORA-00942: table or view does not exist

00942. 00000  - "table or view does not exist"

*Cause:

*Action:

Error at Line:1 Column:14

到晚上才明白,sql语句有错,因为之前也知道在 Oracle Sql Developer中sql语句的表名是要加双引号的,就是说使用SELECT * FROM "ChemicalComposition"可以从数据库中成功地调出数据。

很好,问题的根源找到,那么怎么在C#程序中写sql语句呢?一开始我写成:

AGCommand.CommandText = "SELECT * FROM ""ChemicalComposition""";

当然是行不通的,该错误Visual Studio轻易捕获,接着有改成:

AGCommand.CommandText = "SELECT * FROM " + ""ChemicalComposition"" + """;

还是不行,通不过!

后来在网上一顿猛搜,原来在字符串中的双引号"要用反斜杠加双引号\"表示!我靠!这个不是在C语言中学过么???真是个极其弱智的问题!

这种问题在用C语言编程时就碰到过,为何到C#中却不加注意?这是经验不足么?还是缺乏变通能力?

posted @ 2008-09-25 20:44 李奇 阅读(1005) 评论(1) 编辑

2008年9月17日

一个子查询小问题

posted @ 2008-09-17 21:38 李奇 阅读(42) 评论(0) 编辑

2008年9月9日

数据库设计,你要如何设计才会更美?

posted @ 2008-09-09 20:04 李奇 阅读(106) 评论(4) 编辑

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

导航

统计

公告

昵称:李奇
园龄:3年5个月
粉丝:0
关注:0

搜索

 
 

常用链接

随笔档案

最新评论

阅读排行榜

评论排行榜

推荐排行榜