Java开发中JDBC连接数据库

来源:互联网 发布:bilibili 知乎 编辑:程序博客网 时间:2024/06/08 15:45

基本方式

连接步骤

     * 1.加载JDBC驱动类     * 2.通过URL获取Connection连接     * 3.创建Statement     * 4.执行SQL语句     * 5.处理结果集     * 6.关闭连接

实现代码

public void base() {        try {            /*             * 加载JDBC驱动类             *              * MySql:"com.mysql.jdbc.Driver" SQL             * Server:"com.microsoft.sqlserver.jdbc.SQLServerDriver"             * Oracle:"oracle.jdbc.OracleDriver"             * PostgreSQL:"org.postgresql.Driver" SQLite:"org.sqlite.JDBC"             */            Class.forName("com.mysql.jdbc.Driver");            /*             * 通过连接URL获取Connection连接             *              * MySql:             * "jdbc:mysql://localhost:3306/database?user=<username>&password=<password>"             * SQL Server:             * "jdbc:sqlserver://localhost\\sqlexpress;user=<username>;password=<password>"             * Oracle:"jdbc:oracle:thin:@localhost:1521:mydb"             * PostgreSQL:"jdbc:postgresql:testdb"             * SQLite:"jdbc:sqlite:sample.mydb"             */            Connection con = DriverManager                    .getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=root");            // 执行静态SQL语句。通常通过Statement实例实现            Statement stmt = con.createStatement();            String sql = "select * from user";            // 执行SQL语句            stmt.executeUpdate(sql);            // 执行动态SQL语句。通常通过PreparedStatement实例实现            sql = "select * from where username=?";            PreparedStatement pstmt = con.prepareStatement(sql);            pstmt.setString(1, "zs");            // 执行SQL语句            ResultSet rs = pstmt.executeQuery();            while (rs.next()) {// 获取ResultSet中的数据                System.out.println(rs.getString(1) + rs.getString(2)                        + rs.getString(3));            }            //关闭连接            stmt.close();            stmt.close();            rs.close();            con.close();        } catch (ClassNotFoundException e) {            System.out.println("找不到驱动类,驱动类加载失败!");            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }    }

利用c3p0连接池连接数据库

所需jar包

mchange-commons-0.2.jar

c3p0-0.92-pre1.jar

mysql-connector-java-5.1.28-bin.jar

配置文件

文件位置:项目src目录下

文件名称:c3p0-config.xml

配置内容:

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

实现代码

public class JDBCUtil {    /**     * 使用默认配置文件     */    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();    /**     * 使用连接池返回一个连接     *      * @return返回Connection对象     * @throws SQLException     */    public static Connection getConnection() throws SQLException {        return dataSource.getConnection();    }    /*     * 返回池对象     */    public static DataSource getDataSource() {        return dataSource;    }}
原创粉丝点击