首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

一个简略封装的jdbc工具类

2012-11-26 
一个简单封装的jdbc工具类import java.sql.*import javax.sql.*public final class JdbcUtil {/** * @pa

一个简单封装的jdbc工具类

import java.sql.*;import javax.sql.*;public final class JdbcUtil {/** * @param args */private static String url = "jdbc:mysql://localhost:3306/jdbc"; // jdbc:mysql:///jdbc 本地默认端口可以省略private static String user = "root";private static String password = "123456";private JdbcUtil() { }static {try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {throw new ExceptionInInitializerError(e);}}public static Connection getConnection() throws SQLException{return DriverManager.getConnection(url, user, password);}public static void free(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();}}}}}}


static块用来注册驱动

getConnection函数用来获取连接

free函数用来安全的释放资源

热点排行