• JDBC


    一、jdbc基本应用

    1.Connection

    DriverManager.getConnection(prop.getProperty("url"),
    prop.getProperty("username"), prop.getProperty("password"))

    2.PreStatement

        con = JdbcUtils.getConnection();
                String sql = "insert into stu value(?,?,?,?)";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, student.getNumber());
                pstmt.setString(2, student.getName());
                pstmt.setInt(3, student.getAge());
                pstmt.setString(4, student.getGender());
                pstmt.executeUpdate();

    3.JDBCUtils

    private static final String dbconfig = "dbconfig.properties";
        private static Properties prop = new Properties();
        static {
            try {
                InputStream in = Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(dbconfig);
                prop.load(in);
                Class.forName(prop.getProperty("driverClassName"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        public static Connection getConnection() {
            try {
                return DriverManager.getConnection(prop.getProperty("url"),
                        prop.getProperty("username"), prop.getProperty("password"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    4.dbconfig.properties(如果按照上面的路径写的话 ,文件需要写在包的外面)

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=UTF8
    username=root
    password=123456

     二、c3p0和commons.dbutils

    c3p0主要用于控制数据源

    0.c3p0xml配置(需放到src文件夹下)

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <!-- 这是默认配置信息 -->
        <default-config> 
            <!-- 连接四大参数配置 -->
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="user">root</property>
            <property name="password">123456</property>
            <!-- 池参数配置 -->
            <property name="acquireIncrement">3</property>
            <property name="initialPoolSize">10</property>
            <property name="minPoolSize">2</property>
            <property name="maxPoolSize">10</property>
        </default-config>
        
        <!-- 专门为oracle提供的配置信息 -->
        <named-config name="oracle-config"> 
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="user">root</property>
            <property name="password">123</property>
            <property name="acquireIncrement">3</property>
            <property name="initialPoolSize">10</property>
            <property name="minPoolSize">2</property>
            <property name="maxPoolSize">10</property>
        </named-config>
    
    </c3p0-config>

    1.包

    2.获取数据源

    ComboPooledDataSource ds = new ComboPooledDataSource();
    ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
    ds.setUser("root");
    ds.setPassword("123456");
    ds.setDriverClass("com.mysql.jdbc.Driver");
    
    ds.setAcquireIncrement(5);
    ds.setInitialPoolSize(20);
    ds.setMinPoolSize(2);
    ds.setMaxPoolSize(50);
    
    Connection con = ds.getConnection();
    
    System.out.println(con);
    con.close();

    3.commons.dbutils

    包:commons.dbutils

    @Test
        public void fun2() throws SQLException, PropertyVetoException {
            QueryRunner qr = new QueryRunner();
            String sql = "INSERT INTO stu(`name`,age) VALUE(?,?)";
            int i = qr.update(JdbcUtils.getConnection(), sql, "zhangSan", 22);
            System.out.println(i);
        }
    
        @Test
        public void fun1() throws SQLException, PropertyVetoException {
            DataSource ds = JdbcUtils.getDataSource();
            QueryRunner qr = new QueryRunner(ds);
            String sql = "select * from stu where id=?";
            Map<String, Object> map = qr.query(sql, new MapHandler(), 1);
            for (String item : map.keySet()) {
                System.out.println(item + "--" + map.get(item));
            }
        }
    
        @Test
        public void fun3() throws SQLException, PropertyVetoException {
            DataSource ds = JdbcUtils.getDataSource();
            QueryRunner qr = new QueryRunner(ds);
            String sql = "select * from stu where id=?";
            Student student = qr.query(sql,
                    new BeanHandler<Student>(Student.class), 1);
            System.out.println(student.toString());
        }
    
        @Test
        public void fun4() throws SQLException, PropertyVetoException {
            DataSource ds = JdbcUtils.getDataSource();
            QueryRunner qr = new QueryRunner(ds);
            String sql = "select * from stu";
            List<Student> student = qr.query(sql, new BeanListHandler<Student>(
                    Student.class));
            System.out.println(student.toString());
        }
    
        @Test
        public void fun5() throws SQLException, PropertyVetoException {
            DataSource ds = JdbcUtils.getDataSource();
            QueryRunner qr = new QueryRunner(ds);
            String sql = "select * from stu";
            List<Object> list = qr.query(sql, new ColumnListHandler("name"));
            for (Object s : list) {
                System.out.println(s);
            }
        }
    
        @Test
        public void fun6() throws SQLException, PropertyVetoException {
            DataSource ds = JdbcUtils.getDataSource();
            QueryRunner qr = new QueryRunner(ds);
            String sql = "select count(*) from stu";
            Number number = (Number) qr.query(sql, new ScalarHandler());
            int cnt = number.intValue();
            System.out.println(cnt);
        }
  • 相关阅读:
    反向迭代器实现字符串逆序
    排序更新
    快速排序的实现方法,调用sort函数
    第k个素数
    ACM Online Judge
    Hadoop系列(一)开篇简介
    Hadoop系列(番外) hadoop3.1.2的完全分布式部署
    synchronized关键字
    Oracle的运行
    在Oracle下创建数据库,连接数据库
  • 原文地址:https://www.cnblogs.com/lzhp/p/5551704.html
Copyright © 2020-2023  润新知