今天学习时候碰见特别费解的一个事情刚开始我还打算直接调用bean的赋值方法,结果被秒杀Method setMethod = User.class.getMethod(methodName,new Class[]{Object.class});
setMethod.invoke(user,rs.getObject(colName));这里是为什么呢?上面行参数可以是任何类型对象,调用时候就直接从数据库里面拿出来object也不判断了,各个字段类型肯定会又不一样的,结果出错!好吧,我承认我想简单了,我把JAVA的自动判断类型想得过于智能了。
但是看下面的教程代码,我也很崩溃,因为确实是method.invoke(t, rs.getObject(colName))我执行不过去,但是视频上面过去了,我这边报错很明显argument type mismatch,秒看懂:参数类型不匹配。
求理解,是代码有误,还是JDK原因,学习开始使用的是JDK7U15,视频的是传智的专门讲JDBC的视频mappingUser方法大家能看出来Bean和数据库的各个字段类型,在这里没作用。public class ORMTest
{
public static void main(String[] args) throws Exception
{
User users = getT("select id as Id,name as Name,birthday as Birthday,money as Money from user where id=1",User.class); System.out.println(users);
} static <T>T getT(String sql,Class<T> clazz) throws Exception
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; try
{
conn = JdbcUtils.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colNames = new String[count];
for (int i = 1; i <= count; i++)
{
colNames[i - 1] = rsmd.getColumnLabel(i);
} T t = null;
Method[] methods = clazz.getMethods();
if (rs.next())
{
t = clazz.newInstance();
for (int i = 0; i < colNames.length; i++)
{
String colName = colNames[i]; // 数据库里面的列名,这里可能是SQL的别名 String methodName = "set" + colName; // 方法名 for (Method method : methods)
{
if (methodName.equals(method.getName()))
{
method.invoke(t, rs.getObject(colName));
}
} // Method setMethod = User.class.getMethod(methodName,new Class[]{Object.class});
// setMethod.invoke(user,rs.getObject(colName));
} } return t;
}
finally
{
JdbcUtils.free(rs, ps, conn);
}
}
// private User mappingUser(ResultSet rs) throws SQLException
// {
// User user = new User();
//
// user.setId(rs.getInt("id"));
// user.setName(rs.getString("name"));
// user.setBirthday(rs.getDate("birthday")); // 子类赋值给父类,可以不转换
// user.setMoney(rs.getFloat("money"));
//
// return user;
// }
}
JDBCreflect传智
setMethod.invoke(user,rs.getObject(colName));这里是为什么呢?上面行参数可以是任何类型对象,调用时候就直接从数据库里面拿出来object也不判断了,各个字段类型肯定会又不一样的,结果出错!好吧,我承认我想简单了,我把JAVA的自动判断类型想得过于智能了。
但是看下面的教程代码,我也很崩溃,因为确实是method.invoke(t, rs.getObject(colName))我执行不过去,但是视频上面过去了,我这边报错很明显argument type mismatch,秒看懂:参数类型不匹配。
求理解,是代码有误,还是JDK原因,学习开始使用的是JDK7U15,视频的是传智的专门讲JDBC的视频mappingUser方法大家能看出来Bean和数据库的各个字段类型,在这里没作用。public class ORMTest
{
public static void main(String[] args) throws Exception
{
User users = getT("select id as Id,name as Name,birthday as Birthday,money as Money from user where id=1",User.class); System.out.println(users);
} static <T>T getT(String sql,Class<T> clazz) throws Exception
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; try
{
conn = JdbcUtils.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colNames = new String[count];
for (int i = 1; i <= count; i++)
{
colNames[i - 1] = rsmd.getColumnLabel(i);
} T t = null;
Method[] methods = clazz.getMethods();
if (rs.next())
{
t = clazz.newInstance();
for (int i = 0; i < colNames.length; i++)
{
String colName = colNames[i]; // 数据库里面的列名,这里可能是SQL的别名 String methodName = "set" + colName; // 方法名 for (Method method : methods)
{
if (methodName.equals(method.getName()))
{
method.invoke(t, rs.getObject(colName));
}
} // Method setMethod = User.class.getMethod(methodName,new Class[]{Object.class});
// setMethod.invoke(user,rs.getObject(colName));
} } return t;
}
finally
{
JdbcUtils.free(rs, ps, conn);
}
}
// private User mappingUser(ResultSet rs) throws SQLException
// {
// User user = new User();
//
// user.setId(rs.getInt("id"));
// user.setName(rs.getString("name"));
// user.setBirthday(rs.getDate("birthday")); // 子类赋值给父类,可以不转换
// user.setMoney(rs.getFloat("money"));
//
// return user;
// }
}
JDBCreflect传智
解决方案 »
- (已经求教很多人,均无答案)何如将<xliff:g>标签写入xml文件中
- 关于属小母牛的问题 改了一下cow.grow();的位置,结果咋就不一样了啊 ,想不明白啊 (从网上找的一个例子)
- tomcat 的问题
- 求剪刀石头布程序源程序 好的话我再加分
- 非常简单的问题?
- 请教static问题,请各位大侠指教,不胜感激!
- 怎样把paint(Graphics g)限定在组件中(比如jPanel)中
- 关于常量与继承的问题。(提供简单源代码)
- JBuilder的中文资料!马上给分!!
- java成员初始化时调用的语句需要抛出异常,该怎么处理
- 数据库连不上
- jdk安装好后,环境变量也配置好了,但是出了这种问题
不行,而且真心没必要因为方法本身定义时候 static <T>T getT(String sql,Class<T> clazz)就是传进去一个Class类,泛型只是为了方便以后用,表示穿进去的哪个就是输出的哪个
谢谢回答,我问的是为什么视频里面可以跑起来这个代码,我却运行部起来import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import com.nyohh.jdbc.domain.User;
import com.nyohh.jdbc.utils.JdbcUtils;
/**
*
* 2008-12-7
*
* @author <a href="mailto:[email protected]">liyong</a>
*
*/
public class ORMTest2 { /**
* @param args
* @throws Exception
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws SQLException
*/
public static void main(String[] args) throws SQLException,
IllegalAccessException, InvocationTargetException, Exception {
User user = (User) getObject(
"select id as Id, name as Name, birthday as Birthday, money as Money from user where id=1",
User.class);
System.out.println(user);
} static List<Object> getObjects(String sql, Class clazz)
throws SQLException, Exception, IllegalAccessException,
InvocationTargetException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
String[] colNames = getColNames(rs); List<Object> objects = new ArrayList<Object>();
Method[] ms = clazz.getMethods();
while (rs.next()) {
Object object = clazz.newInstance();
for (int i = 0; i < colNames.length; i++) {
String colName = colNames[i];
String methodName = "set" + colName;
// Object value = rs.getObject(colName);
// try {
// Method m = clazz
// .getMethod(methodName, value.getClass());
// if (m != null)
// m.invoke(object, value);
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// //
// }
for (Method m : ms) {
if (methodName.equals(m.getName())) {
m.invoke(object, rs.getObject(colName));
break;
}
}
objects.add(object);
}
}
return objects;
} finally {
JdbcUtils.free(rs, ps, conn);
}
} private static String[] getColNames(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colNames = new String[count];
for (int i = 1; i <= count; i++) {
colNames[i - 1] = rsmd.getColumnLabel(i);
}
return colNames;
} static Object getObject(String sql, Class clazz) throws SQLException,
Exception, IllegalAccessException, InvocationTargetException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
String[] colNames = getColNames(rs); Object object = null;
Method[] ms = clazz.getMethods();
if (rs.next()) {
object = clazz.newInstance();
for (int i = 0; i < colNames.length; i++) {
String colName = colNames[i];
String methodName = "set" + colName;
// Object value = rs.getObject(colName);
// try {
// Method m = clazz
// .getMethod(methodName, value.getClass());
// if (m != null)
// m.invoke(object, value);
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// //
// }
for (Method m : ms) {
if (methodName.equals(m.getName())) {
m.invoke(object, rs.getObject(colName));
break;
}
}
}
}
return object;
} finally {
JdbcUtils.free(rs, ps, conn);
}
}
}
Object java.sql.ResultSet.getObject(String columnLabel) throws SQLExceptionGets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.