jdbcUtil封装的工具类

来源:互联网 发布:vmware虚拟机备份软件 编辑:程序博客网 时间:2024/06/05 07:16
public class JdbcUtil{//提取参信息--方便以后进行封装private static String driver = "";private static String url = "";private static String username = "";private static String userpwd = "";//加载类的时候自动执行--静态代码块static{//通过流的方式来解析配置文件的内容//创建properties对象Properties properties = new Properties();try {//用properties对象读取配置文件,这里需要用当前类的class事例对象调用类加载器加载二进制流对象                        资源作为                         properties.load(JdbcUtil.class.getClassLoader().getResourceAsStream("Bjsxtjdbc.                        properties"));//获取配置参数信息,通过properties对象的getProperty()方法,这里参数中的jdbcname是对应配置文                       //件中的属性前缀                        String jdbcname = properties.getProperty("jdbcname");driver = properties.getProperty(jdbcname+"driver");url = properties.getProperty(jdbcname+"url");username = properties.getProperty(jdbcname+"username");userpwd = properties.getProperty(jdbcname+"userpwd");System.out.println("JdbcUtis.enclosing_method():"+"[driver]"+driver+"[url]"+url+"[username]"+username+"[userpwd]"+userpwd);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//创建连接public static Connection getConnection(){//声明连接Connection connection = null;try {//加载驱动Class.forName(driver);//创建连接connection = DriverManager.getConnection(url, username, userpwd);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {System.out.println("JdbcUtil.getConnection(请检查连接参数):"+"[url]:"+url+" [username]:"+username+" [userpwd]:"+userpwd);e.printStackTrace();}return connection;}//创建sql命令发生器preparedStatementpublic static PreparedStatement getPreparedStatement(Connection connection,String sql){//声明sql命令发送器对象PreparedStatement preparedStatement = null;try {preparedStatement = connection.prepareStatement(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return preparedStatement;}//创建statement命令发生器public static Statement getStatement(Connection connection){//声明sql命令发送器对象Statement statement = null;try {statement = connection.createStatement();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return statement;}//关闭资源连接 connection,statement,resultsetpublic static void closeAll(ResultSet resultSet,Statement statement,Connection connection){//后开的先关, 判断是否为空try {if(resultSet!=null){resultSet.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(statement!=null){statement.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(connection!=null){connection.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//封装DML语句public static int excuteDML(String sql,Object...objs){//声明一个变量int n = 0;//声明连接Connection connection = null;//声明sql命令发送器PreparedStatement preparedStatement = null;try {//创建连接connection = getConnection();preparedStatement = getPreparedStatement(connection, sql);//赋值for (int i = 0; i < objs.length; i++) {preparedStatement.setObject(i+1, objs[i]);}//发送sqln = preparedStatement.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//关闭连接closeAll(null,preparedStatement,connection);}return n;}

原创粉丝点击