Java Web和Bootstrap进行web开发

这里写图片描述


获取连接池的接口


import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {

    private static DataSource ds = null;
    static{
        ds = new ComboPooledDataSource();
    }

    public static DataSource getDataSource(){
        return ds;
    }

    public static Connection getConnection() throws SQLException{
        return ds.getConnection();
    }

}


获取UUID的接口

import java.util.UUID;

public class WebUtils {

    public static String makeID(){
        return UUID.randomUUID().toString();
    }

}


c3p0-config.xml 这个文件可以直接放在src目录下

<?xml version="1.0" encoding="UTF-8"?>


<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/bookstore</property>
        <property name="user">root</property>
        <property name="password">root</property>

        <property name="acquireIncrement">5</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">5</property>
        <property name="maxPoolSize">20</property>


    </default-config>

    <named-config name="flx">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/day16</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="acquireIncrement">50</property>
        <property name="initialPoolSize">100</property>
        <property name="minPoolSize">50</property>
        <property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
    </named-config>

</c3p0-config>


BookDao常用代码模板

public class BookDaoImpl implements BookDao {

    /*
     * private String id;
    private String name;
    private String author;
    private double price;
    private String image;  //记住图片的名称
    private String description;
    private String category_id;
     */

    public void add(Book book){
        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "insert into book(id,name,author,price,image,description,category_id) values(?,?,?,?,?,?,?)";
            Object params[] = {book.getId(),book.getName(),book.getAuthor(),book.getPrice(),book.getImage(),book.getDescription(),book.getCategory_id()};
            runner.update(sql, params);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public Book find(String id){

        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "select * from book where id=?";
            return (Book) runner.query(sql, id, new BeanHandler(Book.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public List<Book> getPageData(int startindex,int pagesize){

        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "select * from book limit ?,?";
            Object params[] = {startindex,pagesize};
            return (List<Book>) runner.query(sql, params, new BeanListHandler(Book.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public int getTotalRecord(){
        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());

            String sql = "select count(*) from book";
            long totalrecord = (Long) runner.query(sql, new ScalarHandler());
            return (int)totalrecord;
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public List<Book> getPageData(int startindex,int pagesize,String category_id){

        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "select * from book where category_id=? limit ?,?";
            Object params[] = {category_id,startindex,pagesize};
            return (List<Book>) runner.query(sql, params, new BeanListHandler(Book.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public int getTotalRecord(String category_id){
        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());

            String sql = "select count(*) from book where category_id=?";
            long totalrecord = (Long) runner.query(sql, category_id,new ScalarHandler());
            return (int)totalrecord;
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

FR:海涛高软(QQ技术交流群:386476712)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值