美文网首页
1、JDBCTools

1、JDBCTools

作者: 在努力中 | 来源:发表于2018-06-25 16:38 被阅读0次
    package com.lsy.jdbc;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class JDBCTools {
        /**
         * 1、获取数据库连接
         */
        public static  Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
            //0、读取配置文件
            /*
             * 1、属性文件对应Java 中的 Properties类
             *      文件放在src下,实际上是在bin(类所在目录)中
             * 2、可以使用 类加载器 加载 bin 目录 (路径下的文件)
             */
            Properties properties = new Properties();
            InputStream inStream  = 
                    JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(inStream);
            
            //1、准备连接数据库的四个字符串:user password,jdbcUrl,driverClass
            // 从配置文件中得到
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("jdbcUrl");
            String driverClass = properties.getProperty("driver");
            
            
            //2、加载驱动:Class.forName(driverClass)
            Class.forName(driverClass);
            
            //3、调用:DriverManager.getConnection(jdbcUrl,user,password)
            //3.1、获取数据库的连接
            Connection connection =
                    DriverManager.getConnection(url,user,password);
            return connection;
        }
        /**
         * 2、释放数据库资源
         */
        public static void releaseDB(ResultSet resultSet,Statement statement,Connection connection){
            
            if(resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        /**
         * 3、更新方法(update add delete)没有 select
         * 执行 SQL 的方法
         */
        public static void update(String sql){
            System.out.println("update");
            //1.获取数据连接
            Connection connection = null;
            Statement statement = null;
            
            try {
                //2.调用Connection 对象的 【createStatement()】 方获取 Statement 对象
                connection = getConnection();
                statement = connection.createStatement();
                    
                //3.准备 SQL 语句
    //          sql = "update customer set name = 'cadddda' where id = 3";
                
                //4.发送 SQL 语句:调用 Statement 对象的 【executeUpdate(sql)】 方法
                statement.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //5.关闭数据库资源:由里向外关闭
                releaseDB(null, statement, connection);
            }
            System.out.println("update");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:1、JDBCTools

          本文链接:https://www.haomeiwen.com/subject/obbyyftx.html