在java中用数据库连接池连接数据库
好多大型的企业项目都要用到高级JDBC连接数据库配置高级JDBC实例如下:
第一:配置Tomcat下的conf文件夹下的context.xml文件
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/名称" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="5" maxwait="-1" username="sa" password="1234"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;Databasename=books" />
</Context>
各属性说明
<?xml version='版本' encoding='编码方式'?>
<Context>//上下文
name=数据源名称 auth-管理者=Container或Appliction type=数据源类型 maxActive=最大连接数 macIdle=最小连接数 maxwait=连接等待时间 username=数据库用户名 password=数据库连接密码 driverClassName=驱动名称 url=驱动连接类型databaseName=数据库名称
</Context>
第二:配置项目下的WebRoot目录下的web—inf目录下的web.xml文件
<resource-ref>
<description>说明文档</description>
<res-ref-name>数据源名称<res-ref-name/>
<res-type>数据源类型</res-type>
<res-auth>管理者类型</res-auth>
</resource-ref>
第三:配置项目下的BaseDao连接类
Context context=new initialContext(); //初始化
DataSource ds=(Datasource)context.lookup("java:comp/env/数据源名称"); //前缀+驱动名称
Connection conn=ds.getConnection(); //获得连接
相关文档:
Java的数据结构有哪些?Map与Set的本质区别是什么?
分析:Java常见的数据结构有Collection和Map,其中Collection接口下包括List和Set接口,其下又有多个实现类如List下有ArrayList、LinkedList和Vector等实现类,Set下有HashSet、LinkedSet等实现类和SortedSet接口,HashSet下有LinkedHashSet子类,SortedSet接口下有Tre ......
1.新建一个servlet程序,文件名为Test.java,文件内容如下:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServle ......
One:14109
Two:14000
Three:15141
four:14297
package com.zbalpha.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListTest {
public static void main(String args[]){
List<Long> ......
import java.util.prefs.*;
public class Registery {
String[] keys = {"oa"};
String[] values = {"reg"};
//把相应的值储存到变量中去
public void writeValue() { ......