JDBC连接Oracle,读取Properties

public class JDBC_Conn_DB {
	private static String path="src/com/briup/environment/file/jdbc.properties";
	//读取properties文件
	public static Properties getProperties() {
		
		Properties prop = new Properties();
		try {
			prop.load(new FileReader(new File(path)));
			/*String driver = prop.getProperty("driver");
			String url = prop.getProperty("url");
			String username = prop.getProperty("username");
			String password = prop.getProperty("password");
			System.out.println("driver:"+driver);
			System.out.println("url:"+url);
			System.out.println("username:"+username);
			System.out.println("password:"+password);*/
		}catch (IOException e) {
			e.printStackTrace();
		}
		return prop;		
	}
	//JDBC连接Oracle数据库测试
	public static void main(String[] args) {
		Properties prop = getProperties();
		try {
			//1.注册驱动
			Class.forName(prop.getProperty("driver"));
			//2.创建Connection对象
			String url = prop.getProperty("url");
			String username = prop.getProperty("username");
			String password = prop.getProperty("password");
			Connection conn=DriverManager.getConnection(url, username, password);
			//3.创建Statement对象
			String sql = "insert into e_detail_1(name,srcId,dstId)values(?,?,?)";
			PreparedStatement prep=conn.prepareStatement(sql);
			//4.执行SQL语句
			prep.setString(1, "李四"); 
			prep.setString(2, "222"); 
			prep.setString(3, "333");
			prep.execute();
			//5.处理结果集
			//6.关闭连接
			prep.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}

猜你喜欢

转载自blog.csdn.net/elice_/article/details/82497152