tomcat配置連接資料庫
① 怎麼在tomcat配置資料庫連接池
方法/步驟
Tomcat大都是免安裝綠色版的,找到Tomcat的解壓路徑,然後找打一下文件:tomcat\conf\context.xml。
打開文件context.xml。
將右側滾動條拉到最下方,並添加如下圖紅色框內的代碼。
也可復制如下代碼:
<Resource driverClassName="oracle.jdbc.driver.OracleDriver"
maxActive="30" maxIdle="3" maxWait="5000" name="jdbc/test" username="test" password="test"
type="javax.sql.DataSource" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
那上邊的代碼有什麼作用呢?下邊來給大家解釋比較常用的幾個參數。
driverClassName:驅動的名稱。
該參數為資料庫驅動類名稱,比如上邊使用的是oracle的驅動。當然也意為著只能連接Oracle的資料庫。如果想用MySQL的,那就寫下邊這個嘍。
MySQL的驅動為:com.mysql.jdbc.Driver
name/password:為資料庫的用戶名和密碼。
② tomcat如何配置資料庫連接池,使得連接中斷後自動重連
給你一段代碼,看看對你有沒有幫助:
這是我的tomcatde DHCP的配置
<Resource driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" logAbandoned="true" maxActive="20" maxIdle="2" maxWait="5000" name="system" password="sa" removeAbandoned="true" removeAbandonedTimeout="60" type="javax.sql.DataSource"
url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=base" username="sa"/>
當中的
logAbandoned="true" removeAbandoned="true" removeAbandonedTimeout="60"就是用來配置資料庫斷開後自動連接的。
③ tomcat如何配置資料庫連接池,使得連接中斷後自動重連
給你一段代碼,看看對你有沒有幫助:x0dx0a這是我的tomcatde DHCP的配置x0dx0a
④ 如何在tomcat中配置數據源用oracle資料庫
一、Tomcat6.0中配置數據源
1.在Tomcat根目錄/conf/Catalina/localhost目錄下新建一個XML文件,文件名稱跟工程名稱一致.文件中的內容如下:
<?xmlversion='1.0'encoding='utf-8'?>
<ContextdocBase="E:Eclipse3.4.2workspacemmisWebContent"path="/mmis">
<Resourcename="mmisds"type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.2.250:1521:hmisb"
username="mtms"password="mtms"
validationQuery="select1fromal"
maxIdle="100"maxActive="500"maxWait="1000"defaultAutoCommit="true"
removeAbandoned="ture"removeAbandonedTimeout="60"logAbandoned="true"/>
</Context>
2.在tomcat的conf/context.xml中的<Context>標簽中添加一個<Resource/>,內容如下:
<Resourcename="jdbc/oa"auth="Container"type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
maxActive="100"maxIdle="500"maxWait="10000"
username="oa"password="oa"driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@172.16.60.30:1521:HMIS"defaultAutoCommit="true"
removeAbandoned="ture"removeAbandonedTimeout="60"logAbandoned="true"/>
然後在web.xml<web-app>中添加引用(tomcat5.5以上可以不用添加這一段)
<resource-ref>
<description>DBConnection</description>
<res-ref-name>jdbc/oa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2.1.獲取連接對象
publicclassConnectionPool{
publicstaticConnectiongetConn()throwsException{
//初始化上下文
ContextinitContext=getInitContext();
ContextenvContext=(Context)initContext.lookup("java:/comp/env");
DataSourcedataSource=(DataSource)envContext.lookup("jdbc/oa");
//獲取連接對象
returnds.getConnection();
}
}
docBase是指Web工程所在的位置,path是工程的名稱,name是指JNDI的名稱,type是數據源的類型,driverClassName是驅動名稱,url是驅動的連接字元串
username是指資料庫的用戶名,password是指資料庫的密碼,defaultAutoCommit是否自動提交
⑤ 如何在tomcat是中配置資料庫信息
1、把jtds-1.2.5.jar放到Tomcat目錄里的lib目錄下。如:D:IntelliJ omcat7lib
2、在D:IntelliJ omcat7conf目錄里的context.xml文件里加上如下代碼:
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resourcename="jdbc/sqlserver"auth="Container"type="javax.sql.DataSource"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433/yourDatabaseName"
username="username"password="password"maxActive="100"maxIdle="30"/>
</Context>
3、在你項目里的web.xml文件里加入如下內容:
<!--配置數據源-->
<resource-ref>
<description>SqlserverDatasource</description>
<res-ref-name>jdbc/sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、在你的代碼里引用數據源:
privateDataSourceds;
publicLoginBean(){
try{
//初始化查找命名空間
Contextctx=newInitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver");
}catch(NamingExceptione){
System.out.println("使用lookup找不到數據源.");
e.printStackTrace();
}
}
5、測試test.jsp:
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%@pageimport="java.sql.*"%>
<%@pageimport="javax.sql.*"%>
<%@pageimport="javax.naming.*"%>
<%!
finalStringJNDINAME="java:comp/env/jdbc/sqlserver";
%>
<%
Connectionconn=null;
try
{
//初始化查找命名空間
Contextctx=newInitialContext();
//找到DataSource
DataSourceds=(DataSource)ctx.lookup(JNDINAME);
conn=ds.getConnection();
Stringsql="selectpasswd,=?";
PreparedStatementpwdQuery=conn.prepareStatement(sql);
pwdQuery.setString(1,"admin");
ResultSetresult=pwdQuery.executeQuery();
if(!result.next()){
return;
}
System.out.println(result.getString("passwd"));
}
catch(Exceptione)
{
System.out.println(e);
}
%>
<%=conn%>
<%
//將連接重新放回到池中
conn.close();
%>