Java中JDBC连接数据库以及tomcat下配置jdbc连接池

作为Java连接数据库的基础,每个人都必须了解数据库连接过程。

1、JDBC连接数据

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcOpDb {
    public static void main(String[] args) {
        
        try {
            //1.加载驱动,借助java的反射机制
            Class.forName("com.mysql.jdbc.Driver");
            //2、构造jdbc连接串
            String url ="jdbc:mysql://127.0.0.1:3306/javadb";
            String username ="root";
            String password ="root";
            //3.获取连接
            Connection conn = DriverManager.getConnection(url,username,password);
            //4.定义sql
            String sql ="select 1 from users";
            //5.获取执行sql的对象
            Statement stmt = conn.createStatement();
            //6.执行sql查询,获取结果集
            ResultSet rs = stmt.executeQuery(sql);
            //7.处理返回结果
            while(rs.next()) {
                String name = rs.getString("name");
                System.out.println(name);
            }
            //8.释放资源,遵守先开后关,后开先关原则
            rs.close();
            conn.close();
            stmt.close();
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }


    }
}

2、tomcat下配置数据库连接池

项目中最简单的方式,在项目的WEB-INF下创建context.xml,也可以直接在tomcat安装目录下编写context文件

<Context>

    <!-- maxTotal: Maximum number of database connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle database connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP 2 documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWaitMillis: Maximum time to wait for a database connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL username and password for database connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->

    <!-- url: The JDBC connection url for connecting to your MySQL database.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javadb"/>

</Context>

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值