[原创] Java JDBC连接数据库,反射创建实体类对象并赋值数据库行记录(支持存储过程)
2013-07-21 16:27
961 查看
1、SqlHelper.java
Program
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
相关文章推荐
- [转]C#反射,根据反射将数据库查询数据和实体类绑定,并未实体类赋值
- java 利用反射 从数据库取出数据对象list 类似hibernate
- object对象java 利用反射 从数据库取出数据对象list 类似hibernate
- 用java反射技术创建通用的JDBC连接
- java使用JDBC连接数据库,提高查询大量数据速度
- 是用JDBC从数据库中获取数据并以java对象返回
- Java/jdbc连接数据库预处理只能查询一条数据?
- JDBC连接数据库键盘录入数据增删改查学生类对象
- java中将数据库中数据赋值给Map对象
- java 创建连接oracle 数据库并查询数据
- Java_jdbc 基础笔记之九 数据库连接 (查询Customer对象的方法)
- java使用反射机制进行对象间数据赋值获取
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- java 演示使用jdbc-odbc桥连接的方式操作数据库 向数据库中添加数据
- C#反射,根据反射将数据库查询数据和实体类绑定,并为实体类赋值
- 【java基础:JDBC】通过读取配置文件进行数据库连接并读取数据的Demostration
- 从数据库读取数据后利用反射为对应的实体类的属性赋值
- Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)
- JAVA连接各种数据库的JDBC驱动下载及连接字符串URL写法(原创加转帖)
- java JDBC编程——从属性文件读取信息,并创建到数据库的连接