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

JDBCUtils如何写

那个JDBCUtils工具类如何写,求大神指点

正在回答

3 回答

http://img1.sycdn.imooc.com//5aaf24dc000104dd07720096.jpg这咋整?

0 回复 有任何疑惑可以回复我~

谢谢你

0 回复 有任何疑惑可以回复我~

版本1:

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class JDBCUtil {

    private static DataSource dataSource = new ComboPooledDataSource();

    public static DataSource getDataSource() {

        return dataSource;

    }

    public static Connection getConnection() throws SQLException {

        return dataSource.getConnection();

    }

}

版本2:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class JDBCUtil1 {

    private static String url = "jdbc:mysql://localhost:3306/regist_web";

    private static String user = "root";

    private static String password = "test123";

    private JDBCUtil1() {}

static {

        try {

            Class.forName("com.mysql.jdbc.Driver");

        } catch (ClassNotFoundException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        }

}

    public static Connection getConnection() {

        try {

            return DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        return null;

        }

    }


    public static void free(ResultSet rs,Statement st,Connection conn) {

        if(rs != null) {

            try {

                rs.close();

            } catch (SQLException e) {

                // TODO Auto-generated catch block

            e.printStackTrace();

            } finally {

                if(st != null) {

                    try {

                        st.close();

                    } catch (SQLException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                    } finally {

                        if(conn != null) {

                                try {

                                    conn.close();

                                } catch (SQLException e) {

                                    // TODO Auto-generated catch block

                                    e.printStackTrace();

                                }

                        }

                }

        }

    }

}

}

}

视频中用的是版本1,我用版本1在daoImpl()里有点问题,所以我用的是版本2。

0 回复 有任何疑惑可以回复我~
#1

潇湘魅

非常感谢!!!!!!!!
2018-01-23 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

JDBCUtils如何写

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信