2017年7月

package com.ssm.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Access {
    private Connection connection;

    private PreparedStatement ps;

    private ResultSet rs;

    public Connection getConn() {

        try {
            // 加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root123");

        } catch (ClassNotFoundException e) {

            e.printStackTrace();// 打印异常输出

        } catch (SQLException e) {

            e.printStackTrace();

        }
        return connection;
    }

    /**
     * 关闭所打开的连接
     * 
     * @param con
     * @param ps
     * @param rs
     */
    public void colseCon(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 测试是否连接成功
     * 
     * @param args
     */
    public static void main(String[] args) {
        Access cd = new Access();
        if(cd.getConn()!=null){
            System.out.println("链接成功");
        }else{
            System.out.println("链接shibai");
        }
        //System.out.println("链接成功"+cd.getConn().hashCode());

    }
}