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);
    }

 

posted @ 2016-06-02 07:04  haiziguo  阅读(282)  评论(0编辑  收藏  举报