Java Web和Bootstrap进行web开发

来源:互联网 发布:mysql语句更改排序 编辑:程序博客网 时间:2024/06/18 17:51

这里写图片描述


获取连接池的接口

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
原创粉丝点击