Java Web和Bootstrap进行web开发

Java Web和Bootstrap进行web开发_第1张图片


获取连接池的接口


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目录下




<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driverproperty>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/bookstoreproperty>
        <property name="user">rootproperty>
        <property name="password">rootproperty>

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


    default-config>

    <named-config name="flx">
        <property name="driverClass">com.mysql.jdbc.Driverproperty>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/day16property>
        <property name="user">rootproperty>
        <property name="password">rootproperty>
        <property name="acquireIncrement">50property>
        <property name="initialPoolSize">100property>
        <property name="minPoolSize">50property>
        <property name="maxPoolSize">1000property>
    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 getPageData(int startindex,int pagesize){

        try{
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "select * from book limit ?,?";
            Object params[] = {startindex,pagesize};
            return (List) 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 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) 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)

你可能感兴趣的:(Java-Web)