您的位置:首页 > 编程语言 > Java开发

[原创] Java JDBC连接数据库,反射创建实体类对象并赋值数据库行记录(支持存储过程)

2013-07-21 16:27 961 查看
1、SqlHelper.java

import java.util.List;

public class Program {

public static void main(String[] args) {
// TODO Auto-generated method stub

String sql = "INSERT INTO [dbo].[UserInfo] ([UserName] ,[LoginName] ,[LoginPwd] ,[UserSex] ,[Birthday]) VALUES ('%s','%s','%s','%s','%s')";
sql = String.format(sql, "wyp1","wyp1","wyp1","1","1981-04-21");
int identyValue = SqlHelper.executeIdentity(sql);
System.out.println(String.format("Identity Value:%d",identyValue));

//        List<UserInfo> list = SqlHelper.executeList(UserInfo.class, "select * from UserInfo");
//        for (UserInfo userInfo : list) {
//            System.out.println(String.format(
//                    "UserInfoId:%d,UserName:%s,LoginName:%s,LoginPwd:%s,UserSex:%s,Birthday:%s",
//                    userInfo.getUserInfoId(),userInfo.getUserName(),userInfo.getLoginName(),userInfo.getLoginPwd(),userInfo.getUserSex()?"男":"女",DateHelper.toString(userInfo.getBirthday())));
//        }

//        SqlParameter param = new SqlParameter("sortField", "[UserInfoId] DESC");
//        List<UserInfo> list = SqlHelper.executeList(UserInfo.class, "{call dbo.UserInfoSelectAll(?)}",param);
//        for (UserInfo userInfo : list) {
//            System.out.println(String.format(
//                    "UserInfoId:%d,UserName:%s,LoginName:%s,LoginPwd:%s,UserSex:%s,Birthday:%s",
//                    userInfo.getUserInfoId(),userInfo.getUserName(),userInfo.getLoginName(),userInfo.getLoginPwd(),userInfo.getUserSex()?"男":"女",DateHelper.toString(userInfo.getBirthday())));
//        }

//        SqlParameter paramSortField = new SqlParameter("sortField", "[UserInfoId] DESC");
//        SqlParameter paramPageSize = new SqlParameter("pageSize", 10);
//        SqlParameter paramPageIndex = new SqlParameter("pageIndex", 1);
//        SqlParameter paramWhere = new SqlParameter("where", "1=1");
//        List<UserInfo> list = SqlHelper.executeList(UserInfo.class, "{call dbo.UserInfoSelectByPagerParams(?,?,?,?)}",paramSortField,paramPageSize,paramPageIndex,paramWhere);
//        for (UserInfo userInfo : list) {
//            System.out.println(String.format(
//                    "UserInfoId:%d,UserName:%s,LoginName:%s,LoginPwd:%s,UserSex:%s,Birthday:%s",
//                    userInfo.getUserInfoId(),userInfo.getUserName(),userInfo.getLoginName(),userInfo.getLoginPwd(),userInfo.getUserSex()?"男":"女",DateHelper.toString(userInfo.getBirthday())));
//        }

//        SqlParameter paramWhere = new SqlParameter("where", "1=1");
//        SqlParameter paramRecordCount = new SqlParameter(java.sql.Types.INTEGER, "recordCount");
//        SqlHelper.executeNonQuery("{call dbo.UserInfoCountByWhere(?,?)}", paramWhere,paramRecordCount);
//        if(paramRecordCount.Value instanceof Integer){
//            Integer recordCount = (Integer)paramRecordCount.Value;
//            System.out.println(String.format("RecordCount:%d",recordCount));
//        }

//        SqlParameter paramWhere = new SqlParameter("where", "1=1");
//        SqlParameter paramRecordCount = new SqlParameter(java.sql.Types.INTEGER, "recordCount");
//        SqlParameter paramReturnValue = new SqlParameter(java.sql.Types.INTEGER);
//        SqlHelper.executeNonQuery("{? = call dbo.UserInfoCountByWhere(?,?)}", paramReturnValue,paramWhere,paramRecordCount);
//        if(paramRecordCount.Value instanceof Integer){
//            Integer recordCount = (Integer)paramRecordCount.Value;
//            System.out.println(String.format("RecordCount:%d",recordCount));
//        }
//        if(paramReturnValue.Value instanceof Integer){
//            Integer returnValue = (Integer)paramReturnValue.Value;
//            System.out.println(String.format("ReturnValue:%d",returnValue));
//        }
}

}


Program
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐