javaweb_day7(JDBC)的配置信息提取到配置文件

来源:互联网 发布:vc控制台 数据库 编辑:程序博客网 时间:2024/05/18 20:48

JDBC配置信息提取到配置文件


1.首先需要在工程下创建一个jdbc.properties文件

【jdbc.properties内容】

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///jdbc_demousername=rootpassword=root

这里写图片描述


2.读取文件里面的信息
通过:Properties properties =new Properties();对象里面的load方法
这里写图片描述

3.配置文件的工具类

package com.jdbc.utils;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;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 Jdbcutils {    private static final String driverClassName;//驱动    private static final String url;//连接数据库的地址    private static final String username;//用户名    private static final String password;//密码     static{            //创建对象             Properties properties =new Properties();            try {                //加载配置文件                properties.load(new FileInputStream("src/jdbc.properties"));            } catch (FileNotFoundException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            //读取到的配置信息在赋值            driverClassName=properties.getProperty("driverClassName");            url=properties.getProperty("url");            username=properties.getProperty("username");            password=properties.getProperty("password");    }    /**     * 注册驱动的方法     */    public static void loadDriver(){        try {            Class.forName(driverClassName);        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    /**     * 获得连接的方法     */    public static Connection getConnection(){        Connection conn = null;        try{            // 将驱动一并注册:            loadDriver();            // 获得连接            conn = DriverManager.getConnection(url,username, password);        }catch(Exception e){            e.printStackTrace();        }        return conn;    }    /**     * 释放资源的方法     */    public static void release(Statement stmt,Connection conn){        if(stmt != null){            try {                stmt.close();            } catch (SQLException e) {                e.printStackTrace();            }            stmt = null;        }        if(conn != null){            try {                conn.close();            } catch (SQLException e) {                e.printStackTrace();            }            conn = null;        }    }    public static void release(ResultSet rs,Statement stmt,Connection conn){        // 资源释放:        if(rs != null){            try {                rs.close();            } catch (SQLException e) {                e.printStackTrace();            }            rs = null;        }        if(stmt != null){            try {                stmt.close();            } catch (SQLException e) {                e.printStackTrace();            }            stmt = null;        }        if(conn != null){            try {                conn.close();            } catch (SQLException e) {                e.printStackTrace();            }            conn = null;        }    }}

【测试工具类】
案例:查询

package com.jdbc.demo2;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import org.junit.Test;import com.jdbc.utils.Jdbcutils;public class Demo2 {    // 查询    @Test    public void testSeach() throws Exception {        Connection connection = null;// 连接对象        Statement statement = null;// 执行sql语句对象        ResultSet resultSet = null;// 返回结果集对象        connection = Jdbcutils.getConnection();// 获取连接        statement = connection.createStatement();// 创建执行sql语句对象        String sql = "select * from t_user";//编写sql语句        resultSet = statement.executeQuery(sql);//执行sql语句        //遍历结果集        while (resultSet.next()) {System.out.println(resultSet.getInt("id") + "  " + resultSet.getString("name"));        }        //释放资源        Jdbcutils.release(resultSet, statement, connection);    }}

测试成功:
这里写图片描述

原创粉丝点击