Java SQLite 初探

最近开始新的项目,初步规划使用Java,以及SQLite做CS架构的应用。目前用的是SQLite JDBC Driver 3.7.2。

使用的时候,首先引用

import java.sql.*

然后连接到数据库:

java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:sqlite:sample.db");

执行SQL语句,准备查询字符串,以及执行查询:

java.sql.Statement statement = connection.createStatement();
statement.executeUpdate("drop table if exists sample");
statement.executeUpdate("create table sample(int_1 integer, str_1 string)");
statement.executeUpdate("insert into sample values(1, 'first')");
statement.executeUpdate("insert into sample values(2, 'second')");
java.sql.ResultSet rs = statement.executeQuery("select * from sample");

从结果集中取出内容:

while(rs.next())
{
    System.out.println("string = " + rs.getString("str_1"));
    System.out.println("integer = " + rs.getInt("int_1"));
}

到此为止上述代码完成了最基础的SQL语句的执行操作,SQLite数据库和java语言环境成功地连接起来了。

然而必须要说的是,这里使用的JDBC Driver尽管实现了上述操作,达到了在java环境中使用SQLite数据库的目标,但是数据库性能真的是令人堪忧。目前我们还没有进一步的需求使用更高性能的数据库,所以依然在使用JDBC Driver,但是如果日后对数据库的要求上来了,我估计在维持java和SQLite两个选项不变的前提下,是很需要把JDBC Driver换成相应的C Wrapper的。

站点迁移完成

今天完成了从旧站点fishinbox到新站点staryland.com的迁移工作。旧的fishinbox.tk站点依然存在,只是不会继续维护了。

新的站点依然使用cloudflare的CDN服务,国内访问速度不是很乐观,但是好在方便省心,激进的优化设置也许会有兼容性问题,不过就目前来看,在本站上运行状况良好。

此次迁移也标志着我正式开始记录日志了,思绪或者经验总是要与人分享的,记录下来宜人宜己。本站内容主要集中在编程(C++,C#,Java,PHP),数据库应用(SQL)等方面上,但日后会更加丰富。

成绩查询器失效说明

关于昨天下午发生的成绩查询器不稳定以及之后的不能查询成绩的说明。

自2012年初到现在大概有1年的时间了,很多人使用了我发布的成绩查询器,感谢大家的支持。经同学们的反馈,考虑到原1.0版本易用性以及安全性并不是特别理想,在考完后15号-16号期间完成了成绩查询器1.1的重编写与调试。

1.1版本有以下新的功能:

1、15号上午:URP系统以及查询窗口并列显示,且URP页面默认导向本学期成绩页面;用户可以方便的使用上方URP系统的参数来查询详细成绩组成以及暂存成绩的录入状态。

2、15号下午:加入了程序可用性验证,以减少程序潜在的不恰当使用的后果。

3、16号上午:一键查询当前登陆用户本学期所有详细成绩及状态

这里要说明一下,程序使用的是一个接口,对该接口教务系统开发者未正确处理,导致了接口公共空间可用性的存在。

16号增加的一键查询功能,可以使得用户机在短时间内(内网的话1000ms以内)产生对该接口大量的数据请求。程序测试过程中以及后来的小范围分发测试,很可能引起了教务服务器维护人员的注意,并且针对此做出修改(事实上修复这个缺陷应当是一个相当简单的事)。

所以很遗憾,该缺陷被修补后,所有我发布的成绩查询程序都不能够继续正常使用,不论是0.x测试版,1.0版还是1.1测试版。

目前有迹象表明可以使用另一个接口访问成绩数据,但是出于各种考虑不会发布相关查询软件。

再次感谢大家的支持。

CET大学英语测试无须保护盾查询

前两天写了个php版本的CET成绩查询工具,已经开源到GitHub

https://github.com/fishinbox/CET-Result-Checkout

总体思想是 HTTP_REFER欺骗,目前HTTP_REFER欺骗主要有2种形式,一种是利用本地浏览器插件或者是封包截取工具修改Refer标头,另外一种是通过中转服务器伪造Refer标头来请求信息。

有能力使用本地插件的同学应当不用我多说,把Refer标头修改为:

http://cet.99sushe.com 就可以通过http://cet.99sushe.com/s 页面查询成绩了,post域内容为id,即准考证号,name,即gbk编码的姓名前两个字。

github托管的是99宿舍和学信网两个查询方式,页面很干净,没有广告和其他乱七八糟的东西。

希望测试以下的可以见http://phptest2.sinaapp.com/,不过请大家手下留情,SAE的云豆小弟不多,不要刷爆了。

微软Office Excel开发——打开Excel文档

今天在一个VB交流群上面见到人问怎么打开一个有写保护的Excel文档而不需要用户额外操作。其实这个问题自己查阅MSDN就完全可以解决的,但是最后不得已还是受人以鱼了,整理在此,也算是自己的回顾吧。

本次探讨的背景是微软dotNet框架,主要代码语言是C#/CSharp。编写环境Visual Studio 2010,测试环境Windows 7 64 bit & Office 2010 Pro Plus 64bit。理论上支持的Excel版本最低到Excel 2003,在低的版本就不敢保证了。

所有Excel相关操作使用微软Microsoft.Office.Interop.Excel命名空间,我们这里先做引用:

using Excel = Microsoft.Office.Interop.Excel;

言归正传。

取得与Excel程序的通信

Excel.Application excel = new Excel.Application();
excel.Visible = false;

声明Excel程序实例,并且不显示Excel的界面。

Workbook.Open()方法

打开Excel文档使用下列方法:

String filename="filename"; //文件路径
Excel.Workbook workbook = excel.Workbooks.Open(filename);

其中Workbook.Open的原型是

Workbook Open(
	string Filename,
	Object UpdateLinks,
	Object ReadOnly,
	Object Format,
	Object Password,
	Object WriteResPassword,
	Object IgnoreReadOnlyRecommended,
	Object Origin,
	Object Delimiter,
	Object Editable,
	Object Notify,
	Object Converter,
	Object AddToMru,
	Object Local,
	Object CorruptLoad
)

Filename
类型: System.String
必需字符串,给出所打开工作簿的路径。

UpdateLinks
类型: System.Object
可选对象。 Specifies the way links in the file are updated. If this argument is omitted, the user is prompted to specify how links will be updated. Otherwise, this argument is one of the values listed in the following table.
If Microsoft Excel is opening a file in the WKS, WK1, or WK3 format and the UpdateLinks argument is 2, Microsoft Excel generates charts from the graphs attached to the file. If the argument is 0, no charts are created.

ReadOnly
类型: System.Object
可选对象。设为true则以只读模式打开工作簿。

Format
类型: System.Object
可选对象。 If Microsoft Excel is opening a text file, this argument specifies the delimiter character, as shown in the following table. If this argument is omitted, the current delimiter is used.

Password
类型: System.Object
可选对象。 打开受保护工作簿所需的密码字符串。 如果该参数被忽略而工作簿是受保护的,用户将会被要求提供密码。

WriteResPassword
类型: System.Object
可选对象。 修改写保护工作簿所需的写保护密码字符串。 如果该参数被忽略而工作簿是写保护的,用户将会被要求提供写保护密码。

IgnoreReadOnlyRecommended
类型: System.Object
可选对象。设置为true来避免显示推荐只读模式打开消息。

Origin
类型: System.Object
可选对象。 If the file is a text file, this argument indicates where it originated (so that code pages and Carriage Return/Line Feed (CR/LF) can be mapped correctly). Can be one of the following XlPlatform constants: xlMacintosh, xlWindows, or xlMSDOS. If this argument is omitted, the current operating system is used.

Delimiter
类型: System.Object
可选对象。 如果目标文件是一个文本文件且Format参数设置为6, 则本参数是一个用来确定分隔符的String对象。 只有字符串首位的字符会被用作为分隔符。

Editable
类型: System.Object
可选对象。 If the file is a Microsoft Excel 4.0 add-in, this argument is True to open the add-in so that it’s a visible window. If this argument is False or omitted, the add-in is opened as hidden, and it cannot be unhidden. This option doesn’t apply to add-ins created in Microsoft Excel 5.0 or later. If the file is an Excel template, use True to open the specified template for editing or False to open a new workbook based on the specified template. The default value is False.

Notify
类型: System.Object
可选对象。 If the file cannot be opened in read/write mode, this argument is True to add the file to the file notification list. Microsoft Excel will open the file as read-only, poll the file notification list, and then notify the user when the file becomes available. If this argument is False or omitted, no notification is requested, and any attempts to open an unavailable file will fail.

Converter
类型: System.Object
可选对象。 The index of the first file converter to try when opening the file. The specified file converter is tried first; if this converter doesn’t recognize the file, all other converters are tried. The converter index consists of the row numbers of the converters returned by the FileConverters property.

AddToMru
类型: System.Object
可选对象。 True to add this workbook to the list of recently used files. The default value is False.

Local
类型: System.Object
可选对象。 True saves files against the language of Microsoft Excel (including control panel settings). False (default) saves files against the language of Visual Basic for Applications (VBA) (which is typically U.S. English unless the VBA project where Workbooks.Open is run from is an old internationalized XL5/95 VBA project).

CorruptLoad
类型: System.Object
可选对象。 Can be one of the following constants: xlNormalLoad, xlRepairFile, and xlExtractData. The default behavior if no value is specified is usually normal, but may be safe load or data recovery if Excel has already attempted to open the file. The first attempt is normal. If Excel stops operating while opening the file, the second attempt is safe load. If Excel again stops operating, the next attempt is data recovery.

可选参数的忽略
我们注意到,只有第一个参数filename是必须参数,其他的都是可选参数。 不想明确的可选参数可以使用Type.Missing(C#)或missing(VB)代替。

与Excel交互

我们这里只做最基础的交互演示,详情请查技术文档。

Excel.Worksheet worksheet = workbook.ActiveSheet;
Range range = worksheet.get_Range("A1",Type.Missing);
//get_Range(cell1,cell2)可以接受2个参数,可以是Excel A1 Style的字符串代表目标单元格区域的左上及右下单元格
//range.Value将设置或返回指定区域的值
//range.Value2将设置或返回指定单元格的值
</pre>
<h1>与Excel通信的结束</h1>
我们使用_Application接口下的Quit()方法,必须先调用Quit方法,Excel进程才能被释放:
<pre lang="csharp">
excel.Quit();
excel=null;

示例

我们现在有一个名为test.xlsx的Excel文档,分别有文档保护密码”pswd1″以及文档写保护密码”pswd2″。我们不希望修改Excel文档内容,只需要读取A1单元格的内容并且显示出来,并且不需要用户的额外操作。

    String passwd = "pswd1";
    String WRPasswd = "pswd2";
    Excel.Application excel = new Excel.Application();
    Excel.Workbook workbook = excel.Workbooks.Open(@"E:ShowCaseExcel1test.xlsx", Type.Missing, true, Type.Missing,passwd);
    Excel.Worksheet worksheet = workbook.ActiveSheet;
    String content=worksheet.get_Range("A1", Type.Missing).Value2;
    excel.Quit();
    excel = null;
    MessageBox.Show(content);

MediaWiki File Cache

由于前几日新开的Wiki站服务器压力有点小大,于是琢磨着开启缓存。本来站点使用了CloudFlare的CDN,但可惜命中率低下,服务器压力并不能缓解。于是只能退而求其次,使用文件缓存。当然MediaWiki的文件缓存功能和很多主流php应用一样,都是针对非注册用户的。
MediaWiki由于众所皆知的原因,并没有提供强大地后台管理页面,对Wiki站的配置主要集中在对LocalSettings.php这个文件的修改了。
MediaWiki 对File Cache主要由以下参数控制:

主要选项


$wgUseFileCache = true; /* default: false */
$wgFileCacheDirectory = "$IP/cache";
$wgShowIPinHeader = false;

其中$wgShowIPinHeader = false;选项为必须项,但是自2009年一月27日版本r46374起,该操作由系统自动完成,所以具体还要看你的Wiki版本是多少。
$wgFileCacheDirectory = “$IP/cache”;其中$IP意为指Install Path,即安装路径,是不能够通过LocalSettings.php设置的。$IP/后面接着的字符串即为你所指定的缓存文件夹。一般使用cache或者filecache。
$wgUseFileCache = true;该选项设置是否使用文件缓存功能,默认值是false,即不开启。

可选选项


$wgUseGzip = true;

该选项设置缓存文件为gzip压缩过的,当Client请求AcceptEncoding包含gzip时,即会返回*.html.gz的内容给服务器。
但是注意!在使用该参数之前,请确保您的MediaWiki并没有开启gzip,否则将会返回给用户2次gzip过后的内容,导致浏览器不能正常解析。本站Wiki通过修改index.php以及load.php的代码,为全Wiki站启用了gzip压缩,而第一次设置的时候没有考虑到这一点,还同时启用了$wgUseGzip导致浏览页面一片乱码,最终找到问题,并且此篇文章写成前也在官方Wiki站找到了相关说明:File Cache Compression
必须说,文件缓存还是很管用的,我的Wiki站服务器很差劲,不开缓存基本上需要20s才能完成一次渲染,而开启缓存之后,页面载入时间降到了2-3s,对于纯浏览用户来说,这是个巨大地提高。

坦克世界 XML 文档提取工具

坦克世界 XML 文档提取工具
在坦克世界中,xml文档是经过自定义压缩的,记载了游戏的各种设定、参数,主要集中在 resscriptsitem_defs 中。更多信息请移步http://xvm.garphy.com/?page_id=129

作者 Hamilleton/ZZR

协议:CC-BY-NC-SA

知识共享(CC):署名(BY)-非商业性(NC)-相同方式共享(SA)

本软件应WOT/xml项目组的邀请而编写,目的是为坦克世界的xml整理工作提供方便,但是很多功能也可用于其他通用xml文档。

功能:

解码坦克世界xml文档
通用xml文档导出到Excel
通用xml文档生成设置档并根据设置档导出到Excel

运行需求:
最低
Microsoft .Net Framework 4 Client Profile
Microsoft Office Excel 2003*
推荐
Microsoft .Net Framework 4
Microsoft Office Excel 2010*带*号项目并非强制必需,仅在需要使用Excel导出功能时才有依赖。

软件的详细使用指导请前往 http://wiki.fishinbox.tk/index.php/WOT_XML_Extractor

Effective C++ Item 12: Prefer initialization to assignment in constructors

When you write the NamedPtr constructor, you have to transfer the values of the parameters to the corresponding
data members. There are two ways to do this. The first is to use the member initialization list:

template<class T>
NamedPtr<t>::NamedPtr(const string& initName, T *initPtr )
: name(initName), ptr(initPtr)
{}

The second is to make assignments in the constructor body:

template<class T>
NamedPtr<t>::NamedPtr(const string& initName, T *initPtr)
{
    name = initName;
    ptr = initPtr;
}

There are important differences between these two approaches.
From a purely pragmatic point of view, there are times when the initialization list must be used. In particular,
const and reference members may only be initialized, never assigned. So, if you decided that a NamedPtr
object could never change its name or its pointer, you might follow the advice of Item 21 and declare the
members const:

template<class T>
class NamedPtr {
public:
    NamedPtr(const string& initName, T *initPtr);
...
private:
    const string name;
    T * const ptr;
};

This class definition requires that you use a member initialization list, because const members may only be
initialized, never assigned.
You’d obtain very different behavior if you decided that a NamedPtr object should contain a reference to an
existing name. Even so, you’d still have to initialize the reference on your constructors’ member initialization
lists. Of course, you could also combine the two, yielding NamedPtr objects with read-only access to names
that might be modified outside the class:

template<class T>
class NamedPtr {
public:
    NamedPtr(const string& initName, T *initPtr);
...
private:
    const string& name; // must be initialized via
    // initializer list
    T * const ptr; // must be initialized via
    // initializer list
};

The original class template, however, contains no const or reference members. Even so, using a member
initialization list is still preferable to performing assignments inside the constructor. This time the reason is
efficiency. When a member initialization list is used, only a single string member function is called. When
assignment inside the constructor is used, two are called. To understand why, consider what happens when you
declare a NamedPtr object.
Construction of objects proceeds in two phases:
1. Initialization of data members. (See also Item 13.)
2. Execution of the body of the constructor that was called.
(For objects with base classes, base class member initialization and constructor body execution occurs prior to
that for derived classes.)

SQL data character set configuration

Days ago I posted an article containing Greek characters which didn’t go as expected, but I failed to look further in this issue. I totally didn’t realize that it has some thing to do with my database character set configuration.

But today, a friend post a comment on my page and it’s so sad that the comment became multiple question marks “????…”. And this is when I realized that there was something wrong and by Googling I gained the knowledge that it may has something to do with my American free web space provider, who provided SQL database but in latin character set as default, which leads to the whole following things.

It’s tough to change the character set field by filed but finally I made it, and this web site is supposed to be compatible with all UTF-8 characters.

Overload the Logical Operators to Sort Strings Alphabetically

Overload the Logical Operators to Sort Strings Alphabetically

In c++, we have function like strcmp(const char* c1,const char* c2) to help us decide which is alphabetically before the other. Yet we still want to use a more easy-going function as a bool operator < or something else like other logical operators.

And here we have a customized user-define class String, which is incomplete due this is only for demonstration of overloading operators.

#include <iostream>
#include <iomanip>
using namespace std;

class String{

	char* chars;

public:

	int length;

	String String::operator =(const char* r){
		length=strlen(r);
		chars=(char*)malloc((length+1)*sizeof(char));
		for(int i=0;i<=length;i++){
			this->chars[i]=r[i];
		}
		return *this;
	}

	String String::operator =(String r){
		length=r.length;
		chars=(char*)malloc((length+1)*sizeof(char));
		for(int i=0;i<=length;i++){
			this->chars[i]=r[i];
		}
		return *this;
	}

	char &String::operator [](int idx){
		return chars[idx];
	}

	bool operator <(String r)
	{
		for(int idx=0;idx<=(length<r.length?length:r.length);idx++)
		{
			if(tolower(chars[idx])<tolower(r[idx]))
				return true;
			else
				if(tolower(chars[idx])>tolower(r[idx]))
					return false;
				else
					continue;
		}
		return false;
	}

	bool operator >(String r)
	{
		return r<*this;
	}

	bool operator ==(String r)
	{
		return !(*this>r)&&!(*this<r);
	}

	bool operator !=(String r)
	{
		return !(*this==r);
	}

	bool operator <=(String r)
	{
		return !(*this>r);
	}

	bool operator >=(String r)
	{
		return !(*this<r);
	}

	friend ostream&operator <<(ostream &os,String &right);

	friend istream&operator <<(istream &is,String &right);

	String(int n){
		length=n;
		chars=new char[length];
	}

	String (){
		length=0;
	}

	String (const char* cs){
		*this=cs;
	}

};

ostream & operator<<(ostream &os,  String &right)
{
	for(int i=0;i<=right.length;i++)
	{
		os<<right[i];
	}

	return os;
}

istream & operator>>(istream &is,String &right)
{
	int idx=2;
	char* tmpcs=new char[idx];
	char c;
	int i=0;
	for(;;i++)
	{
		c=is.get();
		if(c!='n')
		{
			if(i>=idx)
			{
				idx=i+1;
				tmpcs=(char*)realloc(tmpcs,idx*sizeof(char));
			}
			if(isalpha(c))
			{
				tmpcs[i]=c;
			}

		}
		else
		{
			if(i>=idx)
			{
				idx=i+1;
				tmpcs=(char*)realloc(tmpcs,idx*sizeof(char));
			}
			tmpcs[i]='';
			break;
		}
	}
	right=tmpcs;
	return is;

}

template <typename T>
void sort(T *source,int startidx,int endidx)
{
	for(int i=startidx+1;i<=endidx;i++)
	{
		for(int j=startidx;j<i;j++)
		{
			if(source[i]<source[j])
			{
				T key=source[i];
				for(int k=i;k>j;k--)
				{
					source[k]=source[k-1];
				}
				source[j]=key;
				break;
			}
		}
	}
}
int main()
{
	String group[5];

	cout<<"Please input 5 words"<<endl;
	for(int i=0;i<5;i++)
	{
		cin>>group[i];
	}
	sort(group,0,4);
	cout<<"after the sorting"<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<group[i]<<endl;
	}
	system("pause");
	return 0;
}