封装的DBHelper(JAVA)

JDBC+Mysql

public class DBHelper {
	private static String driver ="com.mysql.jdbc.Driver";
	private static String url ="jdbc:mysql://localhost:端口号(3306)/数据库名"; 
	private static String username ="账号";
	private static String password ="密码";	
	
	//静态, 连接
	public static Connection ToConnection(){
		Connection conn = null;
		try {
			Class.forName(driver);
			try {
				conn = DriverManager.getConnection(url, username, password);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return conn;
	}

	//关闭连接
	public static void CloseConnection(Connection conn){
		if(conn !=null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//关闭结果集
	public static void CloseResultSet(ResultSet rs){
		if(rs!=null)
		{
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			rs=null;
		}
	}
	//关闭statement
	public static void ClosePreparedStatement(PreparedStatement psmt){
		if(psmt!=null)
		{
			try {
				psmt.close();
				psmt=null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void CloseStatement(Statement stmt){
		if(stmt!=null)
		{
			try {
				stmt.close();
				stmt=null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。