【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了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, pass
word);
System.out.println ("连接bjtdb数据库成功!");
Statement stmt = con.createStat