c2中oracle数据源连接并修改数据
第一步:修改tongweb.xml
<transaction-service name="UserTransaction" type="com.atomikos.icatch.jta.UserTransactionImp" transaction-factory="com.atomikos.icatch.jta.UserTransactionFactory"> <property name="transactionTimeout" value="300"/> </transaction-service> <jdbc-connection-pool link-name="jdbc/DataSource" name="jdbc/DataSource" is-xa="true" > <xa type="com.atomikos.jdbc.AtomikosDataSourceBean" listener="com.atomikos.tongweb.AtomikosLifecycleListener"> <property name="uniqueResourceName" value="jdbc/DataSource"/> <property name="xaDataSourceClassName" value="oracle.jdbc.xa.client.OracleXADataSource"/> <property name="xaProperties.user" value="liyin"/> <property name="xaProperties.password" value="liyin"/> <property name="maxPoolSize" value="200"/> <property name="xaProperties.URL" value="jdbc:oracle:thin:@10.10.87.13:1521:xe"/> <property name="factory" value="com.atomikos.tongweb.EnhancedTongWebAtomikosBeanFactory"/> </xa> </jdbc-connection-pool>
如下为完整实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | <?xml version= "1.0" encoding= "UTF-8" standalone= "yes" ?> <tongweb> <apps> <web-app delegate = "true" vs-names= "localhost" context-root= "/webdemo" app-root= "webdemo.war" caching-allowed= "true" cache-ttl= "5000" cache- object -max-size= "1" cache-max-size= "2000" deploy-order= "100" session-cookie-path= "/" > </web-app> </apps> <server shutdown-port= "8006" > <web-container> < virtual -host name= "localhost" accesslog-enabled= "false" app- base = "autodeploy" auto-deploy= "true" > <!-- <valve clazz= "com.tongweb.container.valves.RemoteIpValve" >--> <!-- <property name= "trustedProxies" value= "0:0:0:0:0:0:0:1|::1" />--> <!-- </valve>--> </ virtual -host> <!-- <http-listener name= "https" port= "8443" io-mode= "nio" ssl-enabled= "true" http2-enabled= "true" > <ssl keystore-file= "conf/ssl/tongweb.keystore" keystore-pass= "xxxxxx" keystore-type= "JKS" client-auth= "false" truststore-file= "conf/ssl/tongweb.keystore" truststore-pass= "xxxxxx" truststore-type= "JKS" /> </http-listener>--> <!-- <http-listener name= "https" port= "8444" io-mode= "nio" ssl-enabled= "true" > <ssl ssl-protocol= "GMSSLv1.1" keystore-type= "PKCS12" keystore-file= "conf/ssl/sm2.enc.pfx" keystore-pass= "xxxxxx" truststore-file= "conf/ssl/sm2.sig.pfx" truststore-pass= "xxxxxx" truststore-type= "PKCS12" /> </http-listener>--> <http-listener port= "8088" io-mode= "nio" > </http-listener> </web-container> <monitor-service monitoring-enabled= "false" > <monitor-config name= "Memory" monitoring-enabled= "false" /> <monitor-config name= "JVMMemoryPool" /> <monitor-config name= "GarbageCollector" /> <monitor-config name= "JVMThread" /> <monitor-config name= "Compilation" /> <monitor-config name= "ClassLoading" /> <monitor-config name= "Runtime" /> <monitor-config name= "OperatingSystem" /> <monitor-config name= "TWServer" /> <monitor-config name= "ConnectorAndThreadPool" /> <monitor-config name= "DataSource" /> <monitor-config name= "WebModule" /> <monitor-config name= "SessionManager" /> <monitor-config name= "Loader" /> <monitor-config name= "ResourceCache" /> <monitor-config name= "Request" /> </monitor-service> <transaction-service name= "UserTransaction" type= "com.atomikos.icatch.jta.UserTransactionImp" transaction-factory= "com.atomikos.icatch.jta.UserTransactionFactory" > <property name= "transactionTimeout" value= "300" /> </transaction-service>< <jdbc-connection-pool link-name= "jdbc/DataSource" name= "jdbc/DataSource" is -xa= "true" > <xa type= "com.atomikos.jdbc.AtomikosDataSourceBean" listener= "com.atomikos.tongweb.AtomikosLifecycleListener" > <property name= "uniqueResourceName" value= "jdbc/DataSource" /> <property name= "xaDataSourceClassName" value= "oracle.jdbc.xa.client.OracleXADataSource" /> <property name= "xaProperties.user" value= "liyin" /> <property name= "xaProperties.password" value= "liyin" /> <property name= "maxPoolSize" value= "200" /> <property name= "xaProperties.URL" value= "jdbc:oracle:thin:@10.10.87.13:1521:xe" /> <property name= "factory" value= "com.atomikos.tongweb.EnhancedTongWebAtomikosBeanFactory" /> </xa> </jdbc-connection-pool> <!-- <transaction-service name= "UserTransaction" type= "com.atomikos.icatch.jta.UserTransactionImp" transaction-factory= "com.atomikos.icatch.jta.UserTransactionFactory" > <property name= "transactionTimeout" value= "300" /> </transaction-service> <jdbc-connection-pool link-name= "jdbc/DS_MYSQL1" name= "jdbc/DS_MYSQL1" is -xa= "true" > <xa type= "com.atomikos.jdbc.AtomikosDataSourceBean" listener= "com.atomikos.tongweb.AtomikosLifecycleListener" > <property name= "uniqueResourceName" value= "jdbc/DS_MYSQL1" /> <property name= "xaDataSourceClassName" value= "com.mysql.cj.jdbc.MysqlXADataSource" /> <property name= "xaProperties.databaseName" value= "test1" /> <property name= "xaProperties.serverName" value= "localhost" /> <property name= "xaProperties.port" value= "3306" /> <property name= "xaProperties.user" value= "root" /> <property name= "xaProperties.password" value= "root" /> <property name= "maxPoolSize" value= "200" /> <property name= "xaProperties.url" value= "jdbc:mysql://localhost:3306/test1?characterEncoding=UTF8" /> <property name= "factory" value= "com.atomikos.tongweb.EnhancedTongWebAtomikosBeanFactory" /> </xa> </jdbc-connection-pool> <jdbc-connection-pool link-name= "jdbc/DS_MYSQL2" name= "jdbc/DS_MYSQL2" is -xa= "true" > <xa type= "com.atomikos.jdbc.AtomikosDataSourceBean" listener= "com.atomikos.tongweb.AtomikosLifecycleListener" > <property name= "uniqueResourceName" value= "jdbc/DS_MYSQL2" /> <property name= "xaDataSourceClassName" value= "com.mysql.cj.jdbc.MysqlXADataSource" /> <property name= "xaProperties.databaseName" value= "test2" /> <property name= "xaProperties.serverName" value= "localhost" /> <property name= "xaProperties.port" value= "3306" /> <property name= "xaProperties.user" value= "root" /> <property name= "xaProperties.password" value= "root" /> <property name= "maxPoolSize" value= "200" /> <property name= "xaProperties.url" value= "jdbc:mysql://localhost:3306/test2?characterEncoding=UTF8" /> <property name= "factory" value= "com.atomikos.tongweb.EnhancedTongWebAtomikosBeanFactory" /> </xa> </jdbc-connection-pool>--> <!-- <jdbc-connection-pool link-name= "jdbc/test_global" name= "jdbc/test" --> <!-- jdbc-driver= "com.mysql.cj.jdbc.Driver" --> <!-- jdbc-url= "jdbc:mysql://xxxx:3306/xxxx?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai" --> <!-- user-name= "xxxxx" password= "xxxxx" --> <!-- initial-size= "10" max-active= "100" min-idle= "10" max-wait-time= "30000" --> <!-- validation-query= "SELECT 1" validation-query-timeout= "5" test- on -borrow= "false" --> <!-- test- on -connect= "false" test- on - return = "false" test- while -idle= "false" --> <!-- time-between-eviction-runs= "60000" min-evictable-idle-time= "60000" remove-abandoned= "true" --> <!-- remove-abandoned-timeout= "2" />--> <!-- <security-service>--> <!-- <auth-realm name= "defaultRealm" type= "File" lock -enabled= "true" failure-count= "5" lock - out -time= "300" cache-size= "1000" >--> <!-- <property name= "UsersFile" value= "twusers.properties" />--> <!-- <property name= "GroupsFile" value= "twgroups.properties" />--> <!-- </auth-realm>--> <!-- <auth-realm name= "defaultRealm" type= "SQL" lock -enabled= "true" failure-count= "5" lock - out -time= "300" cache-size= "1000" >--> <!-- <property name= "jdbcURL" value= "jdbc:mysql://127.0.0.1:3306/test" />--> <!-- <property name= "jdbcDriver" value= "com.mysql.cj.jdbc.Driver" />--> <!-- <property name= "jdbcUser" value= "root" />--> <!-- <property name= "jdbcPassword" value= "123456" />--> <!-- <property name= "userSelect" value= "SELECT user_name, user_pass FROM users WHERE user_name = ?" />--> <!-- <property name= "groupSelect" value= "SELECT user_name, role_name FROM user_roles WHERE user_name = ?" />--> <!-- </auth-realm>--> <!-- </security-service>--> </server> </tongweb> |
第二部分 测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <%@ page import= "java.sql.*" %> <%@ page import= "javax.sql.DataSource" %> <%@ page import= "javax.naming.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>tomcat数据源</title> </head> <body> <% Connection conn = null ; DataSource ds = null ; Statement stmt = null ; Context context = null ; ResultSet rs = null ; try { if (context== null ){ context = new InitialContext(); } //检索对象 ds = (DataSource)context.lookup( "java:comp/env/jdbc/DataSource" ); //创建数据库连接 conn = ds.getConnection(); conn.setAutoCommit( false ); //创建statment对象用来将sql语句送入数据库 stmt = conn.createStatement(); Statement stmt2 = conn.createStatement(); stmt2.executeUpdate( "update TESTDATA set FOO=8888 where ID=1" ); conn.commit(); String sql = "SELECT * from TESTDATA" ; //执行sql语句 out .append( "<table>" ); //执行sql语句 rs = stmt.executeQuery(sql); //打印结果 while ( rs.next() ) { out .append( "<tr><td>" ).append(rs.getInt( "id" )+ "" ).append( "</td><td>" ) .append(rs.getString( "FOO" )).append( "</td><td>" ) .append(rs.getString( "BAR" )).append( "</td></tr>" ); } out .append( "</table>" ); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (stmt != null ) stmt.close(); if (rs != null ) rs.close(); if (conn != null ) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } %> </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix