• JavaWeb网上图书商城完整项目-数据库操作工具类


    1、首先安装数据库,在windows上安装和在unix上面安装环境不一样,我在自己的本地电脑上安装,安装成功之后,如果使用navicat远程工具访问,需要允许mysql远程能被访问

    方法二、直接授权(推荐)

      从任何主机上使用root用户,密码:youpassword(你的root密码)连接到mysql服务器:

    # mysql -u root -proot 
    mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;

    操作完后切记执行以下命令刷新权限 

    FLUSH PRIVILEGES 

    工程中需要添加下面的jar包

    数据库表:

    我们来看程序的代码:

    package com.weiyuan.test;
    
    public class Person {
        private String pid;
        private String pname;
        private int age;
        private String sex;
        public String getPid() {
            return pid;
        }
        public void setPid(String pid) {
            this.pid = pid;
        }
        public String getPname() {
            return pname;
        }
        public void setPname(String pname) {
            this.pname = pname;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + ((pid == null) ? 0 : pid.hashCode());
            result = prime * result + ((pname == null) ? 0 : pname.hashCode());
            result = prime * result + ((sex == null) ? 0 : sex.hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person other = (Person) obj;
            if (age != other.age)
                return false;
            if (pid == null) {
                if (other.pid != null)
                    return false;
            } else if (!pid.equals(other.pid))
                return false;
            if (pname == null) {
                if (other.pname != null)
                    return false;
            } else if (!pname.equals(other.pname))
                return false;
            if (sex == null) {
                if (other.sex != null)
                    return false;
            } else if (!sex.equals(other.sex))
                return false;
            return true;
        }
        public Person(String pid, String pname, int age, String sex) {
            super();
            this.pid = pid;
            this.pname = pname;
            this.age = age;
            this.sex = sex;
        }
        public Person(){
            
        }
        @Override
        public String toString() {
            return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age
                    + ", sex=" + sex + "]";
        }
        
    
    }
    package com.weiyuan.test;
    
    import java.sql.SQLException;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.MapHandler;
    import org.apache.commons.dbutils.handlers.MapListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import cn.itcast.jdbc.JdbcUtils;
    import cn.itcast.jdbc.TxQueryRunner;
    
    /**
     * 对数据库操作类的测试
     * */
    public class TxRunnerTest {
    
        @Test
        public void testUpdate() throws SQLException{    
            QueryRunner run = new TxQueryRunner();
            String sql = "insert into person(pid,pname,age,sex)values(?,?,?,?)";
            Object[] params = {"2","张三",20,"男"};
            run.update(sql,params);
            System.out.println("ok");
            
        }
        
        /*
         * 测试批处理的方法
         * */
        
        @Test
        public void testUpdate2() throws SQLException{
            try{
                //开启事物
                JdbcUtils.beginTransaction();
                QueryRunner run = new TxQueryRunner();
                String sql = "insert into person(pid,pname,age,sex)values(?,?,?,?)";
                Object[] params = {"2","张三",20,"男"};
                run.update(sql,params);
                
        
                Object[] params2 = {"20","张三",20,"男"};
                run.update(sql,params2);
                
                //提交事物
                JdbcUtils.commitTransaction();
            }catch(Exception e){
                //如果存在异常回滚事物
                JdbcUtils.rollbackTransaction();
            }
        }
        
        
        /**测试查询方法
         * @throws SQLException 
         * 注意事项:1 person类中的字段必须和数据库中表的字段名称一模一样
         * 2 person存在public Person(){
            
        }的构造函数
         * */
        @Test
        public void testQuery() throws SQLException{
            
            QueryRunner run = new TxQueryRunner();
            String sql = "select * from person where pid=?";
            Person p = run.query(sql, new BeanHandler<Person>(Person.class),"20");
            System.out.println(p);
            
        }
        
        @Test
        public void testQuery2() throws SQLException{
            
            QueryRunner run = new TxQueryRunner();
            String sql = "select * from person";
            List<Person> lists = run.query(sql, new BeanListHandler<Person>(Person.class));
            System.out.println(lists);
            
        }
        
        @Test
        public void testQuery3() throws SQLException{
            /*
             * {sex=男, age=20, pname=张三, pid=20}
             * */
            QueryRunner run = new TxQueryRunner();
            String sql = "select * from person where pid=?";
            Map<String,Object> map= run.query(sql, 
                    new MapHandler(),"20");
            System.out.println(map);
            
        }
        
        @Test
        public void testQuery4() throws SQLException{
            /*
             * [{sex=男, age=1, pname=l;l;sl;, pid=1}, {sex=男, age=20, pname=张三, pid=2}, 
             * {sex=男, age=20, pname=张三, pid=2}, {sex=男, age=20, pname=张三, pid=2}, 
             * {sex=男, age=20, pname=张三, pid=2}, {sex=男, age=20, pname=张三, pid=2},
             *  {sex=男, age=20, pname=张三, pid=20}, {sex=男, age=20, pname=张三, pid=2}, {sex=男, age=20, pname=张三, pid=20}, 
             * {sex=男, age=20, pname=张三, pid=2}]
             * */
            QueryRunner run = new TxQueryRunner();
            String sql = "select * from person";
            List<Map<String,Object>> maps= run.query(sql, 
                    new MapListHandler());
            System.out.println(maps);
            
        }
        
        @Test
        public void testQuery5() throws SQLException{
            /*
             * 统计表的总共有多少条记录
             * ScalarHandler将单行单列的结果封装成Object对象
             * 直接如果直接将obj转成Int对象不同的驱动可能存在不一样的问题,
             * 所以先转换成Number对象,在转化成int类型
             * */
            QueryRunner run = new TxQueryRunner();
            String sql = "select count(*) from person";
            Object obj = run.query(sql, new ScalarHandler());
            Number number = (Number) obj;
            int count = number.intValue();
            System.out.println(count);
            
        }
    }
  • 相关阅读:
    html 3
    html标签2
    html标签
    2017.4.27
    2017.4.26
    2017.4.25
    2017.4.20
    2017.1.18
    2017.4.17
    2017.4.16.
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/6779153.html
Copyright © 2020-2023  润新知