JDBC 工厂封装返回一个Connection对象

Oracle 数据库的 JDBC工厂封装,信息全写在配置文件中

public class ConnectionFactory {
	private static String driver;
	private static String url;   //XE
	private static String username;
	private static String password;
	private static Properties properties;
	private static Connection connection;
	static {
		properties = new Properties();
		try {
			FileInputStream fs = new FileInputStream("src/jdbc.properties");
			properties.load(fs);
			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			username = properties.getProperty("user");
			password = properties.getProperty("password");
			//System.out.println(driver+url+username+password);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection (){
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection
					(url,properties);  //使用两参的时候命名必须规范 user password url driver
					//(url,username,password);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}

}

jdbc.properties 文件内容

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
user=oracle
password=oracle



Mysql  JDBC 工厂封装

public class ConnectionMySql {
	private static String driver;
	private static String url;   //XE
	private static String user;
	private static String password;
	private static Connection connection ;
	private static Properties properties;
	private static PreparedStatement pStatement;
	static {
		properties = new Properties();
		try {
			FileInputStream fis = new FileInputStream("src/mysql.properties");
			properties.load(fis);
			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("password");
			System.out.println("url,properties");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Connection getMySqlConnection(){
		try {
			Class.forName(driver);
			connection=DriverManager.getConnection(url,properties);
					//(url,user,password);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return connection;
	}
	
	
	/* public static void main(String[] args) {
		String sql = "select * from message";
		Connection mySqlConnection = getMySqlConnection();
		try {
			pStatement =mySqlConnection.prepareStatement(sql);
			ResultSet rs = pStatement.executeQuery();
			while(rs.next()){
				System.out.println("ID="+rs.getInt("ID")+
						" COMMAND="+rs.getString("COMMAND")+
						" DESCRIPTION"+rs.getString("DESCRIPTION")
						+" CONTENT="+rs.getString("CONTENT"));
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	/*	String url = "jdbc:mysql://127.0.0.1:3306/message?user=root&useUnicode=true&characterEncoding=UTF8";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection= DriverManager.getConnection(url);
			String sql = "select * from message";
			pStatement= connection.prepareStatement(sql);
			ResultSet rs = pStatement.executeQuery();
			while (rs.next()) {
				System.out.println("ID="+rs.getInt("ID")+
						" COMMAND="+rs.getString("COMMAND")+
						" DESCRIPTION"+rs.getString("DESCRIPTION")
						+" CONTENT="+rs.getString("CONTENT"));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}*/
	

}

mysql.properties 配置信息

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/message
user=root
password=





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值