mysql jdbc 连接类

来源:互联网 发布:嫁给程序员好吗 编辑:程序博客网 时间:2024/06/07 02:02
package com.dxf.two_day;import java.sql.*;/** * Created by Administrator on 2017/12/10 0010. *mysql jdbc 连接类 */public class DBTool {    static{        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    static String url="jdbc:mysql://localhost:3306";    static String user="root";    static String password="root";    public static Connection getCon(){        try {            return DriverManager.getConnection(url,user,password);        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }    public void close(Connection con){        if (con!=null){            try {                con.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    public void close(PreparedStatement pstmt){        if (pstmt!=null){            try {                pstmt.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    public void close(Statement stmt){        if (stmt!=null){            try {                stmt.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    public void close(ResultSet rs){        if (rs!=null){            try {                rs.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }        public void rollback(Connection con){//事件回滚        try {            con.rollback();        } catch (SQLException e) {            e.printStackTrace();        }    } }

原创粉丝点击