cn.properties配置的用法

来源:互联网 发布:java爬虫 项目 编辑:程序博客网 时间:2024/06/10 16:14
1、cn.properties文件的写法
driver=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver://127.0.0.1:1433; databaseName=shoppingusername=sapassword=123

2、调用properties文件的写法
package com;import java.util.*;import java.io.*;import java.sql.*;public class DBUtil {Connection cn = null;Properties p = null;public Connection getConn(){p = new Properties();try {p.load(DBUtil.class.getClassLoader().getResourceAsStream("cn.properties"));String driver =p.getProperty("driver");String url = p.getProperty("url");String username = p.getProperty("username");String password = p.getProperty("password");Class.forName(driver);cn = DriverManager.getConnection(url,username,password);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return cn;}public void closeConn(){try {if(cn!=null)cn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
原创粉丝点击