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

📄 countrymanager.java

📁 eclipse java/jsp 航空管理系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package kangyi.model;

import java.sql.*;
import java.util.Date;
import java.text.DateFormat;

import zhangchunliang.model.AppMode;

import kangyi.form.CountryManagerForm;
import kangyi.ConnectionPool.DbConnection;

/** * <p>Description  :  国家和城市数据字典中所有对数据库的操作 </p>
 * <p>Project      :  ciqms
 * <p>Company      :  东软股份国际合作事业部</p>
 * <p>Create Date  :  2005.4.17</P>
 * @author         :  康毅 | kangyi@neusoft.com
 * @version        :  0.2
 * @see            :  kangyi.form.CountryManagerForm */


public class CountryManager {

	/**
	 * 
	 * @uml.property name="db"
	 * @uml.associationEnd multiplicity="(0 1)"
	 */
	DbConnection db = null;

    Connection con=null;

	/*
	 * name:         转换字符串方法
	 * Description:  把屏幕上获取的中文字符串正确的传入MODEL中
	 * @return       Str 转换完的字符串 
	 */
	public String change(String s) {
		String str = s;
		try {
			byte b[] = str.getBytes("ISO-8859-1");
			str = new String(b);
			return str;
		} catch (Exception e) {
			return str;
		}
	}

	/*
	 * name:   生成系统时间
	 * 静态方法 不用事例化直接调用 属于类方法
	 */
	public static String nowDate() {
		/** 系统时间变量 */
		Date nowdate = new Date();

		/** 格式化后时间变量 */
		String formatDate;

		/** 格式化为"YYYY-MM-DD" */
		formatDate = DateFormat.getDateInstance().format(nowdate);
		return formatDate;
	}

	/*生成系统时间*/
	String insert_date = nowDate();

	/*生成系统时间*/
	String update_date = nowDate();

	/*
	 * name:         国家和城市添加
	 * describing:   在有国家可选择的情况下进行国家和城市信息的添加
	 * @param        CountryManagerForm cmf
	 * @return       int info 判断是否对数据库修改成功
	 *               
	 */
	public int countryAndCityAdd(CountryManagerForm cmf) {
		
		AppMode.registerUser();

		/*获得FORM里的参数*/
		CountryManagerForm countrymanagerform = cmf;
		int info;
		Statement sql = null;
		ResultSet rs = null;
		String countryname;
		String cityname;
		countryname = countrymanagerform.getCountryname();
		cityname = countrymanagerform.getCityname();
		countryname = change(countryname);
		cityname = change(cityname);
		//System.out.println(cityname);
		//System.out.println(countryname);
		/*临时存放城市名称,用来和页面get来的城市名称比较*/
		String tempcityname = null;
		try {
			db=new DbConnection();
            con=db.getCon();
			sql = con.createStatement();
			rs = sql.executeQuery("SELECT cityname "
                                 +"from country where cityname="
							     + "'" + cityname + "'");
			/*int getRow()得到当前的行号,如果结果集没有行,返回0。如不判断将返回SQL异常*/
			rs.last();
			//int i=rs.getRow();
			//System.out.println(i);
			if (rs.getRow() != 0) {
				rs.first();
				tempcityname = rs.getString(1);
			} else {
				tempcityname = "null";
			}
			System.out.println(tempcityname);
			/*
			 * 构造插入语句
			 * 插入和更新的ID 要根据登陆的用户名改动
			 */
			String condition = "insert into country(" 
                               +"countryname," 
                               +"cityname," 
                               +"insert_date," 
                               +"insert_user_id," 
                               +"update_date," 
                               +"update_user_id) " 
                               +"values('"
					           + countryname
					           + "','"
					           + cityname
					           + "','"
					           + insert_date
					           + "','kangyi'" + ",'" 
                               + update_date 
                               + "','kangyi')";
			/*相等的时候返回插入失败,信息应该是"您插入的信息在数据库中已经存在"*/
			if (!cityname.equals(tempcityname)&&!cityname.equals("")) {
				sql.executeUpdate(condition);
				con.close();
                db.d_close();
                
                AppMode.loginoutUser();
                
				return info = 1;
			} else {
				con.close();
                db.d_close();
                
                AppMode.loginoutUser();
                
				return info = 2;
			}
		} catch (SQLException e) {
			
			AppMode.loginoutUser();
			
            System.out.println(e);
			System.out.print("操作失败");
			return info = 7;
		}
	}

	/*
	 * name:         国家和城市添加
	 * describing:   在没有国家可选择的情况下进行国家和城市信息的添加
	 * @param        CountryManagerForm cmf
	 * @return       int info 判断是否对数据库修改成功
	 *               
	 */
	public int countryAdd(CountryManagerForm cmf) {
		
		AppMode.registerUser();

		/*获得FORM里的参数*/
		CountryManagerForm countrymanagerform = cmf;
		int info;
		Statement sql = null;
		ResultSet rs = null;
		String countryname;
		String cityname;
		countryname = countrymanagerform.getCountryname();
		cityname = countrymanagerform.getCityname();
		countryname = change(countryname);
		cityname = change(cityname);
		/*临时存放城市名称,用来和页面get来的城市名称比较*/
		String tempcityname = null;
		/*临时存放国家名称,用来和页面get来的国家名称比较*/
		String tempcountryname = null;
		try {
			db=new DbConnection();
            con=db.getCon();
			sql = con.createStatement();
			rs = sql.executeQuery("SELECT countryname from country " 
                                 +"where countryname="+ "'" 
                                 + countryname + "'");
			/*int getRow()得到当前的行号,如果结果集没有行,返回0。如不判断将返回SQL异常*/
            rs.last();
            if (rs.getRow() != 0) {
                rs.first();
                tempcountryname = rs.getString(1);
            } else {
                tempcountryname= "null";
                tempcityname = "null";
            }
			rs.last();
			//int i=rs.getRow();
			//System.out.println(i);
			/*
			 * 构造插入语句
			 * 插入和更新的ID 要根据登陆的用户名改动
			 */
			String condition = "insert into country(" 
                               +"countryname," 
                               +"cityname," 
                               +"insert_date," 
                               +"insert_user_id," 
                               +"update_date," 
                               +"update_user_id) " 
                               +"values('"
					           + countryname
					           + "','"
					           + cityname
					           + "','"
					           + insert_date
					           + "','kangyi'" + ",'" 
                               + update_date 
                               + "','kangyi')";
			/*如果结果集为空则执行SQl语句*/
			if (!countryname.equals(tempcountryname)&&!cityname.equals("")&&!countryname.equals("")) {
				sql.executeUpdate(condition);
				con.close();
                db.d_close();
                
                AppMode.loginoutUser();
                
				return info=1;
			} else {
				con.close();
                db.d_close();
                
                AppMode.loginoutUser();
                
				return info=2;
			}
		} catch (SQLException e) {
			
			AppMode.loginoutUser();
			
            System.out.println(e);
			System.out.print("操作失败");
			return info=7;
		}
	}

	/*
	 * name:         国家和城市删除
	 * describing:   删除country表中的国家和城市信息
	 * @param        CountryManagerForm cmf
	 * @return       boolean b 判断是否对数据库修改成功
	 *               (有待改进,返回类型最好为int型可以时间多种异常跳转)
	 */
	public boolean countryAndCityDelete(CountryManagerForm cmf) {
		
		AppMode.registerUser();

		/*获得FORM里的参数*/
		CountryManagerForm countrymanagerform = cmf;
		boolean b = true;
		Statement sql = null;
		ResultSet rs = null;
		String cityname;
		/*从FORM中把城市信息GET出来*/
		cityname = countrymanagerform.getCityname();
		cityname = change(cityname);
		//System.out.println(cityname);
		try {
			db=new DbConnection();
            con=db.getCon();
			sql = con.createStatement();
			/*构造删除语句*/
			String condition = "delete from country " 
                               +"where cityname=" 
                               + "'"+ cityname + "'";
			sql.executeUpdate(condition);
			con.close();
            db.d_close();
            
            AppMode.loginoutUser();
            

⌨️ 快捷键说明

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