DataSource ds=null;

創(chuàng)新互聯(lián)專注于零陵網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供零陵營銷型網(wǎng)站建設(shè),零陵網(wǎng)站制作、零陵網(wǎng)頁設(shè)計、零陵網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造零陵網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供零陵網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
String username=null;
try{
//實現(xiàn)數(shù)據(jù)連接池
Context ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/userInfo");
}catch(NamingException ne){ne.printStackTrace();}
try{
con=ds.getConnection();
String sql="select * from guestbook order by gst_time desc";
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery(sql);
。。。。。。
。。。
}
在這段代碼中
Context ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/userInfo");就是連接池代碼。
它是讀取配置文件(Context.xml)中數(shù)據(jù)的。
以下是配置文件:
Context path="/WebModule1" docBase="E:\Home\WebModule1" reloadable="true"
Resource name="jdbc/userInfo" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="bye0406"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;databasename=userInfo" /
/Context
path和docBase中的路徑根據(jù)自己需要配置路徑。該路徑是WEB應(yīng)用程序的路徑。driverClassName和url是連接數(shù)據(jù)庫的驅(qū)動類和連接的URL,是根據(jù)你使用的哪種數(shù)據(jù)庫而定。
以下是四種數(shù)據(jù)庫的配制驅(qū)動:
驅(qū)動
SQL Server 2000
類名:com.microsoft.jdbc.sqlserver.SQLServerDriver
URL:jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs
Oracle
類名:oracle.jdbc.driver.OracleDriver
URL:jdbc:oracle:thin:@localhost:152:ORCL
Mysql
類名:com.mysql.jdbc.Driver
URL:jdbc:mysql://localhost:3306/databasename
JDBC-ODBC
類名:sun.jdbc.odbc.JdbcOdbcDriver
URL:jdbc:odbc:datasource_name;
方法一:繼承 Thread 類,覆蓋方法 run(),我們在創(chuàng)建的 Thread 類的子類中重寫 run() ,加入線程所要執(zhí)行的代碼即可。下面是一個例子:
public class MyThread extends Thread {
int count= 1, number;
public MyThread(int num) {
number = num;
System.out.println("創(chuàng)建線程 " + number);
}
public void run() {
while(true) {
System.out.println("線程 " + number + ":計數(shù) " + count);
if(++count== 6) return;
}
}
public static void main(String args[]) {
for(int i = 0; i 〈 5; i++) new MyThread(i+1).start();
}
}
用c3po來創(chuàng)建數(shù)據(jù)庫連接池,
1.到網(wǎng)絡(luò)上下載c3p0-0.9.0.4.jar包,導(dǎo)入工程;(注意以下方法只能用于web服務(wù)).
2.找出web服務(wù)器中conf目錄下的centent.xml,在里面加上以下配置
Resource auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="4"
minPoolSize="2"
acquireIncrement="1"
name="jdbc/TestDB"
user="root"
password="root"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/自己的數(shù)據(jù)庫名?autoReconnect=true" /
3.java獲取連接池的代碼;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBUtils {
static InitialContext ic;
static DataSource ds;
static{
try {
ic = new InitialContext();
ds = (DataSource)ic.lookup("java:comp/env/jdbc/TestDB");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getCon() {
try {
return ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
通過以上代碼,即可獲取連接池中的連接.
本文標題:java代碼實現(xiàn)連接池,java連接池工作原理
本文網(wǎng)址:http://chinadenli.net/article1/dsidsod.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、定制網(wǎng)站、ChatGPT、外貿(mào)建站、網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)