📄 bjtapplet.java
字号:
import java.sql.*;import java.awt.*;import java.awt.event.*;import java.applet.*;/** * File: bjtApplet.java 主程序 * Title: 小型数据库查寻系统 * Description: 查寻北京市区范围内任意两个公交车站之间的乘坐路线。 * Copyright: Copyright (c) 2002 * Company: 中国科学院 * @author 李小满 * @version 1.0 由于教程需要,此版本仅仅提供了简单的直达和一次转乘查询。 */public class bjtApplet extends Applet { String mystat1=new String(""); String mystat2=new String(""); boolean isStandalone = false; Button button1 = new Button(); TextField user_mystat1 = new TextField(); TextField user_mystat2 = new TextField(); List line_list = new List(); Button button2 = new Button(); Label label1 = new Label(); /**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 bjtApplet() { } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { button1.setLabel("查询"); button1.setBounds(new Rectangle(28, 206, 176, 24)); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); this.setLayout(null); user_mystat1.setText("出发站"); user_mystat1.setBounds(new Rectangle(28, 176, 116, 23)); user_mystat2.setText("到达站"); user_mystat2.setBounds(new Rectangle(154, 176, 121, 26)); button2.setLabel("取消"); button2.setBounds(new Rectangle(216, 206, 60, 24)); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); line_list.setMultipleMode(true); line_list.setBounds(new Rectangle(28, 30, 243, 92)); label1.setText("请在下面两个文本框分别填写入出发站和到达站:"); label1.setBounds(new Rectangle(27, 151, 296, 19)); this.add(button1, null); this.add(user_mystat1, null); this.add(user_mystat2, null); this.add(button2, null); this.add(line_list, null); this.add(label1, null); } /**Start the applet*/ public void start() { } /**Stop the applet*/ public void stop() { } /**Destroy the applet*/ public void destroy() { } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /**Main method*/ public static void main(String[] args) { bjtApplet applet = new bjtApplet(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("北京市区公交查寻系统"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } void button1_actionPerformed(ActionEvent e) { ///////////////////////////////////////////////////// //查寻两站之间的可行乘车路线,如果存在直达线路则显示直达路线, //反之,则进一步提示用户寻找转乘路线。 ////////////////////////////////////////////////////// mystat1=user_mystat1.getText(); mystat2=user_mystat2.getText(); String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; String databaseURL = "jdbc:odbc:bjtdb"; String user = "bjtdb"; String password = "bjtdb"; String sqlStr="SELECT DISTINCT line FROM stat_line WHERE stat='"+mystat1+"' and line in (SELECT line FROM stat_line where stat='"+mystat2+"')"; try { Class.forName (driverName); System.out.println ("成功加载JDBC-ODBC驱动程序!"); } catch (java.lang.ClassNotFoundException ex) { System.out.println ("加载JDBC-ODBC驱动程序失败!"); System.out.println (ex.getMessage ()); return; } try { Connection con = DriverManager.getConnection (databaseURL, user, password); System.out.println ("连接bjtdb数据库成功!"); Statement stmt = con.createStatement(); try{ ResultSet result = stmt.executeQuery(sqlStr); //初始化(清空)路线列表. line_list.clear(); //判断是否得到直达路线 if (!result.next()) { //没有直达路线,提示用户进行下一步查找(查寻是否可以通过转乘站到达目的站) step1_Frame f1=new step1_Frame("没有直达路线。点击<<下一步>>寻找转车车站。"); f1.mystat1=this.mystat1; f1.mystat2=this.mystat2; f1.show(); } else { //有直达路线,直接给出直达路线。 line_list.clear(); line_list.add("OK,这里是所有的可直达路线:"); msgFrame msgframe=new msgFrame("查询到了可以直达的路线,请看“直达路线列表”。"); msgframe.show(); //由于前面判断语句中使用了result.next(), //这个函数不仅返回了一个Boolean的判断, //而且把结果集合指针向后移动了一个纪录。 //所以这里先用result.getString(1)方法读出当前的记录。 //后面的while循环将不在能读到头条记录. line_list.add(result.getString(1)); //循环读出剩下的所有记录. while (result.next()) { String returnInfo = result.getString(1); line_list.add(returnInfo); } // end while }; } catch(SQLException ex){ System.out.println ("查询数据库数据失败!"); System.err.println("SQLException: " + ex.getMessage()); } con.close(); } catch(SQLException ex) { System.out.println ("连接bjtdb数据库失败!"); System.err.println("SQLException: " + ex.getMessage()); return; }//end try }//end void void button2_actionPerformed(ActionEvent e) { this.hide(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -