JDBC 完整封装(二)

JDBC 完整封装(二)_第1张图片

JDBCUtil 工具类

  • 使用properties加载属性文件
  • 获取数据库连接
  • 释放资源(重载释放资源方法)

实现代码

  • main 测试类
  • JDBCUtil 工具类
  • jdbc.properties 配置类

/demo1/src/MainTest.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Driver;

public class MainTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Connection conn  = null;
        Statement stmt   = null;
        ResultSet rs     = null;
        try {

            conn = JDBCUtil.getConnection();
            // 3. 创建连接对象
            stmt = conn.createStatement();
            String sql = "select * from student";
            // 4. 执行sql,获得查询结果
            rs = stmt.executeQuery(sql);
            // 5. 循环遍历,处理每一条结果
            while (rs.next()) {
                int id = rs.getInt("studentNo");
                String name = rs.getString("name");
                System.out.println("id :" + id + ", name : " + name);
            }


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, stmt, rs);
        }

    }

}

/demo1/src/jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/myschool
user=root
password=103013

/demo1/src/JDBCUtil.java

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 JDBCUtil {
    private static String url;
    private static String user;
    private static String password;

    static {

        // 使用properties加载属性文件
        Properties prop = new Properties();
        try {
            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            prop.load(is);
            // 注册驱动(获取属性文件中的数据)
            String driverClassName = prop.getProperty("driverClassName");
            Class.forName(driverClassName);
            // 获取属性文件中的url,username,password
            url = prop.getProperty("url");
            user = prop.getProperty("user");
            password = prop.getProperty("password");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取数据库连接
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    // 释放资源
    public static void close(Connection conn, Statement stat, ResultSet rs) {
        close(conn, stat);
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 释放资源
    public static void close(Connection conn, Statement stat) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(java)