日期:2014-05-16 浏览次数:20476 次
package cn.felay.xyhdt.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; /** * 该类是数据库的工具类 * @author FelayMan * */ public class GetConn { private static String url = "jdbc:mysql://localhost:3306/android"; private static String user = "root"; private static String password = "felayman"; static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } /** * 获取数据库连接 * * @return * @throws SQLException */ @Test public static Connection getConn() throws SQLException { return DriverManager.getConnection(url, user, password); } /** * 释放资源 * * @param rs * @param st * @param conn */ public static void freeConn(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }