2014年2月25日星期二

java简单工程模式

实现一个计算器

1.新建计算类
  
public class Operation {
	private double _numa;
	private double _numb;
	private double _result;

	/**
	 * 获取_numa
	 * 
	 * @return _numa
	 */

	public double get_numa() {
		return _numa;
	}

	/**
	 * 设置_numa
	 * 
	 * @param _numa
	 */
	public void set_numa(double _numa) {
		this._numa = _numa;
	}

	/**
	 * 获取_numb
	 * 
	 * @return _numb
	 */

	public double get_numb() {
		return _numb;
	}

	/**
	 * 设置_numb
	 * 
	 * @param _numb
	 */
	public void set_numb(double _numb) {
		this._numb = _numb;
	}

	/**
	 * 获取_result
	 * 
	 * @return _result
	 */

	public double get_result() {
		return _result;
	}

	/**
	 * 设置_result
	 * 
	 * @param _result
	 */
	public void set_result(double _result) {
		this._result = _result;
	}
}

2.新建一个加法类继承OpetationAdd并重写get_result()方法
  
public class OperationFactory {
	/**
	 * 创建运算类
	 */
	public static Operation createOperation(String operator) {
		Operation operation = null;
		switch (operator) {
		case "+":
			operation = new OperationAdd();
			break;

		default:
			break;
		}
		return operation;
	}

	/**
	 * 描述方法的作用
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Operation op = new OperationFactory().createOperation("+");
		op.set_numa(2);
		op.set_numb(2);
		System.out.println(op.get_result());
	}

}
3.新建OperationFactory类并新建createOperation()方法实现抽象运算
  
public class OperationFactory {
	/**
	 * 创建运算类
	 */
	public static Operation createOperation(String operator) {
		Operation operation = null;
		switch (operator) {
		case "+":
			operation = new OperationAdd();
			break;

		default:
			break;
		}
		return operation;
	}

	

}
4.测试运行加法
  
/**
	 * 描述方法的作用
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Operation op = new OperationFactory().createOperation("+");
		op.set_numa(2);
		op.set_numb(2);
		System.out.println(op.get_result());
	}
以后需要修改加法只要修改OperationAdd类即可
如果需要添加其他复杂运算,只要添加相关类并在OperationFactory类的createOperation()方法中添加switch分支即可

2014年2月24日星期一

Android SQLite使用demo

Android SQLite使用demo

1.建立dbhelper类:
Context context;
public static int DB_VERSION = 1;
public static String DB_NAME = "d_weather.db";
public static String TABLE_NAME = "t_weather";

/**
* 构造方法
*
* @param context
* @param name
* @param factory
* @param version
*/
public SQLiteDataHelp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// SQLiteOpenHelper子类必须要的一个构造函数
}

/**
* 构造方法
*/
public SQLiteDataHelp(Context context) {
// 必须实现父类构造方法
this(context, DB_VERSION);
}

public SQLiteDataHelp(Context context, int version) {
// 必须实现父类构造方法
this(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
// 数据库初始化
db.execSQL("create table if not exists " + TABLE_NAME + "("
+ "w_id integer primary key," + "w_name varchar,"
+ "w_temp varchar)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 当打开数据库时传入的版本号与当前的版本号不同时会调用该方法

}

public void insert(ContentValues contentValues) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, contentValues);
db.close();
}

/**
*
* query
*
* @return
*/
public Cursor query() {
SQLiteDatabase db = getReadableDatabase();
// 获取Cursor
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null,
null);
return c;
}

//更新数据库的内容
public void update(ContentValues values, String whereClause, String[]whereArgs){
   SQLiteDatabase db = getWritableDatabase();
   db.update(TABLE_NAME, values, whereClause, whereArgs);
}

// 根据唯一标识_id 来删除数据
public void delete(int id) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, "w_id=?", new String[] { String.valueOf(id) });
}

2.调用CRUD方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CRUD操作
// insert
SQLiteDataHelp dbhelp = new SQLiteDataHelp(MainActivity.this);
ContentValues contentValues = new ContentValues();
contentValues.put("w_name", "北京");
contentValues.put("w_temp", "22°C");
// dbhelp.insert(contentValues);
// delete
// dbhelp.delete(1);
// 修改
dbhelp.update(contentValues, "w_name='北京'", null);
// 查询
Cursor cursor = dbhelp.query();
if (cursor.getCount() == 0) {
GlobalConstant.i("no data!!");
} else {
while (cursor.moveToNext()) {
GlobalConstant.i("w_id--->"
+ cursor.getInt(cursor.getColumnIndex("w_id")));
GlobalConstant.i("w_name--->"
+ cursor.getString(cursor.getColumnIndex("w_name")));
GlobalConstant.i("w_temp--->"
+ cursor.getString(cursor.getColumnIndex("w_temp")));
GlobalConstant.i("-----------------");
}
}
}

2013年8月8日星期四

mysql命令语法3


  • 添加外键语法:


alter table t_user add constraint u_paid_id2p_id
foreign key (u_paid_id)
references t_paid (p_id) 
on delete cascade on update cascade;

注意key后面不要写表名,如果写成这样:

alter table t_user add constraint u_paid_id2p_id
foreign key t_user(u_paid_id)
references t_paid (p_id)
on delete cascade on update cascade;

会报错:ERROR 1061 (42000): Duplicate key name 't_user'
原因是mysql会把表名't_user'当成外键名称,然后再次新建外键时候会报重复外键的错,具体信息请参看
mysql>show innodb status;
找到LATEST FOREIGN KEY ERROR一行

  • 删除外键出错
ERROR:Error on rename of "xxxx" to "xxx"(error:152)
解决方法:正确删除索引后在删除key
出错原因:使用了错误的外键名称,详见show innodb status命令

2013年8月7日星期三

mysql语法命令2

清空所有字段:      truncate table xf_tb
查看key:                show keys from xf_bt
查看索引:           show index from xf_bt
删除索引:           alter table xf_tb drop index idx_us
查看建表语句:      show create table xf_tb
修改表名:           rename table xf_tb to xf_bt
查看是否有其他进程连接:      show process list;
停止,启动mysql服务          service mysql start/stop
重命名数据库:
                         1.进入mysql的bin目录
                         2.进入mysql命令行:create database shop(新数据库名)
                         3.use shop
                         4.\. e:\shop.sql

2012年8月24日星期五

eclipse 4.2.0 juno 同步svn错误解决


右键使用svn share project出现以下错误:

org.tigris.subversion.javahl.ClientException: Unsupported working copy format
svn: This client is too old to work with working copy 'E:\android workspace\xf19_GPS_share'.  You need
to get a newer Subversion client, or to downgrade this working copy.
See http://subversion.tigris.org/faq.html#working-copy-format-change
for details.

org.tigris.subversion.javahl.ClientException: Unsupported working copy format
svn: This client is too old to work with working copy 'E:\android workspace\xf19_GPS_share'.  You need
to get a newer Subversion client, or to downgrade this working copy.
See http://subversion.tigris.org/faq.html#working-copy-format-change
for details.



解决办法:
1.下载最新的subversion 1.8.x,在install new software中添加最新subversion,地址为:http://subclipse.tigris.org/update_1.8.x/
2.在项目右键-team-share project,然后直接下一步完成即可

升级成功:
subversion:1.8.3
subclipse:1.8.16

Eclipse 4.2.0下svn使用

安装svn

1.选择安装插件


2.添加插件地址:http://subclipse.tigris.org/update_1.4.x

点击确定重启即可安装完成

myEclipse9.0下svn使用

配置svn

1.下载SVN插件: site-1.8.16.zip
http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240
2.全部文件解压到:X:\...\MyEclipse 9.0\dropins(MyEclipse的安装目录)
3.重启MyEclipse即可出现SVN(重启会很慢,等大概1分钟)


myeclipse项目同步到svn


1.安装TortoiseSVN
2.在项目文件下右击,选择svn checkout
3.URl of repository填入:
https://xunfang-19-gps-service.googlecode.com/svn/trunk/ 
(google code注册的项目名字,注意开头为https,否则commit会提示405错误)
checkout directory填入:
E:\myEclipse workspace\GpsShareServer (注意选择文件夹,要在当前路径生成.svn文件夹)
点击确定提交

4.输入用户名密码

5.提交成功

还原版本


1.还原到指定版本:

2.在其他机器恢复程序:
 
必须安装有TortoiseSVN,在文件夹右击,选择svn checkout,然后填入信息