📄 jdbcdao.java
字号:
/**
* Copyright 2005 Jdon.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jdon.framework.test.dao;
import javax.sql.DataSource;
import com.jdon.servicelocator.ejb.ServiceLocator;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.jdon.controller.model.PageIterator;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import com.jdon.servicelocator.ServiceLocatorException;
import com.jdon.model.query.PageIteratorSolver;
import java.sql.PreparedStatement;
import com.jdon.framework.test.model.UserTest;
import com.jdon.framework.test.JNDINames;
public class JdbcDAO implements java.io.Serializable{
private final static Logger logger = Logger.getLogger(JdbcDAO.class);
private DataSource dataSource = null;
private PageIteratorSolver pageIteratorSolverOfUser = null;
private PageIteratorSolver pageIteratorSolverOfAddress = null;
private final static String GET_FIELD =
"select * from user where id = ?";
private final static String GET_ALL_ITEMS_ALLCOUNT =
"select count(1) from user ";
private final static String GET_ALL_ITEMS =
"select id from user ORDER BY sortId asc ";
/** 查询所有Item的PageIterator 结束 */
public JdbcDAO() {
try {
ServiceLocator sl = new ServiceLocator();
dataSource = (DataSource) sl.getDataSource(JNDINames.DATASOURCE);
pageIteratorSolverOfUser = new PageIteratorSolver(dataSource);
pageIteratorSolverOfAddress = new PageIteratorSolver(dataSource);
}
catch (ServiceLocatorException slx) {
logger.error(slx);
}
}
public UserTest getField(String Id) throws Exception {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
UserTest ret = null;
try {
c = dataSource.getConnection();
ps = c.prepareStatement(GET_FIELD,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ps.setString(1, Id);
rs = ps.executeQuery();
if (rs.first()) {
ret = new UserTest();
ret.setName(rs.getString("name"));
ret.setUserId(rs.getString("userId"));
}
}
catch (SQLException se) {
throw new Exception("SQLException: " + se.getMessage());
}
finally {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (c != null)
c.close();
}
return ret;
}
public PageIterator getUsers(int start, int count) throws
Exception {
return pageIteratorSolverOfUser.getDatas("", GET_ALL_ITEMS_ALLCOUNT,
GET_ALL_ITEMS, start, count);
}
public void clearCacheOfItem() {
pageIteratorSolverOfUser.clearCache();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -