package org.test;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public class ConnectionTest {
/**
* 匹配指定class中數(shù)據(jù),并返回包含get和set方法的object
*
* @author chenpeng
* @param clazz
* @param beanProperty
* @return
*/
private Object[] beanMatch(Class clazz, String beanProperty) {
Object[] result = new Object[2];
char beanPropertyChars[] = beanProperty.toCharArray();
beanPropertyChars[0] = Character.toUpperCase(beanPropertyChars[0]);
String s = new String(beanPropertyChars);
String names[] = { ("set" + s).intern(), ("get" + s).intern(),
("is" + s).intern(), ("write" + s).intern(),
("read" + s).intern() };
Method getter = null;
Method setter = null;
Method methods[] = clazz.getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
// 只取公共字段
if (!Modifier.isPublic(method.getModifiers()))
continue;
String methodName = method.getName().intern();
for (int j = 0; j < names.length; j++) {
String name = names[j];
if (!name.equals(methodName))
continue;
if (methodName.startsWith("set")
|| methodName.startsWith("read"))
setter = method;
else
getter = method;
}
}
result[0] = getter;
result[1] = setter;
return result;
}
/**
* 為bean自動(dòng)注入數(shù)據(jù)
*
* @author chenpeng
* @param object
* @param beanProperty
*/
private void beanRegister(Object object, String beanProperty, String value) {
Object[] beanObject = beanMatch(object.getClass(), beanProperty);
Object[] cache = new Object[1];
Method getter = (Method) beanObject[0];
Method setter = (Method) beanObject[1];
try {
// 通過(guò)get獲得方法類(lèi)型
String methodType = getter.getReturnType().getName();
if (methodType.equalsIgnoreCase("long")) {
cache[0] = new Long(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("int")
|| methodType.equalsIgnoreCase("integer")) {
cache[0] = new Integer(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("short")) {
cache[0] = new Short(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("float")) {
cache[0] = new Float(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("double")) {
cache[0] = new Double(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("boolean")) {
cache[0] = new Boolean(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("java.lang.String")) {
cache[0] = value;
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("java.io.InputStream")) {
} else if (methodType.equalsIgnoreCase("char")) {
cache[0] = (Character.valueOf(value.charAt(0)));
setter.invoke(object, cache);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 轉(zhuǎn)換connection查詢結(jié)果為指定對(duì)象實(shí)體集合。
*
* @author chenpeng
* @param connection
* @param clazz
* @param sql
* @return
*/
public Collection get(final Connection connection, final Class clazz,
final String sql) {
// 創(chuàng)建PreparedStatement
PreparedStatement ptmt = null;
// 創(chuàng)建resultset
ResultSet rset = null;
// 創(chuàng)建collection
Collection collection = null;
try {
// 賦予實(shí)例
ptmt = connection.prepareStatement(sql);
rset = ptmt.executeQuery();
collection = get(rset, clazz);
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
// 關(guān)閉rs并釋放資源
if (rset != null) {
rset.close();
rset = null;
}
// 關(guān)閉ps并釋放資源
if (ptmt != null) {
ptmt.close();
ptmt = null;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return collection;
}
public Collection get(final ResultSet result, final Class clazz) {
// 創(chuàng)建collection
Collection collection = null;
try {
ResultSetMetaData rsmd = result.getMetaData();
// 獲得數(shù)據(jù)列數(shù)
int cols = rsmd.getColumnCount();
// 創(chuàng)建等同數(shù)據(jù)列數(shù)的arraylist類(lèi)型collection實(shí)例
collection = new ArrayList(cols);
// 遍歷結(jié)果集
while (result.next()) {
// 創(chuàng)建對(duì)象
Object object = null;
try {
// 從class獲得對(duì)象實(shí)體
object = clazz.newInstance();
} catch (Exception e) {
}
// 循環(huán)每條記錄
for (int i = 1; i <= cols; i++) {
beanRegister(object, rsmd.getColumnName(i), result
.getString(i));
}
// 將數(shù)據(jù)插入collection
collection.add(object);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
}
return collection;
}
public static void main(String[] args) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = DriverManager.getConnection(url, "root", "xxxx");
ConnectionTest test = new ConnectionTest();
// Ltest是我測(cè)試用類(lèi),實(shí)際操作請(qǐng)注入相關(guān)對(duì)象,支持set,get,is,read,writer為前綴數(shù)據(jù)對(duì),更多請(qǐng)繼續(xù)添加。
Collection collection = test.get(connection, Ltest.class,
"select * from ltest");
for (Iterator it = collection.iterator(); it.hasNext();) {
Ltest ltest = (Ltest) it.next();
System.out.println(ltest.getId() + ":" + ltest.getName());
}
}
// SQL異常,用于拋出SQL語(yǔ)句處理中所引發(fā)的錯(cuò)誤。
catch (SQLException e) {
System.err.println(e.getMessage());
}
// finally,此標(biāo)識(shí)用以包含必須訪問(wèn)的內(nèi)容。
finally {
try {
// 關(guān)閉rs并釋放資源
if (rs != null) {
rs.close();
rs = null;
}
// 關(guān)閉ps并釋放資源
if (ps != null) {
ps.close();
ps = null;
}
// 關(guān)閉connection并釋放資源
if (connection != null) {
connection.close();
connection = null;
}
// 如果關(guān)閉時(shí)產(chǎn)生異常將由此拋出
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
}
________________________
前一陣寫(xiě)loonframework-db時(shí),寫(xiě)過(guò)類(lèi)似的處理方法,只不過(guò)為了效率我都是直接操作的數(shù)組對(duì)象,現(xiàn)改為集合對(duì)象,減少了一些處理,但大體流 程如此。我框架中cache一直沒(méi)有做完,所以沒(méi)有為您添加,您可以用map之類(lèi)做一個(gè)簡(jiǎn)單的cache來(lái)使用,目前萬(wàn)條數(shù)據(jù)會(huì)較慢于 ResultSet,但是可以正常使用。
更多方法,請(qǐng)見(jiàn)http://looframework.sourceforge.net/或http://www.open-open.com/open198258.htm
國(guó)產(chǎn)JAVA游戲開(kāi)源框架Loonframework 這是一個(gè)基于Java技術(shù)的2D游戲框架,將涉及J2SE,J2ME,JavaFX三個(gè)方面。開(kāi)發(fā)目的在于以腳本化的方式,快速的開(kāi)發(fā)可跨平臺(tái)移植的 Java游戲。并且它本身也是一個(gè)持久層框架,能在框架內(nèi)最大限度的滿足用戶需求。目前展示了一個(gè)仿夢(mèng)幻模擬戰(zhàn)世界觀的AVG+SLG腳本,以 Applet方式展示在looframework.sourceforge.net上運(yùn)行,也可以下載此Jar包后雙擊運(yùn)行。由于是采用Graphics 直接繪制界面,所以僅使用了最基礎(chǔ)的AWT作為表示,有很好的移植可能性。功能上,采用腳本方式動(dòng)態(tài)生成界面與對(duì)話情節(jié),重用可行性高。 項(xiàng)目潛力上,隨著日后JavaFX技術(shù)的改進(jìn)與普及,本項(xiàng)目還會(huì)有一次質(zhì)上的飛躍。在WEB2.0概念深入人心,如貓游記等AJAX游戲都能夠有人去玩的 今天,利用JAVA開(kāi)發(fā)的平臺(tái)無(wú)視,且能如RMXP由普通用戶即可制作的游戲框架 (而且,利用Java特性,完全可以做到由用戶通過(guò)網(wǎng)頁(yè)定制游戲并展示,甚至可以考慮提供一個(gè)類(lèi)似于土豆的平臺(tái),用以展示用戶DIY自己的游戲或相關(guān)圖形 資源。)
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public class ConnectionTest {
/**
* 匹配指定class中數(shù)據(jù),并返回包含get和set方法的object
*
* @author chenpeng
* @param clazz
* @param beanProperty
* @return
*/
private Object[] beanMatch(Class clazz, String beanProperty) {
Object[] result = new Object[2];
char beanPropertyChars[] = beanProperty.toCharArray();
beanPropertyChars[0] = Character.toUpperCase(beanPropertyChars[0]);
String s = new String(beanPropertyChars);
String names[] = { ("set" + s).intern(), ("get" + s).intern(),
("is" + s).intern(), ("write" + s).intern(),
("read" + s).intern() };
Method getter = null;
Method setter = null;
Method methods[] = clazz.getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
// 只取公共字段
if (!Modifier.isPublic(method.getModifiers()))
continue;
String methodName = method.getName().intern();
for (int j = 0; j < names.length; j++) {
String name = names[j];
if (!name.equals(methodName))
continue;
if (methodName.startsWith("set")
|| methodName.startsWith("read"))
setter = method;
else
getter = method;
}
}
result[0] = getter;
result[1] = setter;
return result;
}
/**
* 為bean自動(dòng)注入數(shù)據(jù)
*
* @author chenpeng
* @param object
* @param beanProperty
*/
private void beanRegister(Object object, String beanProperty, String value) {
Object[] beanObject = beanMatch(object.getClass(), beanProperty);
Object[] cache = new Object[1];
Method getter = (Method) beanObject[0];
Method setter = (Method) beanObject[1];
try {
// 通過(guò)get獲得方法類(lèi)型
String methodType = getter.getReturnType().getName();
if (methodType.equalsIgnoreCase("long")) {
cache[0] = new Long(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("int")
|| methodType.equalsIgnoreCase("integer")) {
cache[0] = new Integer(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("short")) {
cache[0] = new Short(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("float")) {
cache[0] = new Float(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("double")) {
cache[0] = new Double(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("boolean")) {
cache[0] = new Boolean(value);
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("java.lang.String")) {
cache[0] = value;
setter.invoke(object, cache);
} else if (methodType.equalsIgnoreCase("java.io.InputStream")) {
} else if (methodType.equalsIgnoreCase("char")) {
cache[0] = (Character.valueOf(value.charAt(0)));
setter.invoke(object, cache);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 轉(zhuǎn)換connection查詢結(jié)果為指定對(duì)象實(shí)體集合。
*
* @author chenpeng
* @param connection
* @param clazz
* @param sql
* @return
*/
public Collection get(final Connection connection, final Class clazz,
final String sql) {
// 創(chuàng)建PreparedStatement
PreparedStatement ptmt = null;
// 創(chuàng)建resultset
ResultSet rset = null;
// 創(chuàng)建collection
Collection collection = null;
try {
// 賦予實(shí)例
ptmt = connection.prepareStatement(sql);
rset = ptmt.executeQuery();
collection = get(rset, clazz);
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
// 關(guān)閉rs并釋放資源
if (rset != null) {
rset.close();
rset = null;
}
// 關(guān)閉ps并釋放資源
if (ptmt != null) {
ptmt.close();
ptmt = null;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return collection;
}
public Collection get(final ResultSet result, final Class clazz) {
// 創(chuàng)建collection
Collection collection = null;
try {
ResultSetMetaData rsmd = result.getMetaData();
// 獲得數(shù)據(jù)列數(shù)
int cols = rsmd.getColumnCount();
// 創(chuàng)建等同數(shù)據(jù)列數(shù)的arraylist類(lèi)型collection實(shí)例
collection = new ArrayList(cols);
// 遍歷結(jié)果集
while (result.next()) {
// 創(chuàng)建對(duì)象
Object object = null;
try {
// 從class獲得對(duì)象實(shí)體
object = clazz.newInstance();
} catch (Exception e) {
}
// 循環(huán)每條記錄
for (int i = 1; i <= cols; i++) {
beanRegister(object, rsmd.getColumnName(i), result
.getString(i));
}
// 將數(shù)據(jù)插入collection
collection.add(object);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
}
return collection;
}
public static void main(String[] args) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true";
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = DriverManager.getConnection(url, "root", "xxxx");
ConnectionTest test = new ConnectionTest();
// Ltest是我測(cè)試用類(lèi),實(shí)際操作請(qǐng)注入相關(guān)對(duì)象,支持set,get,is,read,writer為前綴數(shù)據(jù)對(duì),更多請(qǐng)繼續(xù)添加。
Collection collection = test.get(connection, Ltest.class,
"select * from ltest");
for (Iterator it = collection.iterator(); it.hasNext();) {
Ltest ltest = (Ltest) it.next();
System.out.println(ltest.getId() + ":" + ltest.getName());
}
}
// SQL異常,用于拋出SQL語(yǔ)句處理中所引發(fā)的錯(cuò)誤。
catch (SQLException e) {
System.err.println(e.getMessage());
}
// finally,此標(biāo)識(shí)用以包含必須訪問(wèn)的內(nèi)容。
finally {
try {
// 關(guān)閉rs并釋放資源
if (rs != null) {
rs.close();
rs = null;
}
// 關(guān)閉ps并釋放資源
if (ps != null) {
ps.close();
ps = null;
}
// 關(guān)閉connection并釋放資源
if (connection != null) {
connection.close();
connection = null;
}
// 如果關(guān)閉時(shí)產(chǎn)生異常將由此拋出
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
}
________________________
前一陣寫(xiě)loonframework-db時(shí),寫(xiě)過(guò)類(lèi)似的處理方法,只不過(guò)為了效率我都是直接操作的數(shù)組對(duì)象,現(xiàn)改為集合對(duì)象,減少了一些處理,但大體流 程如此。我框架中cache一直沒(méi)有做完,所以沒(méi)有為您添加,您可以用map之類(lèi)做一個(gè)簡(jiǎn)單的cache來(lái)使用,目前萬(wàn)條數(shù)據(jù)會(huì)較慢于 ResultSet,但是可以正常使用。
更多方法,請(qǐng)見(jiàn)http://looframework.sourceforge.net/或http://www.open-open.com/open198258.htm
國(guó)產(chǎn)JAVA游戲開(kāi)源框架Loonframework 這是一個(gè)基于Java技術(shù)的2D游戲框架,將涉及J2SE,J2ME,JavaFX三個(gè)方面。開(kāi)發(fā)目的在于以腳本化的方式,快速的開(kāi)發(fā)可跨平臺(tái)移植的 Java游戲。并且它本身也是一個(gè)持久層框架,能在框架內(nèi)最大限度的滿足用戶需求。目前展示了一個(gè)仿夢(mèng)幻模擬戰(zhàn)世界觀的AVG+SLG腳本,以 Applet方式展示在looframework.sourceforge.net上運(yùn)行,也可以下載此Jar包后雙擊運(yùn)行。由于是采用Graphics 直接繪制界面,所以僅使用了最基礎(chǔ)的AWT作為表示,有很好的移植可能性。功能上,采用腳本方式動(dòng)態(tài)生成界面與對(duì)話情節(jié),重用可行性高。 項(xiàng)目潛力上,隨著日后JavaFX技術(shù)的改進(jìn)與普及,本項(xiàng)目還會(huì)有一次質(zhì)上的飛躍。在WEB2.0概念深入人心,如貓游記等AJAX游戲都能夠有人去玩的 今天,利用JAVA開(kāi)發(fā)的平臺(tái)無(wú)視,且能如RMXP由普通用戶即可制作的游戲框架 (而且,利用Java特性,完全可以做到由用戶通過(guò)網(wǎng)頁(yè)定制游戲并展示,甚至可以考慮提供一個(gè)類(lèi)似于土豆的平臺(tái),用以展示用戶DIY自己的游戲或相關(guān)圖形 資源。)
浙公網(wǎng)安備 33010602011771號(hào)