java 从数据库里面取出数据转化成bean,关键代码如下!高人看看有不对的地方吗?
Connectionconn=GetConnection.getConnection();ResultSetrs=null;PreparedStatementpstmt=...
Connection conn=GetConnection.getConnection();
ResultSet rs=null;
PreparedStatement pstmt=null;
OU_ORGANIZE oo=new OU_ORGANIZE();
try {
pstmt=(PreparedStatement) conn.createStatement();
String sql="select * from OU_ORGANIZE where CODE='"+code+"'";
rs=pstmt.executeQuery(sql);
while(rs.next()){
String className = oo.getClass().getName();
Class<?> clazz = Class.forName(className);
Field[] fields = clazz.getDeclaredFields();//获取类里面的属性
//给属性赋值
for (int i=0;i<rs.getRow();i++){
fields[i].set(oo, rs.getString(i));//--------- 这条语句对吗?从数据库里面的取值循序和对象的赋值循序一样?
}
return oo;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResourceRelase.resourceRelase(conn, pstmt, rs);
return oo; 展开
ResultSet rs=null;
PreparedStatement pstmt=null;
OU_ORGANIZE oo=new OU_ORGANIZE();
try {
pstmt=(PreparedStatement) conn.createStatement();
String sql="select * from OU_ORGANIZE where CODE='"+code+"'";
rs=pstmt.executeQuery(sql);
while(rs.next()){
String className = oo.getClass().getName();
Class<?> clazz = Class.forName(className);
Field[] fields = clazz.getDeclaredFields();//获取类里面的属性
//给属性赋值
for (int i=0;i<rs.getRow();i++){
fields[i].set(oo, rs.getString(i));//--------- 这条语句对吗?从数据库里面的取值循序和对象的赋值循序一样?
}
return oo;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResourceRelase.resourceRelase(conn, pstmt, rs);
return oo; 展开
3个回答
展开全部
package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBManager {
public static Connection con = null;
private static DBManager cm = null;
private DBManager() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hrm", "root", "mysql");
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("链接数据库失败!");
e.printStackTrace();
}
}
public static DBManager getCM() {
if (null == cm)
cm = new DBManager();
return cm;
}
public static Connection getCon() {
return DBManager.getCM().con;
}
public static void closeConection(ResultSet rs,Statement stmt,Connection con)
{
if(null!=rs)
{
try
{
rs.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=stmt)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=con)
{
try
{
con.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConection(Statement stmt,Connection con)
{
if(null!=stmt)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=con)
{
try
{
con.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBManager {
public static Connection con = null;
private static DBManager cm = null;
private DBManager() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hrm", "root", "mysql");
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("链接数据库失败!");
e.printStackTrace();
}
}
public static DBManager getCM() {
if (null == cm)
cm = new DBManager();
return cm;
}
public static Connection getCon() {
return DBManager.getCM().con;
}
public static void closeConection(ResultSet rs,Statement stmt,Connection con)
{
if(null!=rs)
{
try
{
rs.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=stmt)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=con)
{
try
{
con.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConection(Statement stmt,Connection con)
{
if(null!=stmt)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=con)
{
try
{
con.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for (int i=0;i<rs.getRow();i++){
fields[i].set(oo, rs.getString(i));//--------- 这条语句对吗?从数据库里面的取值循序和对象的赋值循序一样?
fields[i].set(oo, rs.getString(i));// rs.getString(i+1);这里面应该是从1开始
fields[i].set(oo, rs.getString(i));//--------- 这条语句对吗?从数据库里面的取值循序和对象的赋值循序一样?
fields[i].set(oo, rs.getString(i));// rs.getString(i+1);这里面应该是从1开始
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为啥通过反射?难道不知道内的具体变量吗?还有就是你可以自己写个小例子试试不就清楚了,问别个还不如自己实验出来,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询