为了账号安全,请及时绑定邮箱和手机立即绑定

Java连接mysql数据库的两种途径:JDBC和连接池

标签:
Java MySQL

永远怀着一颗卑微的心

  • 使用JDBC连接数据库
    BaseDao类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class BaseDao {
    Connection conn = null;
    public Connection getConnection() {    //连接方法
        try {
                Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/数据库名", "root", "数据库密码");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public void CloseAll(Connection conn, Statement st, ResultSet rs) {    //关闭方法
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

public int ExceuteUpdate(String sql,Object...prams){    //增删改建议套用方法
        int result = 0;
        conn = getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            if(prams != null){
                    for(int i =0;i<prams.length;i++){
                        pstmt.setObject(i+1, prams[i]);
                    }
            }
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            closeAll(conn,pstmt,rs);
        }
        return result;
}
}
  • 使用连接池连接数据库
    首先在一下apache中的conf文件夹中的context.xml文件添加下面这段配置信息
    <Resource
    name="jdbc/message"
    auth="Container"
    type="javax.sql.DataSource"
    maxActive = "100"
    maxIdle="30" maxWait="10000" username="root" password="123456"
    driverClassName = "com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/message"
    />

可将上面的配置信息直接在项目中创建一个context.xml文件将信息复制进去
当然也可在项目中的web.xml中写入配置信息(二者选其一)
<resource-ref>
<description>news DateSource</description>
<res-ref-name>jdbc/message</res-ref-name>
<res-type>javax.sql.DateSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

在BaseDao中的连接方法如下

public Connection getConnection(){
        Context ctx = null;
            try {
                ctx = new InitialContext();//初始化上下文
                DataSource ds= (DataSource)ctx.lookup("java:comp/env/jdbc/message");//date对象
                conn = ds.getConnection();
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return conn;
}

记得添加mysql的jar包,这点很重要!
生活就像代码,恰逢其会,猝不及防,学着去做一些有意义的事情,做自己想做的事。

点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
9
获赞与收藏
57

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消