ssh2同时连接2个数据库

首先在mysql数据库中建立数据库test,表functions;在sqlserver2000中建立数据库test,表sfunctions
连个表字段都一样为id,pid,url,functionname.
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 <!-- 定义数据源Bean,使用C3P0数据源实现 -->
 <bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <!-- 指定连接数据库的驱动 -->
  <property name="driverClass" value="com.mysql.jdbc.Driver" />
  <!-- 指定连接数据库的URL -->
  <property name="jdbcUrl" value="jdbc:mysql://localhost/lams" />
  <!-- 指定连接数据库的用户名 -->
  <property name="user" value="root" />
  <!-- 指定连接数据库的密码 -->
  <property name="password" value="root" />
  <!-- 指定连接数据库连接池的最大连接数 -->
  <property name="maxPoolSize" value="20" />
  <!-- 指定连接数据库连接池的最小连接数 -->
  <property name="minPoolSize" value="1" />
  <!-- 指定连接数据库连接池的初始化连接数 -->
  <property name="initialPoolSize" value="1" />
  <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
  <property name="maxIdleTime" value="20" />
 </bean>
 <!-- 定义Hibernate的SessionFactory -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <!-- 依赖注入上面定义的数据源dataSource -->
  <property name="dataSource" ref="dataSource" />
  <!-- 注册Hibernate的ORM映射文件 -->
  <property name="mappingResources">
   <list>
    <value>com/eportal/ORM/Functions.hbm.xml</value>
   </list>
  </property>
  <!-- 设置Hibernate的相关属性 -->
  <property name="hibernateProperties">
   <props>
    <!-- 设置Hibernate的数据库方言 -->
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
    <prop key="hibernate.show_sql">true</prop>
    <!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
    <prop key="hibernate.jdbc.batch_size">50</prop>
   </props>
  </property>
 </bean>
 <!--定义Hibernate的事务管理器HibernateTransactionManager -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <!-- 依赖注入上面定义的sessionFactory -->
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <!--定义Spring的事务拦截器TransactionInterceptor -->
 <bean id="transactionInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <!--  依赖注入上面定义的事务管理器transactionManager -->
  <property name="transactionManager" ref="transactionManager" />
  <!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
  <property name="transactionAttributes">
   <props>
    <!-- 以browse、list、load、get及is开头的所有方法采用只读型事务控制类型 -->
    <prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
    <!-- 所有方法均进行事务控制,如果当前没有事务,则新建一个事务 -->
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理-->
 <bean
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <!--  针对指定的bean自动生成业务代理 -->
  <property name="beanNames">
   <list>
    <value>functionService</value>
   </list>
  </property>
  <!--  这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
  <property name="proxyTargetClass">
   <value>true</value>
  </property>
  <!--  依赖注入上面定义的事务拦截器transactionInterceptor -->
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>

 <!-- 定义连接 GPS 服务器数据库  -->
 <bean id="dataSourceGPS"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass"
   value="net.sourceforge.jtds.jdbc.Driver" />
  <property name="jdbcUrl"
   value="jdbc:jtds:sqlserver://127.0.0.1:1433:DatabaseName=test" />
  <property name="user" value="sa" />
  <property name="password" value="123" />
  <property name="maxPoolSize" value="20" />
  <property name="minPoolSize" value="1" />
  <property name="initialPoolSize" value="1" />
  <property name="maxIdleTime" value="20" />
 </bean>
 <!--定义了Hibernate的SessionFactoryGPS -->
 <bean id="sessionFactoryGPS"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSourceGPS" />
  <property name="mappingResources">
   <list>
    <value>com/eportal/ORM/Sfunctions.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
   </props>
  </property>
 </bean>
 <bean id="transactionManagerGPS"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactoryGPS" />
 </bean>
 <bean id="transactionInterceptorGPS"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <!--  事务拦截器bean需要依赖注入一个事务管理器 -->
  <property name="transactionManager" ref="transactionManagerGPS" />
  <property name="transactionAttributes">
   <!--  下面定义事务传播属性-->
   <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>


 <bean id="functionDao" class="com.eportal.DAO.FunctionDaoImpl">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <bean id="functionService"
  class="com.eportal.service.FunctionServiceImpl">
  <property name="functionDao" ref="functionDao" />
 </bean>

 <bean id="sfunctionDao" class="com.eportal.DAO.sFunctionDaoImpl">
  <property name="sessionFactory" ref="sessionFactoryGPS" />
 </bean>
 <bean id="sfunctionService"
  class="com.eportal.service.sFunctionServiceImpl">
  <property name="sfunctionDao" ref="sfunctionDao" />
 </bean>

 <bean id="userAction" class="com.eportal.struts.action.UserAction"
  scope="prototype">
  <property name="functionService" ref="functionService" />
  <property name="sfunctionService" ref="sfunctionService" />
 </bean>

</beans>


UserAction.java中的方法如下:
 public String getFunctionList(){
  List list1 = functionService.getFunctionList();
  System.out.println("AAAAAAAAAAAAA"+list1.size());
  Functions function = (Functions)list1.get(0);
  
  Sfunctions sfunctions = new Sfunctions();
  sfunctions.setPid(function.getPid());
  sfunctions.setUrl(function.getUrl());
  sfunctions.setFunctionname(function.getFunctionname());
  sfunctionService.insert(sfunctions);
  
  List list2 = sfunctionService.getFunctionList();
  System.out.println("BBBBBBBBBBBBB"+list2.size());
  
  return SUCCESS;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SSH隧道连接数据库是一种安全的方式,可以通过加密和身份验证来保护数据库连接。下面是使用SSH隧道连接数据库的步骤: 1. 配置SSH服务器:首先,你需要有一个可用的SSH服务器,可以是远程服务器或者本地机器上运行的SSH服务器。 2. 生成SSH密钥对:在客户端上生成SSH密钥对,包括公钥和私钥。你可以使用ssh-keygen命令生成密钥对。 3. 将公钥添加到SSH服务器:将生成的公钥添加到SSH服务器的授权文件中,通常是将公钥内容添加到~/.ssh/authorized_keys文件中。 4. 配置SSH隧道:在客户端上配置SSH隧道,使用ssh命令来建立隧道连接。例如,如果你要连接到MySQL数据库,可以使用以下命令: ``` ssh -L <本地端口>:<目标数据库地址>:<目标数据库端口> <SSH服务器地址> ``` 其中,`<本地端口>`是你本地机器上用于连接数据库的端口号,`<目标数据库地址>`是目标数据库的地址,`<目标数据库端口>`是目标数据库的端口号,`<SSH服务器地址>`是SSH服务器的地址。 5. 连接数据库:现在你可以使用本地机器上的数据库客户端工具来连接数据库了。在连接设置中,将数据库地址设置为`localhost`,端口号设置为步骤4中指定的本地端口号。 注意:在使用SSH隧道连接数据库时,确保SSH服务器和数据库服务器都是可信任的,并且已经进行了适当的安全配置

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值