[Java] 纯文本查看 复制代码
public class MyDataSource implements DataSource {
// 将一些连接存入到内存中,可以定义一个集合,用于存储连接对象。
private List<Connection> connList = new ArrayList<Connection>();
// 在初始化的时候构造方法中提供一些连接
public MyDataSource() {
// 初始化连接:
for(int i = 1;i<=3;i++){
// 向集合中存入连接:
connList.add(JDBCUtils.getConnection());
}
}
// 从连接池中获得连接的方法
@Override
public Connection getConnection() throws SQLException {
Connection conn = connList.remove(0);
// 增强连接:
MyConnectionWrapper connWrapper = new MyConnectionWrapper(conn, connList);
return connWrapper;
}
// 编写一个归还连接的方法:
public void addBack(Connection conn){
connList.add(conn);
}
//(还有其他方法,不需要重写,直接调用父类)...
}
[Java] 纯文本查看 复制代码
interface Waiter{
public void server();
}
public class Waitress implements Waiter{
public void server(){
System.out.println(“服务中…”);
}
}
public class WaitressWrapper implements Waiter{
private Waiter waiter;
public WaitressWrapper(Waiter waiter){
this.waiter = waiter;
}
public void server(){
System.out.println(“微笑…”)
waiter.server();
}
}
[Java] 纯文本查看 复制代码
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = -1287011149634093674L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
resp.setContentType("text/html;charset=UTF-8")
// 1.接收表单提交的参数.
String username = req.getParameter("username");
String password = req.getParameter("password");
// 2.封装到实体对象中.
User user = new User();
user.setUsername(username);
user.setPassword(password);
// 3.调用业务层处理数据.
UserService userService = new UserService();
User existUser = userService.login(user);
// 4.根据处理结果显示信息(页面跳转).
if (existUser == null) {
resp.getWriter().println("<h1><font color='red'>登录失败</font></h1>");
}else{
resp.getWriter().println("<h1><font color='green'>登陆成功</font></h1>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//UserServlet类
public class UserService {
public User login(User user) throws SQLException {
UserDao userDao = new UserDao();
return userDao.login(user);
}
}
//UserDao类
public class UserDao {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public User login(User user) throws SQLException {
QueryRunner queryRunner = new QueryRunner(dataSource);
User user2 = queryRunner.query("select * from user where username = ? and password = ?",
new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
return user2;
}
}
[Java] 纯文本查看 复制代码
//创建一个用于提取的工具类:
public class JdbcUtils {
//在c3p0-config.xml文件配置好的情况下,通过此方法获取c3p0的DataSource的方法:
public static DataSource getC3P0DataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
return dataSource;
}
//获取Druid的DataSource的方法:
public static DataSource getDruidDataSource(){
Properties pp = new Properties();
DataSource dataSource = null;
try {
dataSource = DruidDataSourceFactory.createDataSource(pp);
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
}
//获取用c3p0方式连接时的获取连接的方法:
public static Connection getC3P0Connection(){
Connection conn = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
conn = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//获取用Druid方式连接时的获取连接的方法:
public static Connection getDruidConnection(){
Connection conn = null;
try {
Properties pp = new Properties();
pp.load(new FileInputStream("src/name.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(pp);
conn = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//资源释放的方法:(conn的close方法已经是增强过得了,是将连接存放会连接池的方法,不是销毁)
public static void release(Connection conn,Statement stmt){
JdbcUtils.release(conn, stmt,null);
}
public static void release(Connection conn,Statement stmt,ResultSet rs){
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
}
}
[Java] 纯文本查看 复制代码
public class DbUtils {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
@Test
public void showInsert() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
queryRunner.update("insert into account values(null,?,?)","huaye",12000);
}
@Test
public void showUpdate() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
queryRunner.update("update account set name = ?,money = ? where id = 1","mengjinghua",5000);
}
@Test
public void showDelete() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
queryRunner.update("delete from account where id = ?",1);
}
@Test
public void showSelectOne() throws SQLException {
QueryRunner queryRunner = new QueryRunner(dataSource);
Account account = queryRunner.query("select * from account where id = ?", new ResultSetHandler<Account>(){
@Override
public Account handle(ResultSet rs) throws SQLException {
Account account = new Account();
if (rs.next()) {
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
}
return account;
}
},1);
System.out.println(account);
}
@Test
public void showSelectAll() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
List<Account> list = queryRunner.query("select * from account", new ResultSetHandler<List<Account>>(){
@Override
public List<Account> handle(ResultSet rs) throws SQLException {
List<Account> list = new ArrayList<Account>();
while(rs.next()){
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
list.add(account);
}
return list;
}
});
System.out.println(list);
}
@Test
public void showArrayHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
Object[] objs = queryRunner.query("select * from account where id = ?",new ArrayHandler(),1);
System.out.println(Arrays.toString(objs));
}
@Test
public void showArrayListHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
List<Object[]> list = queryRunner.query("select * from account",new ArrayListHandler());
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
}
@Test
/**
* 重点
*/
public void showBeanHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
Account account = queryRunner.query("select * from account where id = ?",
new BeanHandler<Account>(Account.class),1);
System.out.println(account);
}
@Test
/**
* 重点
*/
public void showBeanListHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
List<Account> list = queryRunner.query("select * from account",
new BeanListHandler<Account>(Account.class));
for (Account account : list) {
System.out.println(account);
}
}
@Test
public void showMapHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
Map<String,Object> map = queryRunner.query("select * from account where id = ?", new MapHandler(),1);
System.out.println(map);
}
@Test
public void showMapListHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
List<Map<String, Object>> list = queryRunner.query("select * from account",new MapListHandler());
for (Map<String, Object> map : list) {
System.out.println(map);
}
}
@Test
public void showColumnListHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
List<Object> list = queryRunner.query("select name from account", new ColumnListHandler("name"));
for (Object object : list) {
System.out.println(object);
}
}
@Test
/**
* 重点
*/
public void showScalarHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
Object obj = queryRunner.query("select count(*) from account", new ScalarHandler());
System.out.println(obj);
}
@Test
public void showKeyedHandler() throws SQLException{
QueryRunner queryRunner = new QueryRunner(dataSource);
Map<Object, Map<String, Object>> map = queryRunner.query("select * from account",new KeyedHandler("name"));
for (Object key: map.keySet()) {
System.out.println(key+"\t"+map.get(key));
}
}
}