连接MySQL中的baseDao

package org.demo.jdbc;

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 com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class BaseDao {

    /**第一种连接,获得connecton的方法
    private static Connection getConnection() throws ClassNotFoundException,SQLException {
         Class.forName("com.mysql.jdbc.Driver");
         Connection
         connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore",
         "root", "root");
         return connection;
    }
    */
    
    
    /**以下是第二种连接数据库的方法,个人建议推荐使用方便点*/
    private static MysqlDataSource dataSource;
    static {
        dataSource = new MysqlDataSource();
        dataSource.setDatabaseName("bookstore");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setPort(3306);
    }

    private static Connection getConnection() throws ClassNotFoundException,SQLException {
        
        return dataSource.getConnection();
    }

    public static void executeUpdate(String sql, Object... params)
            throws ClassNotFoundException, SQLException {
        // 获得连接
        Connection connection = getConnection();
        // 获得预处理statement
        PreparedStatement statement = connection.prepareStatement(sql);

        if (params != null && params.length > 0) {

            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);

            }

        }
        //执行操作
        statement.executeUpdate();
        

    }
    //关闭连接,此方法主要针对增,删,改的关闭
    private static void closeResource(ResultSet resultSet,Statement statement,Connection connection){
        try {
            if(null!=resultSet){
                resultSet.close();
            }
            if(null!=statement){
                statement.close();
            }
            if(null!=connection){
                connection.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    //针对查询数据的方法
    public static ResultSet executeQuery(String sql, Object... params) throws ClassNotFoundException, SQLException{
    
        Connection connection=getConnection();
        
        PreparedStatement statement=connection.prepareStatement(sql);
        
        if(null!=params && params.length>0){
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i+1, params[i]);
            }
        }
        
        return statement.executeQuery();
        
    }
    
    //针对查询写的关闭
    public static void closeResource(ResultSet resultSet){
        try {
            Statement statement=resultSet.getStatement();
            Connection connection=statement.getConnection();
            if(null!=resultSet){
                resultSet.close();
            }
            if(null!=statement){
                statement.close();
            }
            if(null!=connection){
                connection.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值