Properties的使用

来源:互联网 发布:淘宝模特神速拍照 编辑:程序博客网 时间:2024/05/22 21:44
package testDB;import java.io.InputStream;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.util.Properties;public class TestDB {public static void main(String[] args) throws Exception {Properties properties = new Properties();InputStream is = properties.getClass().getResourceAsStream("/database.properties");properties.load(is);is.close();String driver = properties.getProperty("driver");System.out.println(driver);String dburl = properties.getProperty("jdbcUrl");System.out.println(dburl);String user = properties.getProperty("user");System.out.println(user);String password = properties.getProperty("password");System.out.println(password);Driver drv = (Driver) Class.forName(driver).newInstance();    DriverManager.registerDriver(drv);                Connection con = DriverManager.getConnection(dburl,user, password);if(con!=null){System.out.println("connection ok");}con.close();}}

database.properties

driver=com.mysql.jdbc.DriverjdbcUrl=jdbc\:mysql\://localhost\:3306/springmvc?useUnicode\=true&characterEncoding\=utf8user=rootpassword=root


0 0
原创粉丝点击