java使用ini配置文件
来源:互联网 发布:网上电商系统java 编辑:程序博客网 时间:2024/05/16 23:51
使用配置文件是一种成熟的技术,能在修改程序代码,只修改ini配置文件情况下实现义务修改,效果同css与html分离一样。
一、基本知识
1、ini文件:存储属性键值对,以下三个格式作用一样:
key1=value1
key2:value2
key3 :value3
2、java使用ini文件常用方法
最简单的方法是使用java.util.Properties类读取或存储ini文件中键值对。
步骤:
1)、获得Properties对象,如:Properties reader=new Properties();
2)、使用Properties对象的load(InputStream input)或load(Reader reader)方法加载ini文件。两方法差别不大,仅在于前者采用了ISO-8859-1编码。
3)、通过Properties对象的getProperty(String key)获取对应键的值。
二、简单实践
使用ini配置文件代替在代码中使用具体信息的方法访问MySQL 数据库jdbctest。
jdbctest数据库:
ini配置文件:myconfig.ini
url=jdbc:mysql://127.0.0.1:3306/jdbctest
user=root
pass=1234
java代码:UseMySQL.java
import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Properties; public class UseMySQL { private static String dbURL=null; private static String user=null; private static String pass=null; public static void main(String[] args) { try{ <span style="white-space:pre"></span>//1、加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); //使用ini配置文件 FileInputStream reader=new FileInputStream("myconfig.ini"); Properties pro=new Properties(); pro.load(reader); dbURL=pro.getProperty("url"); user=pro.getProperty("user"); pass=pro.getProperty("pass"); } catch(Exception e) { System.out.print("加载驱动失败"); e.printStackTrace(); } try{ //2、获得连接对象 // Connection connect=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest","root","1234"); Connection connect=DriverManager.getConnection(dbURL,user,pass); System.out.println("连接成功"); //3、通过连接对象取得statement对象,用于操作数据库 Statement stmt=connect.createStatement(); //4、通过statement对象执行SQL语句 stmt.executeUpdate("insert into studentinfo(name,sex) values('韦','男');"); //5、statement返回结果集ResultSet对象,并解析 ResultSet rs=stmt.executeQuery("SELECT * FROM studentinfo order by id"); while (rs.next()){ System.out.println(rs.getString("id")+'\t'+rs.getString("name")+'\t'+rs.getString("sex"));} } catch(Exception e) { System.out.println("错误"); e.printStackTrace();} } }结果:
0 0
- java使用ini配置文件
- 使用java读取配置文件(ini、properties)
- Unity使用ini配置文件
- java设置ini配置文件
- 使用C#读写ini配置文件
- Windows系统使用INI配置文件
- 用java读写ini配置文件
- 用java读写ini配置文件
- Java读取解析INI配置文件
- [Java]Ini配置文件的读取
- TIniFile类--使用INI配置文件,C++
- 使用win api 读写 ini配置文件
- delphi 中配置文件的使用(*.ini)
- QT 使用QSettings 读写ini配置文件
- 使用API函数操作ini配置文件
- 使用IniEditor读写INI类型配置文件
- QT 使用QSettings读写ini配置文件
- 使用IniEditor读写INI类型配置文件
- hdu 5833 Zhu and 772002(高斯消元)
- 算数运算符
- 悲观锁解决高并发访问问题
- 2016.8.14GDOI组模拟总结
- 抽象工厂模式
- java使用ini配置文件
- volatile关键字
- 算术表达式的转换
- SVN各种图标含义
- 运算符操作数与结合性
- 记录重装系统+安装python IDE +各种python依赖过程
- 内部类
- 动态规划:正整数分组
- Express安装方法