首页 > 代码库 > 封装的DBHelper(JAVA)

封装的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();			}		}	}}

 

封装的DBHelper(JAVA)