从Jabberd2迁移到Openfire(二)


开始导出数据

我们首先创建两个Bean对象分别保存用户的基本信息和用户列表信息,由于篇幅有限,两个Bean对象都省略了get,set方法,以及使用全部成员属性作为参数的构造函数。这两个Bean对象,重要的处理在于重写的equals方法,equals方法里的处理关系到User对象和Group对象是否属于同一个Jid。
User.java

private String username;
private String password;
private String realm;
private String nickname;
private String n_family;
private String n_given;
private String n_middle;

public boolean equals(Object obj) {
if (obj instanceof User) {
String username = ((User) obj).getUsername();
return this.username.equals(username);
} else if (obj instanceof Group) {
String collection_ower = ((Group) obj).getCollection_owner();
return collection_ower.equals(this.username + “@” + this.realm);
} else {
return false;
}
}

public int hashCode() {
// TODO Auto-generated method stub
return this.username.hashCode();
}

Group.java

private String collection_owner;
private String jid;
private String name;
private String group;

public boolean equals(Object obj) {
if(obj instanceof Group) {
String collection_owner = ((Group) obj).getCollection_owner();
String group = ((Group) obj).getGroup();
return this.collection_owner.equals(collection_owner)
&& this.group.trim().equals(group.trim());
} else if(obj instanceof User) {
String username = ((User) obj).getUsername();
String realm = ((User) obj).getRealm();
return this.collection_owner.equals((username + “@” + realm));
} else {
return false;
}
}

public int hashCode() {
return this.collection_owner.hashCode();
}

两个Bean对象创建好了之后,我们再来创建一个DBConnection.java用来管理数据库连接。使用单例模式,简单的查询,使用一个连接即可,提高效率。
DBConnection.java

private static Connection conn;

private DBConnection() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(
“jdbc:mysql://[hostname]:3306/[database]”, “[username]”,
“[password]”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void initConnection() {
if(conn == null) {
new DBConnection();
}
}

public static Connection getConnection() {
return conn;
}

public static void destoryConnction() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s