JDBC工具类--JDBCUtil

public class JDBCUtil {

	//1.加载驱动
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//2.获取连接
	public static Connection getConnection(){
		
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xiaoy", "root", "mysql");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	//3.关闭连接
	public static void close(Connection conn,Statement st,ResultSet rs){
		//关闭连接
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//关闭statement 
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//关闭结果集
		if(rs != null){
			try {
				rs.clos

你可能感兴趣的:(java进阶,java)