概念&入门


public static void main(String[] args) throws Exception {
    //1 导入jar包

    //2注册驱动
    Class.forName("com.mysql.jdbc.Driver");
    //3 获取连接
    Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/db4", "root", "root");
    //4.获取执行者对象
    Statement stat = conn.createStatement();
    //5.执行sql 语句,且接收结果
    String sql = "select * from user";
    ResultSet rs = stat.executeQuery(sql);
    //处理结果
    while (rs.next()){
        System.out.println(rs.getInt("id")+"----"+rs.getString("NAME")+"----"+rs.getInt("age"));
    }
    //释放资源
    rs.close();
    conn.close();
}

工具类



public class JDBCUtils {
    //私有构造方法
    private JDBCUtils(){}

    //声明所需要的配置变量
    private static String driverClass;
    private static String url;
    private static String username;
    private static String password;
    private static Connection conn;

    static {

        try {
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties");
//            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
            Properties prop = new Properties();
            prop.load(is);
            driverClass = prop.getProperty("driverClass");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
            Class.forName(driverClass);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //连接方法

    public static Connection getConnection() {
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }
    //释放资源
    public static void close(Connection conn, Statement stat, ResultSet rs){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void close(Connection conn, Statement stat){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}

sql注入攻击

JDBC管理事务




连接池

dataSource

private static Connection conn;
    private static List<Connection> pool;
    public MyDataSource1(String hostToConnectTo, int portToConnectTo, Properties info, String databaseToConnectTo, String url,Connection conn,List<Connection> pool) throws SQLException {

        super(hostToConnectTo, portToConnectTo, info, databaseToConnectTo, url);
        this.conn = conn;
        this.pool = pool;
    }

    @Override
    public void close() throws SQLException {
        pool.add(conn);
    }



动态代理




public static void main(String[] args) {
        Student stu = new Student();
    /*
        要求:在不改动Student类中任何的代码的前提下,通过study方法输出一句话:来黑马学习
        类加载器:和被代理对象使用相同的类加载器
        接口类型Class数组:和被代理对象使用相同接口
        代理规则:完成代理增强的功能
         */
        StudentInterface proxyStu = (StudentInterface) Proxy.newProxyInstance(stu.getClass().getClassLoader(), new Class[]{StudentInterface.class}, new InvocationHandler() {
            /*
                执行Student类中所有的方法都会经过invoke方法
                对method方法进行判断
                如果是study,则对其增强
                如果不是,还调用学生对象原有的功能即可
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("eat")) {
                    System.out.println("来黑马学习");
                    return null;
                } else {
                    return method.invoke(stu, args);
                }
            }
        });
        proxyStu.eat("面条");
        proxyStu.study();

    }

c3p0


<!-- c3p0-config.xml-->
<c3p0-config>
  <!-- 使用默认的配置读取连接池对象 -->
  <default-config>
  	<!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/db14</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 连接池参数 -->
    <!--初始化的连接数量-->
    <property name="initialPoolSize">5</property>
    <!--最大连接数量-->
    <property name="maxPoolSize">10</property>
    <!--超时时间-->
    <property name="checkoutTimeout">3000</property>
  </default-config>
  
</c3p0-config>
//测试
public static void main(String[] args) throws SQLException {
        DataSource dataSource = new ComboPooledDataSource();
        Connection conn = dataSource.getConnection();
        System.out.println(conn.getClass());
        String sql = "SELECT * FROM student";
        PreparedStatement pst = conn.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        while (rs.next()){
            System.out.println(rs.getString("name"));
        }
        rs.close();
        pst.close();
        conn.close();
    }

Druid



//测试

public static void main(String[] args) {
        try {
            Connection conn = DataSourceUtils.getConnection();
            System.out.println(conn.getClass());
            String sql ="SELECT * FROM student";
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString("name"));
            }
            DataSourceUtils.close(conn,pst,rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
/*
    数据库连接池的工具类
 */
public class DataSourceUtils {
    //1.私有构造方法
    private DataSourceUtils(){}

    //2.声明数据源变量
    private static DataSource dataSource;

    //3.提供静态代码块,完成配置文件的加载和获取数据库连接池对象
    static{
        try{
            //完成配置文件的加载
            InputStream is = DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties");

            Properties prop = new Properties();
            prop.load(is);

            //获取数据库连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //4.提供一个获取数据库连接的方法
    public static Connection getConnection() {
        Connection con = null;
        try {
            con = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //5.提供一个获取数据库连接池对象的方法
    public static DataSource getDataSource() {
        return dataSource;
    }

    //6.释放资源
    public static void close(Connection con, Statement stat, ResultSet rs) {
        if(con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection con, Statement stat) {
        if(con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

自定义JDBC框架