Struts1.2实现MySQL数据库分页

来源:本站原创 作者:佚名 2008-04-30 出处:pcdog.com

.net  apache  eclipse  java  jsp  
上一页 1 2 3 4 5 下一页 
    这里用一个配置文件,database.properties 里面存放数据库的URL 、Driver、Username和Password等,修改成你本机的相应数据,能打开数据库就好。
    database.properties内容如下:
    driver=org.gjt.mm.mysql.Driver
    url=jdbc:mysql://localhost:3306/page
    user=root
    password=1234

    下面是我用的数据库增删改查的Bean-> DBbusiness.java

    package com.yourcompany.dbtool;

    import java.sql.*;

    /**
     * 这是一个连接数据库具有增删改查的Bean
     * @author 树下无影
     *
     */
    public class DBbusiness {
    /*
     * 定义连接参数等
     */
    Connection conn = null;

    PreparedStatement psps = null;

    ResultSet rs = null;
    public DBbusiness (){

    }
    /*
     * 定义公用的Connection
     */
    public Connection getConn() {

     try {
        DBConnection db=DBConnection.getInstance();
        Connection conx = db.getConnection();
        return conx;
     } catch (Exception e) {
         System.out.println("Connection连接出错");

     }
     return null;

    }
    /*
     * 获取数据(查询)方法
     */
    public ResultSet getData(String sql) {
         try {

        conn = getConn();
        psps = conn.prepareStatement(sql);
        rs = psps.executeQuery();
     } catch (Exception e) {
         System.out.println("查询数据库操作出错");
     }
     return rs;


    }
        /*
         * 定义插入数据和更新的方法
         */
    public boolean insert(String sql) {
         try {
            conn = getConn();
            psps = conn.prepareStatement(sql);
            psps.executeUpdate();
            return true;
         } catch (Exception e) {
             System.out.println("数据库更新出错");

         }
         return false;

    }
    /*
     * 定义创建数据库和表的方法
     */
    public boolean create(String sql) {
         try {
            conn = getConn();
            psps = conn.prepareStatement(sql);
            psps.execute();
            return true;
         } catch (Exception e) {

         }
         return false;

    }

    /*
     * 定义关闭连接的方法
     */
    public void allClose() {
         try {
           if (rs != null)
                rs.close();
            if (psps != null)
                psps.close();
            if (conn != null)
            {
                DBConnection db=DBConnection.getInstance();
                db.closeConnection(conn);
            }
         } catch (Exception e) {
             System.out.println("数据库关闭操作出错");

        }
    }

    }
    第四步:创建实体类
   
    在vo包里,创建一个实体类,这步COPY过去就是。
    Product.java
    package com.yourcompany.vo;

    public class Product {
    String id;
    String sortid;
    String name;
    String price;
    String saleprice;
    String descript;
    String contents;
    String saledate;
    String salecount;
    String image;
    public Product(){}
        public Product(String id,String sortid,String name,String price,
             String saleprice,String descript,String contents,
             String saledate,String salecount,String image){
         this.id=id;
         this.sortid=sortid;
         this.name=name;
         this.price=price;
         this.saleprice=saleprice;
         this.descript=descript;
         this.contents=contents;
         this.saledate=saledate;
         this.salecount=salecount;
         this.image=image;

        }
    public String getContents() {
         return contents;
    }

    public void setContents(String contents) {
         this.contents = contents;
    }

    public String getDescript() {
         return descript;
    }

    public void setDescript(String descript) {
         this.descript = descript;
    }

    public String getId() {
         return id;
    }

    public void setId(String id) {
         this.id = id;
    }

    public String getImage() {
         return image;
    }

    public void setImage(String image) {
         this.image = image;
    }

    public String getName() {
         return name;
    }

    public void setName(String name) {
         this.name = name;
    }

    public String getPrice() {
         return price;
    }

    public void setPrice(String price) {
         this.price = price;
    }

    public String getSalecount() {
         return salecount;
    }

    public void setSalecount(String salecount) {
         this.salecount = salecount;
    }

    public String getSaledate() {
         return saledate;
    }

    public void setSaledate(String saledate) {
         this.saledate = saledate;
    }

    public String getSaleprice() {
         return saleprice;
    }

    public void setSaleprice(String saleprice) {
         this.saleprice = saleprice;
    }

    public String getSortid() {
         return sortid;
    }

    public void setSortid(String sortid) {
         this.sortid = sortid;
    }

    }


更多内容请看PCdog.com--MySQL数据备份  数据库相关文章专题
上一页 1 2 3 4 5 下一页 
上一篇:Sun&MySQL全球巡展中国站正式启动
下一篇:DBA的利器:企业监控器简化MySQL管理