Java数据库连接(JDBC)、JDBC连接池(c3p0)、MyBatis分别如何使用?如何连接数据库?

一、JDBC 的使用

第一步:导入 jar 包(mysql-connector-java.jar)

第二步:jdbc 就是一个连接数据库工具,分为几个步骤

  1. 加载驱动
  2. 创建连接
  3. 创建 statement
  4. 执行语句

第三步:编写代码实现

public class JdbcTest {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ClassNotFoundException, SQLException {

   Class.forName(</span>"com.mysql.jdbc.Driver"<span style="color: rgba(0, 0, 0, 1)">);

   String url </span>= "jdbc:mysql://localhost:3306/book"<span style="color: rgba(0, 0, 0, 1)">;

   String user </span>= "root"<span style="color: rgba(0, 0, 0, 1)">;

   String password </span>= "123456"<span style="color: rgba(0, 0, 0, 1)">;

   Connection connection </span>=<span style="color: rgba(0, 0, 0, 1)"> DriverManager.getConnection(url, user, password);

   Statement statement </span>=<span style="color: rgba(0, 0, 0, 1)"> connection.createStatement();

   String sql </span>= "select * from book_info where book_id=1"<span style="color: rgba(0, 0, 0, 1)">;

   ResultSet result </span>=<span style="color: rgba(0, 0, 0, 1)"> statement.executeQuery(sql);

   </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(result.next()) {

       System.out.println(result.getString(</span>1<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(2<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(3<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(4<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(5<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(6<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(7<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(8<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(9<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(10<span style="color: rgba(0, 0, 0, 1)">)

       </span>+"  "+result.getString(11<span style="color: rgba(0, 0, 0, 1)">)

       );

   }

   result.close();

   statement.close();

}</span></pre>

二、c3p0 的使用

第一步:导入 jar 包 mysql-connector-java.jar 和 c3p0.jar

第二步:参数配置

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

    <default-config>

       <property name="user">root</property>

       <property name="password">123456</property>

       <property name="jdbcUrl">jdbc:mysql://localhost:3306/book</property>

       <property name="driverClass">com.mysql.jdbc.Driver</property>

    </default-config>

</c3p0-config>

第三步:编写代码实现

package com.xc.c3p0;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0 {

   </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {

          DataSource source </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ComboPooledDataSource();

          Connection connection </span>=<span style="color: rgba(0, 0, 0, 1)"> source.getConnection();

          Statement statement </span>=<span style="color: rgba(0, 0, 0, 1)"> connection.createStatement();

          String sql </span>= "select * from book_info"<span style="color: rgba(0, 0, 0, 1)">;

          ResultSet result </span>=<span style="color: rgba(0, 0, 0, 1)"> statement.executeQuery(sql);

          </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(result.next()) {

                 System.out.println(result.getString(</span>1<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(2<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(3<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(4<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(5<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(6<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(7<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(8<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(9<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(10<span style="color: rgba(0, 0, 0, 1)">)

                               </span>+"  "+result.getString(11<span style="color: rgba(0, 0, 0, 1)">)

                               );

                        }

                        result.close();

                        statement.close();

   }

}

三、MyBatis 的使用

注:此处不在详细讲解操作步骤,而是主要整理技术演进,详细操作流程请参考其他博主的教程

第一步:项目中需要导入下图中三个 jar 包

第二步:编写配置文件以及测试代码

1、db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/book
username=root
password=123456

2、mybatis-config.xml

"<configuration>
 <properties resource=""db.properties""/>
 <environments default=""development"">
  <environment id=""development"">
   <transactionManager type=""JDBC""></transactionManager>
   <dataSource type=""POOLED"">
    <property name=""driver"" value=""${driver}""></property>
    <property name=""url"" value=""${url}""></property>
    <property name=""username"" value=""${username}""></property>
    <property name=""password"" value=""${password}""></property>
   </dataSource>
  </environment>
 </environments>
 <mappers>
  <mapper resource = ""com/xc/dao/impl/UserMapper.xml""></mapper>
 </mappers>
</configuration>
"

3、UserMapper.xml

"<mapper namespace=""com.xc.dao.impl.UserMapper"">
 <select id=""count"" resultType=""int"">
  select count(1) as count from users
 </select>
</mapper>
"

4、UserMapperTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class UserMapperTest {  
  public static void main(String[] args) throws SQLException, FileNotFoundException {
    String resource = "D:\\Eclipse_WorkSpace\\MavenProject\\WebMavenDemo01\\src\\main\\resources\\mybatis-config.xml";
    InputStream in = new FileInputStream(resource);
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
    int count = 0;
    SqlSession sqlSession = null;
    sqlSession = factory.openSession();
    //执行sql
    count = sqlSession.selectOne("com.xc.dao.impl.UserMapper.count");
    System.out.println(count);
    sqlSession.close();
  }
}

四、总结

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects, 普通的 Java 对象) 映射成数据库中的记录。