码迷,mamicode.com
首页 > 数据库 > 详细

JDBCUtils——原生

时间:2017-11-08 19:50:39      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:cut   time   tac   str   stack   get   runtime   turn   jdbcutil   

需要导入的包:

  • mysql-connector-java-5.1.37-bin.jar

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
    导包:
        mysql-connector-java-5.1.37-bin.jar
*/
public class JdbcUtils {
    private static String DRIVER = "com.mysql.jdbc.Driver";
    private static String URL = "jdbc:mysql://localhost:3306/newdatabase";
    private static String USER = "root";
    private static String PASSWORD = "1234";

    static {
        try {
            Class.forName(DRIVER);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获得连接
     * 
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
        return connection;
    }

    /**
     * 释放资源
     * 
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void closeResouce(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {

            try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

 

JDBCUtils——原生

标签:cut   time   tac   str   stack   get   runtime   turn   jdbcutil   

原文地址:http://www.cnblogs.com/xinmrwang/p/7805140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!