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

📄 step2_frame.java

📁 《JAVA网络编程实例》源代码
💻 JAVA
字号:
import java.sql.*;import java.awt.*;import java.awt.event.*;/** * File:        step2_Frame.java * 按照转乘方式查寻,交互操作的第二步的界面和处理代码 */public class step2_Frame extends Frame {//////////////////////////////////这个界面提供一次转乘方式的乘车路线查寻。////请按照按钮上序号所提示的步骤顺序操作://1,查找中间转乘车站:   给出可能的转乘站。//2,到换乘站路线:       查寻如何乘车可以从  “始  站”  到达  “转乘站”。//3,到目标站路线:       查寻如何乘车可以从  “转乘站”  到达  “目标站”。////这个版本的查询系统仅仅提供最多一次转乘的功能。//更多次转乘可以通过手工反复寻找中间转乘车站拉达到目的。////////////////////////////////  public String mystat1=new String("");  public String mystat2=new String("");  Button button1 = new Button();  Button button2 = new Button();  TextField user_mystat1 = new TextField();  TextField user_mystat2 = new TextField();  Choice stat_choice = new Choice();  Button button3 = new Button();  Button button4 = new Button();  Choice line_list1 = new Choice();  Choice line_list2 = new Choice();  Label label1 = new Label();  Label label2 = new Label();  Label label3 = new Label();  Label label4 = new Label();  Label label5 = new Label();  Label label6 = new Label();  Label label7 = new Label();  Label label8 = new Label();  Label label9 = new Label();  public step2_Frame() {    this.setSize(550,300);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  private void jbInit() throws Exception {    this.setLayout(null);    button2.setLabel("取消");    button2.setBounds(new Rectangle(380, 179, 85, 26));    button2.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button2_actionPerformed(e);      }    });    user_mystat1.setText("出发站");    user_mystat1.setBounds(new Rectangle(6, 120, 76, 42));    user_mystat2.setText("到达站");    user_mystat2.setBounds(new Rectangle(421, 119, 83, 40));    this.setVisible(true);    button1.setLabel("1,查找中间转乘车站");    button1.setBounds(new Rectangle(143, 180, 215, 26));    button1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button1_actionPerformed(e);      }    });    button3.setLabel("2,到换乘站路线");    button3.setBounds(new Rectangle(86, 121, 126, 23));    button3.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button3_actionPerformed(e);      }    });    button4.setLabel("3,到目标站路线");    button4.setBounds(new Rectangle(284, 119, 133, 22));    button4.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button4_actionPerformed(e);      }    });    label1.setText("始站:");    label1.setBounds(new Rectangle(8, 94, 86, 28));    label2.setText("目标站:");    label2.setBounds(new Rectangle(425, 92, 86, 28));    label3.setText("换乘站:");    label3.setBounds(new Rectangle(214, 91, 86, 28));    label5.setText("请按照按钮上序号所提示的步骤顺序操作:");    label5.setBounds(new Rectangle(18, 6, 359, 26));    label6.setText("1,查找中间转乘车站:   给出可能的转乘站。");    label6.setBounds(new Rectangle(20, 31, 276, 16));    label7.setText("2,到换乘站路线:       查寻如何乘车可以从  “始  站”  到达  “转乘站”。");    label7.setBounds(new Rectangle(19, 50, 438, 16));    label8.setText("3,到目标站路线:       查寻如何乘车可以从  “转乘站”  到达  “目标站”。");    label8.setBounds(new Rectangle(20, 70, 446, 16));    label9.setText("----------------------------------------------------------------------------------------------------");    label9.setBounds(new Rectangle(7, 84, 468, 14));    line_list2.setBounds(new Rectangle(286, 142, 131, 18));    line_list1.setBounds(new Rectangle(87, 142, 125, 19));    stat_choice.setBounds(new Rectangle(216, 119, 66, 24));    label4.setBounds(new Rectangle(87, 85, 86, 28));    this.add(label4, null);    this.add(label6, null);    this.add(label5, null);    this.add(button1, null);    this.add(label1, null);    this.add(user_mystat1, null);    this.add(line_list1, null);    this.add(button3, null);    this.add(label3, null);    this.add(stat_choice, null);    this.add(button4, null);    this.add(line_list2, null);    this.add(label2, null);    this.add(user_mystat2, null);    this.add(button2, null);    this.add(label9, null);    this.add(label8, null);    this.add(label7, null);  }  void button1_actionPerformed(ActionEvent e) {  /////////////////////  //1,查找中间转乘车站:   给出可能的转乘站。  /////////////////////    String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";    String databaseURL = "jdbc:odbc:bjtdb";    String user = "bjtdb";    String password = "bjtdb";    String sqlStr="SELECT distinct stat FROM stat_line WHERE line in (select line from stat_line where stat='"+user_mystat1.getText() +"') and stat in (SELECT stat FROM stat_line WHERE line in (select line from stat_line where stat='"+user_mystat2.getText() +"'))";    System.out.print(sqlStr);    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_list1.removeAll();            if (!result.next()) {               msgFrame f=new msgFrame("没有中间转乘车站。由于计算过于复杂,请手工查找。");               f.show();            } else {               msgFrame f=new msgFrame("列表显示了可行中间转乘车站。请选择。");               f.show();            };            stat_choice.removeAll();            //读出当前的记录            stat_choice.add(result.getString(1) );            //循环读出剩下的所有记录            while (result.next()) {              String returnInfo = result.getString(1);              System.out.println(returnInfo);              stat_choice.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 void  void button2_actionPerformed(ActionEvent e) {    this.hide();  }  void button3_actionPerformed(ActionEvent e) {  /////////////////////////  //2,到换乘站路线:       查寻如何乘车可以从  “始  站”  到达  “转乘站”。  /////////////////////////    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='"+user_mystat1.getText()+"' and line in (SELECT line FROM stat_line where stat='"+stat_choice.getSelectedItem()+"')";    System.out.println(sqlStr);    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_list1.removeAll();            while (result.next()) {              String returnInfo = result.getString(1);              line_list1.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; } }  void button4_actionPerformed(ActionEvent e) {  /////////////////////////  //3,到目标站路线:       查寻如何乘车可以从  “转乘站”  到达  “目标站”。  /////////////////////////    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='"+stat_choice.getSelectedItem()+"' and line in (SELECT line FROM stat_line where stat='"+user_mystat2.getText() +"')";    System.out.println(sqlStr);    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_list2.removeAll();            while (result.next()) {              String returnInfo = result.getString(1);              line_list2.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; } }}

⌨️ 快捷键说明

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