📄 busmanageframe.java~258~
字号:
jPanel4.add(jPanel10, null);
jPanel4.add(jTextField1, null);
jPanel4.add(jRadioButton1, null);
jPanel4.add(jRadioButton2, null);
jPanel4.add(jButton11, null);
jPanel4.add(scroll);
jPanel4.add(jPanel12, null);
}
/////////////////////////////////////站点管理
String checihao, xuhao, stationname;
int checinum, stationnum;
////////////////////////增加车次函数
public void addcheci(int linenum) {
String sql = "select * from stationinfo where 车次号=" + linenum + "";
ResultSet rs;
boolean checicunzai = false;
try {
rs = stmt.executeQuery(sql);
while (rs.next()) {
checicunzai = true;
}
rs.close();
}
catch (SQLException e) {
}
if (checicunzai) {
JOptionPane.showMessageDialog(null, "车次已经存在 !");
}
else {
//车次不存在,可进行添加。
String sqlinsert = "insert into stationinfo values(" + linenum +
",1,'0')";
String sqlline = "insert into businfo values(" + linenum +
",'startstation','5:00-22:00','end','5:35-22:35','不是')";
try {
if (!stmt.execute(sqlinsert) & !stmt.execute(sqlline)) {
JOptionPane.showMessageDialog(null, "车次添加成功!请添加车次的线路信息!");
}
else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
}
catch (SQLException e) {
}
}
}
//////////////////////////查找车次函数
public void searchcheci() {
checihao = jTextField1.getText();
}
//////////////////////////每路车的站点数函数
public int countnum(int c) {
int countnum = 0;
String countsql = "select * from stationinfo where 车次号=" + c + "";
System.out.println("sql语句:"+countsql);
try {
ResultSet rs = stmt.executeQuery(countsql);
while(rs.next()){
countnum++;
}
System.out.println("站点数:"+countnum);
rs.close();
}
catch (SQLException sqle1) {
}
return countnum;
}
///////////////////////////////////////////增加站点函数
public void addstation(int c, int x, String sn) {
int countnum; //站点数目
countnum = countnum(c);
//调整插入序号后面的序号都相应的加1
String updatesql = "update stationinfo set 序号=序号+1 where 车次号="+c+" and 序号>=" + x + " ";
try {
stmt.execute(updatesql);
}
catch (SQLException updatesqle) {
}
//插入要增加的站点
String sql = "insert into stationinfo values(" + c + "," + x + "," + "\'" +
sn + "\'" + ")";
try {
boolean a = stmt.execute(sql);
if (!a) {
JOptionPane.showMessageDialog(null, "插入成功!");
}
else {
JOptionPane.showMessageDialog(null, "插入失败!");
}
}
catch (SQLException inserte) {
}
String sql1="select 站点名 from stationinfo where 车次号="+c+" ";
ResultSet rs1;
Vector station=new Vector();
String stationname=null,start=null,end=null;
try{
rs1=stmt.executeQuery(sql1);
while(rs1.next())
{
stationname = rs1.getString("站点名");
station.addElement(stationname);
}
}
catch(SQLException e){}
int stationsize=station.size();
start=station.get(0).toString().trim();
end=station.get(stationsize-1).toString().trim();
String sql2="update businfo set 始发站='"+start+"' , 终点站='"+end+"' where 车次号="+c+"";
System.out.println(sql2);
try{
stmt.executeUpdate(sql2);
}catch(SQLException e2){}
}
///////////////////////////////////////////删除站点函数
public void delstation(int c, int x, String sn) {
checihao = jTextField2.getText(); //车次号
stationname = jTextField3.getText(); //站点名
xuhao = jTextField4.getText(); //序号
int countnum=0;//每次删除后记录站点数
boolean laststation=false;//是否是最后一个站点
String delsql = "delete from stationinfo where 车次号=" + c + " and 序号=" + x +
"";
if(!laststation){
countnum = countnum(c); //返回该线路的站点数
}
if(countnum==1){
laststation=true;//最后一个站点
//如果站点数为0,则该线路为空
int b=JOptionPane.showConfirmDialog(null,"最后一个站点,删除后,该线路将被删除!确实要删除?");
if(b==0){
//确认删除
try{
if(stmt.execute(delsql))
{//删除失败
JOptionPane.showMessageDialog(null,"删除失败!");
}
else{
JOptionPane.showMessageDialog(null,"删除成功!");
}
}catch(SQLException e){}
}
}
else{//如果站点数不为空,可以继续进行删除
try {
int a = JOptionPane.showConfirmDialog(null, "确实要删除" + sn + "站点");
if (a == 0) { //确认删除
stmt.execute(delsql);
}
}
catch (SQLException delsqle) {
}
//删除序号为x的站点,后边的站点序号要减一
String add1sql = "update stationinfo set 序号=序号-1 where 车次号=" + c +
" and 序号>" + x + "";
try {
if (!stmt.execute(add1sql)) {
JOptionPane.showMessageDialog(null, "删除成功!");
}
else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
}
catch (SQLException add1sqle) {
}
String sql1 = "select 站点名 from stationinfo where 车次号=" + c + " ";
ResultSet rs1;
Vector station = new Vector();
String stationname = null, start = null, end = null;
try {
rs1 = stmt.executeQuery(sql1);
while (rs1.next()) {
stationname = rs1.getString("站点名");
station.addElement(stationname);
}
}
catch (SQLException e) {}
int stationsize = station.size();
start = station.get(0).toString().trim();
end = station.get(stationsize - 1).toString().trim();
String sql2 = "update businfo set 始发站='" + start + "' , 终点站='" + end +
"' where 车次号=" + c + "";
System.out.println(sql2);
try {
stmt.executeUpdate(sql2);
}
catch (SQLException e2) {}
}
}
///////////////////////////////修改站点函数
public void changestation(int c, int x, String sn) {
String sql = "update stationinfo set 站点名=" + "\'" + sn + "\'" +
" where 车次号=" + c + " and 序号=" + x + "";
//ResultSet rs;
try {
if (!stmt.execute(sql)) { //执行更新语句
JOptionPane.showMessageDialog(null, "更新成功!");
}
else {
JOptionPane.showMessageDialog(null, "更新失败!");
}
}
catch (SQLException e1) {
}
String sql1="select 站点名 from stationinfo where 车次号="+c+" ";
ResultSet rs1;
Vector station=new Vector();
String stationname=null,start=null,end=null;
try{
rs1=stmt.executeQuery(sql1);
while(rs1.next())
{
stationname = rs1.getString("站点名");
station.addElement(stationname);
}
}
catch(SQLException e){}
int stationsize=station.size();
start=station.get(0).toString().trim();
end=station.get(stationsize-1).toString().trim();
String sql2="update businfo set 始发站='"+start+"' , 终点站='"+end+"' where 车次号="+c+"";
System.out.println(sql2);
try{
stmt.executeUpdate(sql2);
}catch(SQLException e2){}
}
//////////////////////////////////////判断车次是否存在
boolean checicunzai(int checinum) {
String sql = "select * from stationinfo where 车次号=" + checinum + "";
ResultSet rs;
boolean checicunzai = false;
try {
rs = stmt.executeQuery(sql);
while (rs.next()) {
checicunzai = true;
}
rs.close();
}
catch (SQLException e) {
}
if (checicunzai) {
return true;
}
else
return false;
}
/////////////////////////////////////////////////GO按钮
void jButton11_actionPerformed(ActionEvent e) {
String line = jTextField1.getText().trim();
if (line.equals("")) {
JOptionPane.showMessageDialog(null, "线路名称不能为空!");
}
else { //输入线路不为空
if (jRadioButton1.isSelected()) { //增加车次被选中
int linenum = Integer.parseInt(line);
jTextField2.setText(line);
addcheci(linenum);
}
else if (jRadioButton2.isSelected()) { //查询车次被选中
jTextField2.setText(line);
int linenum = Integer.parseInt(line);
if (!checicunzai(linenum)) {
JOptionPane.showMessageDialog(null, "所查车次不存在!");
}
else {
ResultSet rs;
String sql = "select * from stationinfo where 车次号=" + linenum + "";
vector.removeAllElements(); //初始化向量对象
tm.fireTableStructureChanged(); //更新表格内容
try {
rs = stmt.executeQuery(sql);
while (rs.next()) {
Vector line_vector = new Vector(); //从结果集中取数据放入向量line_vector中
line_vector.addElement(String.valueOf(rs.getInt("车次号")));
line_vector.addElement(String.valueOf(rs.getInt("序号")));
line_vector.addElement(rs.getString("站点名"));
vector.addElement(line_vector); //向量line_vector加入向量vector中
}
tm.fireTableStructureChanged(); //更新表格,显示向量vector的内容
rs.close(); //关闭结果集
}
catch (SQLException sqle) {
}
}
}
}
}
///////////////////////////////////////////修改按钮
void jButton14_actionPerformed(ActionEvent e) {
checihao = jTextField2.getText().trim();
xuhao = jTextField3.getText().trim();
stationname = jTextField4.getText().trim();
if (checihao.equals("")) {
JOptionPane.showMessageDialog(null, "请先查询车次!");
}
else if (stationname.equals("")) {
JOptionPane.showMessageDialog(null, "填好站点名!");
}
else if (xuhao.equals("")) {
JOptionPane.showMessageDialog(null, "填好序号!");
}
else {
checinum = Integer.parseInt(checihao);
stationnum = Integer.parseInt(xuhao);
changestation(checinum, stationnum, stationname);
}
}
//////////////////////////////////////////增加站点
void jButton12_actionPerformed(ActionEvent e) {
boolean stationcunzai = false;
checihao = jTextField2.getText().trim(); //车次号
stationname = jTextField4.getText().trim(); //站点名
xuhao = jTextField3.getText().trim(); //序号
if (checihao.equals("")) {
JOptionPane.showMessageDialog(null, "请先查询车次!");
}
else if (stationname.equals("")) {
JOptionPane.showMessageDialog(null, "填好站点名!");
}
else if (xuhao.equals("")) {
JOptionPane.showMessageDialog(null, "填好序号!");
}
else {
checinum = Integer.parseInt(checihao);
stationnum = Integer.parseInt(xuhao);
String sql = "select * from stationinfo where 车次号=" + checinum +
" and 站点名=" + "\'" + stationname + "\'" + "";
try {
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
//判断该站点是否存在
stationcunzai = true;
}
rs.close(); //关闭结果集
}
catch (SQLException sqle) {
}
if (stationcunzai) {
//若站点存在
JOptionPane.showMessageDialog(null, "" + stationname + "站点已经存在!");
}
else {
//该站点不存在,再判断序号是否正确,是否越界
int countnum = 0;
String sql1 = "select * from stationinfo where 车次号=" + checinum + "";
try {
ResultSet rs1 = stmt.executeQuery(sql1);
while (rs1.next()) {
countnum++;
}
rs1.close();
}
catch (SQLException sqle1) {
}
if (stationnum > (countnum + 1)) { //如果输入序号大于车次中最大序号,越界
JOptionPane.showMessageDialog(null, "输入序号越界!");
}
else if (stationnum <= 0) {
JOptionPane.showMessageDialog(null, "输入序号应大于等于1!");
}
else {
//输入没有错误,可以添加
addstation(checinum, stationnum, stationname); //添加站点函数
}
}
}
}
////////////////////////////////////////////////删除站点
void jButton13_actionPerformed(ActionEvent e) {
boolean stationcunzai = false;
checihao = jTextField2.getText().trim(); //车次号
stationname = jTextField4.getText().trim(); //站点名
xuhao = jTextField3.getText().trim(); //序号
if (checihao.equals("")) {
JOptionPane.showMessageDialog(null, "填好车次号!");
}
else if (stationname.equals("")) {
JOptionPane.showMessageDialog(null, "填好站点名!");
}
else if (xuhao.equals("")) {
JOptionPane.showMessageDialog(null, "填好序号!");
}
else {
checinum = Integer.parseInt(checihao);
stationnum = Integer.parseInt(xuhao);
String sql = "select * from stationinfo where 车次号=" + checinum +
" and 站点名=" + "\'" + stationname + "\'" + "";
try {
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
//判断该站点是否存在
stationcunzai = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -