Connection连接和PreparedStatement 操作数据库

链接数据库操作的代码

连接数据库的几种方式

	//数据库连接对象
		private Connection conn;
	
		//预处理
		private PreparedStatement pstmt;
	
		/*
		 * 构造方法
		 */
		public Database() {
			this.conn = null;
			this.pstmt = null;
		}
通过访url建立连接
		/*
		 * 通过访问url建立连接 
		 *
		 * @return  java.sql.Connection
		 * @throws java.lang.Exception
		 */
		public Connection getConnectionByUrl() throws Exception {
			Connection conn = null; 
			try {
				/*
				*	SERVICE_NAME 数据库名
				*   root  用户名
				*   root123  密码
				*/
				conn = DriverManager.getConnection(
						"jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS ="
								+ "(PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))(ADDRESS ="
								+ "(PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA"
								+ "=(SERVICE_NAME = test)))", "root", "root123");
				
			} catch (Exception e) {
				System.out.println("数据库链接异常");	
				throw e;					
			}
		
			
			if(conn==null){
				System.out.println("数据库链接异常");	
			}	
			return conn;
		}
通过访问数据源建立连接
	/*
		 * 通过访问数据源建立连接 
		 *
		 * @return  java.sql.Connection
		 * @throws java.lang.Exception
		 */
		public Connection getConnection() throws Exception {
			Connection conn = null; 
			try {
				//proxool.xml中的Develop2数据库配置
				conn = DriverManager.getConnection("proxool.Develop2");
			} catch (Exception e) {
				System.out.println("数据库链接异常");	
				throw e;					
			}
		
			if(conn==null){
				System.out.println("数据库链接异常");	
				getConnection();
			}	
			return conn;
		}
通过JDBC直接建立连接
		
		/*
		* 通过JDBC直接建立连接 
		*
		* @return  java.sql.Connection
		* @throws java.lang.Exception
		*/
		@SuppressWarnings("unused")
		private Connection getJDBCConnection() throws Exception {
			BaseBean baseBean = new BaseBean();
			String dbDriver = baseBean.getJDBCDriver();
			String dbUrl = baseBean.getDatabaseURL();
			String userName = baseBean.getUserName();
			String userPassword = baseBean.getUserPassword();
			Class.forName(dbDriver);
			return DriverManager.getConnection(dbUrl, userName, userPassword);
		}
开启数据库连接
		/*
		 * 开启数据库连接
		 *
		 * @throws  java.lang.Exception   开启时发生异常,数据库连接失败
		 */
		public boolean connect() throws Exception {
			this.conn = this.getConnection();
			//this.conn = this.getJDBCConnection();
			return true;
		}
	
		

操作数据库

查询
	/*
		 * 查询
		 *
		 * @param   sql   String          sql语句
		 *
		 * @return  java.sql.ResultSet    查询结果
		 * @throws  java.lang.Exception   发生异常
		 */
		public ResultSet executeQuery(String sql) throws Exception {
			//查询处理
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.executeQuery();
			//返回查询结果
			return this.pstmt.getResultSet();
		}
条件查询(支持多个条件)
		/*
		 * 条件查询(支持多个条件)
		 *
		 * @param   sql      String          sql语句
		 * @param   params   List<String>          查询条件
		 *
		 * @return  java.sql.ResultSet    查询结果
		 * @throws  java.lang.Exception   发生异常
		 */
		public ResultSet executeQuery(String sql, List<String> params)
			throws Exception {
			this.pstmt = this.conn.prepareStatement(sql);
			for (int i = 0; i < params.size(); i++) {
				//根据参数个数添加参数,防止sql注入
				this.pstmt.setString(i+1, params.get(i));
			}
			return this.pstmt.executeQuery();
		}
条件查询(1个条件)
		/*
		 * 条件查询(1个条件)
		 *
		 * @param   sql      String          sql语句
		 * @param   param1   String          查询条件
		 *
		 * @return  java.sql.ResultSet    查询结果
		 * @throws  java.lang.Exception   发生异常
		 */
		public ResultSet executeQuery(String sql, String param1)
			throws Exception {
			//查询处理
			this.pstmt = this.conn.prepareStatement(sql);
	
			this.pstmt.setString(1, param1);
	
			this.pstmt.executeQuery();
	
			//返回查询结果
			return this.pstmt.getResultSet();
		}
条件查询(两个条件)
		/*
		 * 条件查询(两个条件)
		 *
		 * @param   sql      String          sql语句
		 * @param   param1   String          查询条件
		 * @param   param2   String          查询条件
		 *
		 * @return  java.sql.ResultSet    查询结果
		 * @throws  java.lang.Exception   发生异常
		 */
		public ResultSet executeQuery(String sql, String param1, String param2)
			throws Exception {
			//查询处理
			this.pstmt = this.conn.prepareStatement(sql);
	
			this.pstmt.setString(1, param1);
			this.pstmt.setString(2, param2);
	
			this.pstmt.executeQuery();
	
			//返回查询结果
			return this.pstmt.getResultSet();
		}
执行sql语句
		/*
		 * 插入记录
		 *
		 * @param   sql   String          sql语句
		 *
		 * @throws  java.lang.Exception   发生异常,未能插入记录
		 */
		public void executeInsert(String sql) throws Exception {
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.executeUpdate();
		}
执行sql 注入参数
		/*
		 * 带参数插入记录
		 *
		 * @param   sql   String          sql语句
		 *
		 * @throws  java.lang.Exception   发生异常,未能插入记录
		 */
		public void executeInsert(String sql, int param1, int param2) throws Exception {
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setInt(1, param1);
			this.pstmt.setInt(2, param2);
			this.pstmt.executeUpdate();
		}
执行sql 多个参数
		/*
		 * 带参数插入记录
		 *
		 * @param   sql   String          sql语句
		 *
		 * @throws  java.lang.Exception   发生异常,未能插入记录
		 */
		public void executeInsert(String sql, String... param) throws Exception {
			this.pstmt = this.conn.prepareStatement(sql);
			for (int i = 0; i < param.length; i++) {
				this.pstmt.setString(i+1, param[i]);
			}
			this.pstmt.executeUpdate();
		}
事务处理开始
		/*
		 * 事务处理开始
		 *
		 * @throws  java.lang.Exception   发生异常,未能关闭自动提交
		 */
		public void beginTransaction() throws Exception {
			this.conn.setAutoCommit(false);
		}
事务处理结束
		/*
		 * 事务处理结束
		 *
		 * @throws  java.lang.Exception   发生异常,未能打开自动提交
		 */
		public void endTransaction() throws Exception {
			this.conn.setAutoCommit(true);
		}
提交处理
		/*
		 * 提交处理
		 *
		 * @throws  java.lang.Exception   发生异常,提交未成功
		 */
		public void commit() throws SQLException {
			this.conn.commit();
		}
回滚(还原数据记录)
		/*
		 * 回滚(还原数据记录)
		 *
		 * @throws  java.lang.Exception   发生异常,还原数据记录未成功
		 */
		public void rollback() throws SQLException {
			this.conn.rollback();
		}
关闭数据库连接
		/*
		 * 关闭数据库连接
		 *
		 * @throws  java.lang.Exception   发生异常,关闭数据库连接未成功
		 */
		public void close() throws Exception {
			if (this.pstmt != null)
				this.pstmt.close();
			if (this.conn != null)
				this.conn.close();
		}
关闭PrepareStatement
		/*
	 * 关闭PrepareStatement
	 *
	 * @throws  java.lang.Exception   发生异常,关闭PrepareStatement未成功
	 */
	public void closePstmt() throws Exception {
		if (this.pstmt != null)
			this.pstmt.close();
	}

proxool.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<proxool-config>


<proxool>
	<alias>Develop2</alias>
<driver-url>jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = dxyytdb)))</driver-url>
	<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
	<driver-properties>
		<property name="user" value="root" />
		<property name="password" value="root123" />
	</driver-properties>
	<maximum-connection-count>50</maximum-connection-count>
  <prototype-count>2</prototype-count>
  <house-keeping-sleep-time>20000</house-keeping-sleep-time>
	<house-keeping-test-sql>select SYSDATE FROM DUAL</house-keeping-test-sql>
	<statistics>1m,1h,1d</statistics>
	<statistics-log-level>ERROR</statistics-log-level>
</proxool>
</proxool-config>


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值