java事物控制-数据库连接操作



java 事物控制


public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
		String driver="";
		String url="";
		String user="";
		String password="";
		
		String serialNo="";
		
		Connection conn1 = (Connection) getConnection2(driver,url,user,password);
		String content = getContent(serialNo,conn1);	
	}


 /**
	   * 获得数据库连接
	 * @throws SQLException 
	   * */

	private static java.sql.Connection getConnection2(String driver, String url, String user, String password) throws SQLException {
		java.sql.Connection conn = null ;
		 try {
			Class.forName(driver) ;
			 conn = DriverManager.getConnection(url, user, password) ;
			 
			 return conn;
			
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
			
		}
		return null;
	}


/**
	   * 获得正文内容
	 * @return 
	 * @throws IOException 
	 * @throws SQLException 
	   * */

	private static String getContent(String serialNo,Connection conn) throws IOException, SQLException {
		StringBuffer contentBuffer = new StringBuffer();
		String content = "";
		if(serialNo!=null){
			java.sql.Connection connection = conn;
			java.sql.PreparedStatement psStat = null;
			Reader reader = null;
			
			try {
				psStat = connection.prepareStatement("select content from notice_info where serialno=?");
				psStat.setString(1, serialNo);
				java.sql.ResultSet rs = psStat.executeQuery();
				if(rs.next()){
					Clob clob = rs.getClob("content");
					if(clob!=null){
						reader=clob.getCharacterStream();
						char buff[]=new char[1];
						for(int i=0;(i=reader.read(buff))>0;)
						{
							contentBuffer.append(buff);
						}
						reader.close();
					}
					
				}
				rs.close();
			}finally{
				if(psStat!=null) psStat.close();
				if(connection!=null) connection.close();
			}
			
			if(contentBuffer.length()>0)
			{
				content = StringFunction.replace(contentBuffer.toString(), "${CONTENT_VIEW_PATH}", ARE.getProperty("DFS_HTTP_PATH")+"/content/imageview");
			}	
		}
		
		return content;
		
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值