转:在Red5中使用Spring-JDBC配置MySql连接池

对没有集成TOMCAT的RED5一般可使用Hibernate和Spring配置MySql数据库的连接池.这里是以Spring-JDBC来完成的.

一、配置需要以下的包:

spring-jdbc.jar

spring-dao.jar

commons-dbcp-1.2.1.jar

mysql-connector-java-5.0.5-bin.jar

将他们放到%RED5_HOME%\lib下即可

二、配置文件:

1、red5-web.properties

加入如下几行:根据你的数据库环境自行修改其值

db.driver=com.mysql.jdbc.Driver

db.url=jdbc:mysql://localhost:3306/test

db.username=root

db.password=root

2、red5-web.xml

<!--Databaseconnectionpoolbean-->

<beanid="myDataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

<propertyname="driverClassName"><value>${db.driver}</value></property>

<propertyname="url"><value>${db.url}</value></property>

<propertyname="username"><value>${db.username}</value></property>

<propertyname="password"><value>${db.password}</value></property>

<propertyname="poolPreparedStatements"><value>true</value></property>

<propertyname="maxActive"><value>10</value></property>

<propertyname="maxIdle"><value>10</value></property>

</bean>

3、Red5中调用:

1)示例方法,RowMapper方式:

publicStringgetSampleString()

{

//Gettingthedatasourcebean

Objecto=scope.getContext().getBean("myDataSource");

JdbcTemplatet=newJdbcTemplate((BasicDataSource)o);

//我的测试数据库为test,数据表为users;

finalListl=t.query("SELECT*FROMusers;",newRowMapper(){

publicObjectmapRow(ResultSetrs,introwNum)throwsSQLException{

returnnewMappedRow(rs.getInt(1),rs.getString(2));

}

});

//循环读取

finalIterator<MappedRow>i=l.iterator();

Strings="";

while(i.hasNext()){

s+=i.next().getName()+";";

}

//returntheresultforanfunction

logger.info("------------------------");

System.out.println("------------------------");

System.out.println("Springjdbcpoolworked.");

returns;

}

2)MappedRow:

publicclassMappedRow{

protectedintid;

protectedStringname;

publicMappedRow(int_id,String_name){

id=_id;

name=_name;

}

publicintgetId(){

returnid;

}

publicStringgetName(){

returnname;

}

}

以上。

相关推荐