代码很少,无聊随便写写,大家看看!
与数据库相关的在 sqlfa.java里 主窗口在trainfram.java里!1个多小时,好久没有写了,感觉有点陌生!哈哈
sqlfa.java
package workfive;
import java.sql.*;
class sqlfa{
//连驱动,数据源
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection con=null;
Statement stmt=null;
String sql=null;
String bbsql;
void lian_driver(){
String yuan="jdbc:odbc:test";
try{
Class.forName(driver);
}catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection(yuan);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(SQLException ee){}
}
//查询
public ResultSet fan_guo()throws SQLException{
ResultSet guo=null;
guo=stmt.executeQuery(sql);
return guo;
}
//接收,修改查询语句
void xiugai_sql(String ru){
this.sql="select * from TrainTicket where TrainNo='"+ru+"'";
}
//计算
public float jisuan(int xx,float yy){
return xx*yy;
}
//更新语句
void gengxin(int sit_x,int bed_x,String no,boolean n)throws SQLException{
if(n){
this.bbsql = "update trainticket set seatnum="+sit_x+" where trainno='"+no+"'";
}else
{
this.bbsql = "update trainticket set bednum="+bed_x+" where trainno='"+no+"'";
}
stmt.executeQuery(bbsql);
}
}
下面是主窗口
package workfive;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
public class trainfram extends JFrame {
public trainfram() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
sqlfa mm=new sqlfa();
ResultSet guoguo=null;
float sum;
float yun=15;
static boolean panduan=true;
int sit_yu,bed_yu;
//*****
public float jisuan(int xx,float yy){
return xx*yy;
}
//计算
void ji_suan(boolean nn)throws Exception{
if(nn)
{
this.sit_yu=this.guoguo.getInt("seatNum")-Integer.parseInt(this.txt_num.getText());
this.sum=this.jisuan(Integer.parseInt(this.txt_num.getText()),this.guoguo.getFloat("seatprice"));
}
else{
this.bed_yu=this.guoguo.getInt("bedNum")-Integer.parseInt(this.txt_num.getText());
this.sum=this.jisuan(Integer.parseInt(this.txt_num.getText()),this.guoguo.getFloat("bedprice"));}
}
//传递
void chuan(){
mm.xiugai_sql(this.txt_no.getText());
}
//show
void showshow()throws SQLException{
this.chuan();
mm.lian_driver();
this.guoguo=mm.fan_guo();
if(this.guoguo.next())
//{ this.guoguo.next();}else {this.guoguo.close();}
{
// try{
// this.ji_suan(this.yingzuo.isSelected());
// }catch(Exception af){}
this.panduan=true;
this.ceshi();
this.guoguo.close();
}
else{this.txt_num.setText("");
this.panduan=false;
this.ceshi();
this.guoguo.close();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jb_top.setText("订购火车票");
jb_top.setBounds(new Rectangle(154, 18, 80, 32));
checi.setText("火车车次:");
checi.setBounds(new Rectangle(60, 86, 83, 29));
fangshi.setText("乘车方式:");
fangshi.setBounds(new Rectangle(58, 145, 70, 28));
yingzuo.setText("硬座");
yingzuo.setBounds(new Rectangle(139, 147, 71, 23));
yingzuo.addActionListener(new trainfram_yingzuo_actionAdapter(this));
yingwo.setText("硬卧");
yingwo.setBounds(new Rectangle(225, 145, 64, 26));
yingwo.addActionListener(new trainfram_yingwo_actionAdapter(this));
shuliang.setText("订购数量:");
shuliang.setBounds(new Rectangle(56, 212, 78, 33));
txt_num.setBounds(new Rectangle(143, 214, 120, 28));
txt_no.setBounds(new Rectangle(141, 86, 125, 29));
jb_ok.setBounds(new Rectangle(151, 285, 108, 40));
jb_ok.setText("确定");
jb_ok.addActionListener(new trainfram_jb_ok_actionAdapter(this));
this.setSize(450,450);
jop_wrong.setToolTipText("");
jop_wrong.setBounds(new Rectangle(66, 119, 262, 90));
jop_wrong.setLayout(null);
this.getContentPane().add(checi);
this.getContentPane().add(fangshi);
this.getContentPane().add(yingzuo);
this.getContentPane().add(yingwo);
this.getContentPane().add(shuliang);
this.getContentPane().add(txt_num);
this.getContentPane().add(txt_no);
this.getContentPane().add(jb_ok);
this.getContentPane().add(jop_wrong);
this.getContentPane().add(jb_top);
this.setVisible(true);
but_zu.add(yingzuo);
but_zu.add(yingwo);
}
//测试
void ceshi()throws SQLException{
if(this.panduan)
{
if(this.yingzuo.isSelected()&this.guoguo.getInt("seatNum")>=Integer.parseInt(this.txt_num.getText())|this.yingwo.isSelected()&this.guoguo.getInt("bedNum")>=Integer.parseInt(this.txt_num.getText()))
{ try{
this.ji_suan(this.yingzuo.isSelected());
}catch(Exception af){}
JOptionPane.showMessageDialog(jop_wrong,"请付款"+this.sum);
mm.gengxin(this.sit_yu,this.bed_yu,this.guoguo.getString("trainNo"),this.yingzuo.isSelected());
}
else{
JOptionPane.showMessageDialog(jop_wrong,"票数不够,请重新输入!");
}
}else
{
JOptionPane.showMessageDialog(jop_wrong,"您输入的车次不存在,请重新输入!");
}
}
public static void main(String[] args) {
trainfram trainfram = new trainfram();
}
JLabel jb_top = new JLabel();
JLabel checi = new JLabel();
JLabel fangshi = new JLabel();
JRadioButton yingzuo = new JRadioButton("",true);
JRadioButton yingwo = new JRadioButton();
JLabel shuliang = new JLabel();
JTextField txt_num = new JTextField();
JTextField txt_no = new JTextField();
JButton jb_ok = new JButton();
JOptionPane jop_wrong = new JOptionPane();
ButtonGroup but_zu = new ButtonGroup();
public void jb_ok_actionPerformed(ActionEvent e) {
try{
this.showshow();
}catch(SQLException aaa){}
}
public void yingzuo_actionPerformed(ActionEvent e) {
}
public void yingwo_actionPerformed(ActionEvent e) {
}
}
class trainfram_yingwo_actionAdapter implements ActionListener {
private trainfram adaptee;
trainfram_yingwo_actionAdapter(trainfram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.yingwo_actionPerformed(e);
}
}
class trainfram_yingzuo_actionAdapter implements ActionListener {
private trainfram adaptee;
trainfram_yingzuo_actionAdapter(trainfram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.yingzuo_actionPerformed(e);
}
}
class trainfram_jb_ok_actionAdapter implements ActionListener {
private trainfram adaptee;
trainfram_jb_ok_actionAdapter(trainfram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jb_ok_actionPerformed(e);
}
}
大家看了,给点意见。谢谢了。。。。还有好多 没有时间发。。。
与数据库相关的在 sqlfa.java里 主窗口在trainfram.java里!1个多小时,好久没有写了,感觉有点陌生!哈哈
sqlfa.java
package workfive;
import java.sql.*;
class sqlfa{
//连驱动,数据源
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection con=null;
Statement stmt=null;
String sql=null;
String bbsql;
void lian_driver(){
String yuan="jdbc:odbc:test";
try{
Class.forName(driver);
}catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection(yuan);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(SQLException ee){}
}
//查询
public ResultSet fan_guo()throws SQLException{
ResultSet guo=null;
guo=stmt.executeQuery(sql);
return guo;
}
//接收,修改查询语句
void xiugai_sql(String ru){
this.sql="select * from TrainTicket where TrainNo='"+ru+"'";
}
//计算
public float jisuan(int xx,float yy){
return xx*yy;
}
//更新语句
void gengxin(int sit_x,int bed_x,String no,boolean n)throws SQLException{
if(n){
this.bbsql = "update trainticket set seatnum="+sit_x+" where trainno='"+no+"'";
}else
{
this.bbsql = "update trainticket set bednum="+bed_x+" where trainno='"+no+"'";
}
stmt.executeQuery(bbsql);
}
}
下面是主窗口
package workfive;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
public class trainfram extends JFrame {
public trainfram() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
sqlfa mm=new sqlfa();
ResultSet guoguo=null;
float sum;
float yun=15;
static boolean panduan=true;
int sit_yu,bed_yu;
//*****
public float jisuan(int xx,float yy){
return xx*yy;
}
//计算
void ji_suan(boolean nn)throws Exception{
if(nn)
{
this.sit_yu=this.guoguo.getInt("seatNum")-Integer.parseInt(this.txt_num.getText());
this.sum=this.jisuan(Integer.parseInt(this.txt_num.getText()),this.guoguo.getFloat("seatprice"));
}
else{
this.bed_yu=this.guoguo.getInt("bedNum")-Integer.parseInt(this.txt_num.getText());
this.sum=this.jisuan(Integer.parseInt(this.txt_num.getText()),this.guoguo.getFloat("bedprice"));}
}
//传递
void chuan(){
mm.xiugai_sql(this.txt_no.getText());
}
//show
void showshow()throws SQLException{
this.chuan();
mm.lian_driver();
this.guoguo=mm.fan_guo();
if(this.guoguo.next())
//{ this.guoguo.next();}else {this.guoguo.close();}
{
// try{
// this.ji_suan(this.yingzuo.isSelected());
// }catch(Exception af){}
this.panduan=true;
this.ceshi();
this.guoguo.close();
}
else{this.txt_num.setText("");
this.panduan=false;
this.ceshi();
this.guoguo.close();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jb_top.setText("订购火车票");
jb_top.setBounds(new Rectangle(154, 18, 80, 32));
checi.setText("火车车次:");
checi.setBounds(new Rectangle(60, 86, 83, 29));
fangshi.setText("乘车方式:");
fangshi.setBounds(new Rectangle(58, 145, 70, 28));
yingzuo.setText("硬座");
yingzuo.setBounds(new Rectangle(139, 147, 71, 23));
yingzuo.addActionListener(new trainfram_yingzuo_actionAdapter(this));
yingwo.setText("硬卧");
yingwo.setBounds(new Rectangle(225, 145, 64, 26));
yingwo.addActionListener(new trainfram_yingwo_actionAdapter(this));
shuliang.setText("订购数量:");
shuliang.setBounds(new Rectangle(56, 212, 78, 33));
txt_num.setBounds(new Rectangle(143, 214, 120, 28));
txt_no.setBounds(new Rectangle(141, 86, 125, 29));
jb_ok.setBounds(new Rectangle(151, 285, 108, 40));
jb_ok.setText("确定");
jb_ok.addActionListener(new trainfram_jb_ok_actionAdapter(this));
this.setSize(450,450);
jop_wrong.setToolTipText("");
jop_wrong.setBounds(new Rectangle(66, 119, 262, 90));
jop_wrong.setLayout(null);
this.getContentPane().add(checi);
this.getContentPane().add(fangshi);
this.getContentPane().add(yingzuo);
this.getContentPane().add(yingwo);
this.getContentPane().add(shuliang);
this.getContentPane().add(txt_num);
this.getContentPane().add(txt_no);
this.getContentPane().add(jb_ok);
this.getContentPane().add(jop_wrong);
this.getContentPane().add(jb_top);
this.setVisible(true);
but_zu.add(yingzuo);
but_zu.add(yingwo);
}
//测试
void ceshi()throws SQLException{
if(this.panduan)
{
if(this.yingzuo.isSelected()&this.guoguo.getInt("seatNum")>=Integer.parseInt(this.txt_num.getText())|this.yingwo.isSelected()&this.guoguo.getInt("bedNum")>=Integer.parseInt(this.txt_num.getText()))
{ try{
this.ji_suan(this.yingzuo.isSelected());
}catch(Exception af){}
JOptionPane.showMessageDialog(jop_wrong,"请付款"+this.sum);
mm.gengxin(this.sit_yu,this.bed_yu,this.guoguo.getString("trainNo"),this.yingzuo.isSelected());
}
else{
JOptionPane.showMessageDialog(jop_wrong,"票数不够,请重新输入!");
}
}else
{
JOptionPane.showMessageDialog(jop_wrong,"您输入的车次不存在,请重新输入!");
}
}
public static void main(String[] args) {
trainfram trainfram = new trainfram();
}
JLabel jb_top = new JLabel();
JLabel checi = new JLabel();
JLabel fangshi = new JLabel();
JRadioButton yingzuo = new JRadioButton("",true);
JRadioButton yingwo = new JRadioButton();
JLabel shuliang = new JLabel();
JTextField txt_num = new JTextField();
JTextField txt_no = new JTextField();
JButton jb_ok = new JButton();
JOptionPane jop_wrong = new JOptionPane();
ButtonGroup but_zu = new ButtonGroup();
public void jb_ok_actionPerformed(ActionEvent e) {
try{
this.showshow();
}catch(SQLException aaa){}
}
public void yingzuo_actionPerformed(ActionEvent e) {
}
public void yingwo_actionPerformed(ActionEvent e) {
}
}
class trainfram_yingwo_actionAdapter implements ActionListener {
private trainfram adaptee;
trainfram_yingwo_actionAdapter(trainfram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.yingwo_actionPerformed(e);
}
}
class trainfram_yingzuo_actionAdapter implements ActionListener {
private trainfram adaptee;
trainfram_yingzuo_actionAdapter(trainfram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.yingzuo_actionPerformed(e);
}
}
class trainfram_jb_ok_actionAdapter implements ActionListener {
private trainfram adaptee;
trainfram_jb_ok_actionAdapter(trainfram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jb_ok_actionPerformed(e);
}
}
大家看了,给点意见。谢谢了。。。。还有好多 没有时间发。。。