druid连接数据库方法的简单介绍

一.配置文件的设置

 

注意:在配置时,username,password属性不可更改, 注意driverClassName的大小写.

二.项目的建立

 1.druid连接数据库的第一种方法

public class DruidConnectDemo {
    public static void main(String[] args) throws SQLException {
        String driverClassName="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/news-system?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
        String username="root";
        String password="123456";

        DruidDataSource druidDataSource=new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);

        druidDataSource.setMaxActive(10);
        druidDataSource.getConnection();


    }
}
注意:druidDataSource.setMaxActive(10)的括号里面的数字可以设置成别的,另外可以利用for循环进行检验.

2.druid连接数据库的第二种方法

 

public class DruidConnectDemo2 {
    public static void main(String[] args) throws Exception {
//        1.获取properties对象
        Properties properties = new Properties();
        InputStream is = DruidConnectDemo2.class.getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(is);

        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);

        Connection connection=dataSource.getConnection();
        System.out.println(connection);
        connection.close();


    }
}

 注意:jdbc.properties在配置时需要确保正确

3.简单的进行测验

利用增删改查进行验证

 

完整代码:

public class JDBCTemplateDemo {
    static JdbcTemplate jdbcTemplate = new JdbcTemplate(DBUtils.getDataSource());
    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(DBUtils.getDataSource());

        int i = jdbcTemplate.update("insert into t_account(user_name,user_password) values (?,?)", "admin11", "11111");
        System.out.println("影响了数据库" + i + "行");

        int i1=jdbcTemplate.update("insert into t_account(user_name,user_password) values (?,?)","aaaaa","88888");
        System.out.println("影响了数据库"+i1+"行");

        int j = jdbcTemplate.update("update t_account set user_name=?,user_password=? where id=7", "adminadmin", "668866");
        System.out.println("影响了数据库" + j + "行");

        jdbcTemplate.update("delete from t_account where id=?",6);
        System.out.println("已删除");
    
        //查询所有
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper(Account.class);
        List<Account> acccountList = jdbcTemplate.query("select * from t_account where id>?", rowMapper, 0);
        System.out.println(acccountList);
        //查询一条数据,根据id查询一个人的信息
        Account acccount = jdbcTemplate.queryForObject("select * from t_account where id=?", rowMapper, 7);
        System.out.println(acccount);
    }
    public static Account queryAccountById(Integer id){
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper(Account.class);
        try {
            return jdbcTemplate.queryForObject("select * from t_account where id=?", rowMapper, id);
        } catch (Exception e) {
            return null;
        }

    }
}

 结果均能实现.

代码可能略有问题,请多指教!!!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值