好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

JDBC_05_ResorceBundle(资源绑定器) 绑定配置文件

ResorceBundle(资源绑定器) 绑定配置文件

jdbc.proprtise 需要在src目录下新建一个文件夹然后将jdbc.proprtise放在文件中然后右键该文件夹选择 Rebuild Config。

在引用该配置文件的时候不需要加后缀名。

代码

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;


public class JDBCTest05 {
    public static void main(String[] args) {

        //使用资源绑定器,绑定配置文件
        ResourceBundle resourceBundle=ResourceBundle.getBundle("config/jdbc"); // 不需要加后缀


        //通过key获取配置文件中的value
        String url=resourceBundle.getString("url");
        String username=resourceBundle.getString("username");
        String pwd=resourceBundle.getString("password");

        //创建jdbc连接对象
        Connection connection=null;
        Statement statement=null;

        try {

            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            connection= DriverManager.getConnection(url,username,pwd);

            //3.获取数据库操作对象
            statement=connection.createStatement();

            //4.执行sql语句
            statement.executeUpdate("insert into dept(deptno,dname,loc)values(80,‘Logistics‘,‘Peking‘)");

            //5.处理查询结果集
           //

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();

        }finally {

            //6.释放资源

                if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }
    }
} 

JDBC_05_ResorceBundle(资源绑定器) 绑定配置文件

标签:ring   uil   oid   ORC   rop   查询   驱动   use   source   

查看更多关于JDBC_05_ResorceBundle(资源绑定器) 绑定配置文件的详细内容...

  阅读:21次