「数字连通java」数字连接成语
本篇文章给大家谈谈数字连通java,以及数字连接成语对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、使用Java 测试网络连通性的几种方法
- 2、JAVA如何生成一个随机的有向连通图
- 3、java如何判断一个二维数组两个位置是否连通
- 4、Java实现IP是否能Ping通功能
- 5、JAVA用1、2、2、3、4、5排列组合,最多能排列多少组合并打印出来。要求:4不能放在第三位,4和5不能相连
- 6、Java开发常用的几个数据库连接池
使用Java 测试网络连通性的几种方法
概述在网络编程中,有时我们需要判断两台机器之间的连通性,或者说是一台机器到另一台机器的网络可达性。在系统层面的测试中,我们常常用 Ping 命令来做验证。尽管 Java 提供了比较丰富的网络编程类库(包括在应用层的基于 URL 的网络资源读取,基于 TCP/IP 层的 Socket 编程,以及一些辅助的类库),但是没有直接提供类似 Ping 命令来测试网络连通性的方法。本文将介绍如何通过 Java 已有的 API,编程实现各种场景下两台机器之间的网络可达性判断。在下面的章节中,我们会使用 Java 网络编程的一些类库 java.net.InetAddress 和 java.net.Socket,通过例子解释如何模拟 Ping 命令。回页首简单判断两台机器的可达性一般情况下,我们仅仅需要判断从一台机器是否可以访问(Ping)到另一台机器,此时,可以简单的使用 Java 类库中 java.net.InetAddress 类来实现,这个类提供了两个方法探测远程机器是否可达 �0�2boolean isReachable(int�0�2timeout) //�0�2测试地址是否可达�0�2boolean isReachable(NetworkInterface�0�2netif, int�0�2ttl, int�0�2timeout) //�0�2测试地址是否可达. 简单说来,上述方法就是通过远端机器的 IP 地址构造 InetAddress 对象,然后调用其 isReachable 方法,测试调用机器和远端机器的网络可达性。注意到远端机器可能有多个 IP 地址,因而可能要迭代的测试所有的情况。清单1:简单判断两台机器的可达性 void isAddressAvailable(String ip){ try{ InetAddress address = InetAddress.getByName(ip);//ping this IP if(address instanceof java.net.Inet4Address){ System.out.println(ip + " is ipv4 address"); }else if(address instanceof java.net.Inet6Address){ System.out.println(ip + " is ipv6 address"); }else{ System.out.println(ip + " is unrecongized"); } if(address.isReachable(5000)){ System.out.println("SUCCESS - ping " + IP + " with no interface specified"); }else{ System.out.println("FAILURE - ping " + IP + " with no interface specified"); } System.out.println("
-------Trying different interfaces--------
"); EnumerationNetworkInterface netInterfaces = NetworkInterface.getNetworkInterfaces(); while(netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); System.out.println( "Checking interface, DisplayName:" + ni.getDisplayName() + ", Name:" + ni.getName()); if(address.isReachable(ni, 0, 5000)){ System.out.println("SUCCESS - ping " + ip); }else{ System.out.println("FAILURE - ping " + ip); } EnumerationInetAddress ips = ni.getInetAddresses(); while(ips.hasMoreElements()) { System.out.println("IP: " + ips.nextElement().getHostAddress()); } System.out.println("-------------------------------------------"); } }catch(Exception e){ System.out.println("error occurs."); e.printStackTrace(); } } 程序输出 --------------START-------------- 10.13.20.70 is ipv4 address SUCCESS - ping 10.13.20.70 with no interface specified -------Trying different interfaces-------- Checking interface, DisplayName:MS TCP Loopback interface, Name:lo FAILURE - ping 10.13.20.70 IP: 127.0.0.1 ------------------------------------------- Checking interface, DisplayName:Intel(R) Centrino(R) Advanced-N 6200 AGN - Teefer2 Miniport, Name:eth0 FAILURE - ping 10.13.20.70 IP: 9.123.231.40 ------------------------------------------- Checking interface, DisplayName:Intel(R) 82577LM Gigabit Network Connection - Teefer2 Miniport, Name:eth1 SUCCESS - ping 10.13.20.70 ------------------------------------------- Checking interface, DisplayName:WAN (PPP/SLIP) Interface, Name:ppp0 SUCCESS - ping 10.13.20.70 IP: 10.0.50.189 ------------------------------------------- --------------END-------------- 从上可以看出 isReachable 的用法,可以不指定任何接口来判断远端网络的可达性,但这不能区分出数据包是从那个网络接口发出去的 ( 如果本地有多个网络接口的话 );而高级版本的 isReachable 则可以指定从本地的哪个网络接口测试,这样可以准确的知道远端网络可以连通本地的哪个网络接口。但是,Java 本身没有提供任何方法来判断本地的哪个 IP 地址可以连通远端网络,Java 网络编程接口也没有提供方法来访问 ICMP 协议数据包,因而通过 ICMP 的网络不可达数据包实现这一点也是不可能的 ( 当然可以用 JNI 来实现,但就和系统平台相关了 ), 此时可以考虑本文下一节提出的方法。回页首指定本地和远程网络地址,判断两台机器之间的可达性在某些情况下,我们可能要确定本地的哪个网络地址可以连通远程网络,以便远程网络可以回连到本地使用某些服务或发出某些通知。一个典型的应用场景是,本地启动了文件传输服务 ( 如 FTP),需要将本地的某个 IP 地址发送到远端机器,以便远端机器可以通过该地址下载文件;或者远端机器提供某些服务,在某些事件发生时通知注册了获取这些事件的机器 ( 常见于系统管理领域 ),因而在注册时需要提供本地的某个可达 ( 从远端 ) 地址。虽然我们可以用 InetAddress.isReachabl 方法判断出本地的哪个网络接口可连通远程玩过,但是由于单个网络接口是可以配置多个 IP 地址的,因而在此并不合适。我们可以使用 Socket 建立可能的 TCP 连接,进而判断某个本地 IP 地址是否可达远程网络。我们使用 java.net.Socket 类中的 connect 方法 void connect(SocketAddress�0�2endpoint, int�0�2timeout) �0�2//使用Socket连接服务器,指定超时的时间 这种方法需要远程的某个端口,该端口可以是任何基于 TCP 协议的开放服务的端口(如一般都会开放的 ECHO 服务端口 7, Linux 的 SSH 服务端口 22 等)。实际上,建立的 TCP 连接被协议栈放置在连接队列,进而分发到真正处理数据的各个应用服务,由于 UDP 没有连接的过程,因而基于 UDP 的服务(如 SNMP)无法在此方法中应用。具体过程是,枚举本地的每个网络地址,建立本地 Socket,在某个端口上尝试连接远程地址,如果可以连接上,则说明该本地地址可达远程网络。程序清单 2:指定本地地址和远程地址,判断两台机器之间的可达性 void printReachableIP(InetAddress remoteAddr, int port){ String retIP = null; EnumerationNetworkInterface netInterfaces; try{ netInterfaces = NetworkInterface.getNetworkInterfaces(); while(netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); EnumerationInetAddress localAddrs = ni.getInetAddresses(); while(localAddrs.hasMoreElements()){ InetAddress localAddr = localAddrs.nextElement(); if(isReachable(localAddr, remoteAddr, port, 5000)){ retIP = localAddr.getHostAddress(); break; } } } } catch(SocketException e) { System.out.println( "Error occurred while listing all the local network addresses."); } if(retIP == null){ System.out.println("NULL reachable local IP is found!"); }else{ System.out.println("Reachable local IP is found, it is " + retIP); } } boolean isReachable(InetAddress localInetAddr, InetAddress remoteInetAddr, int port, int timeout) { booleanisReachable = false; Socket socket = null; try{ socket = newSocket(); // 端口号设置为 0 表示在本地挑选一个可用端口进行连接 SocketAddress localSocketAddr = new InetSocketAddress(localInetAddr, 0); socket.bind(localSocketAddr); InetSocketAddress endpointSocketAddr = new InetSocketAddress(remoteInetAddr, port); socket.connect(endpointSocketAddr, timeout); System.out.println("SUCCESS - connection established! Local: " + localInetAddr.getHostAddress() + " remote: " + remoteInetAddr.getHostAddress() + " port" + port); isReachable = true; } catch(IOException e) { System.out.println("FAILRE - CAN not connect! Local: " + localInetAddr.getHostAddress() + " remote: " + remoteInetAddr.getHostAddress() + " port" + port); } finally{ if(socket != null) { try{ socket.close(); } catch(IOException e) { System.out.println("Error occurred while closing socket.."); } } } return isReachable; } 运行结果 --------------START-------------- FAILRE - CAN not connect! Local: 127.0.0.1 remote: 10.8.1.50 port22 FAILRE - CAN not connect! Local: 9.123.231.40 remote: 10.8.1.50 port22 SUCCESS - connection established! Local: 10.0.50.189 remote: 10.8.1.50 port22 Reachable local IP is found, it is 10.0.50.189 --------------END-------------- 回页首IPv4 和 IPv6 混合网络下编程当网络环境中存在 IPv4 和 IPv6,即机器既有 IPv4 地址,又有 IPv6 地址的时候,我们可以对程序进行一些优化,比如 由于IPv4 和 IPv6 地址之间是无法互相访问的,因此仅需要判断 IPv4 地址之间和 IPv6 地址之间的可达性。 对于IPv4 的换回地址可以不做判断,对于 IPv6 的 Linklocal 地址也可以跳过测试 根据实际的需要,我们可以优先考虑选择使用 IPv4 或者 IPv6,提高判断的效率程序清单 3: 判断本地地址和远程地址是否同为 IPv4 或者 IPv6 // 判断是 IPv4 还是 IPv6 if(!((localInetAddr instanceofInet4Address) (remoteInetAddr instanceofInet4Address) || (localInetAddr instanceofInet6Address) (remoteInetAddr instanceofInet6Address))){ // 本地和远程不是同时是 IPv4 或者 IPv6,跳过这种情况,不作检测 break; } 程序清单 4:跳过本地地址和 LinkLocal 地址 if( localAddr.isLoopbackAddress() || localAddr.isAnyLocalAddress() || localAddr.isLinkLocalAddress() ){ // 地址为本地环回地址,跳过 break; } 回页首总结和展望本文列举集中典型的场景,介绍了通过 Java 网络编程接口判断机器之间可达性的几种方式。在实际应用中,可以根据不同的需要选择相应的方法稍加修改即可。对于更加特殊的需求,还可以考虑通过 JNI 的方法直接调用系统 API 来实现,能提供更加强大和灵活的功能,这里就不再赘述了。参考资料 学习 参考developerWorks 的文章 Java 应用程序的网络运行环境编程,获取更多网络编程相关的信息。
如果要通过 JNI 进行网络编程,可以参考 developerWorks 上的文章 用JNI 进行 Java 编程,了解更多 JNI 相关的信息和例子。
参考Javadoc 获取更多关于 Java 网络编程的 API 的信息。
developerWorks Java 技术专区:这里有数百篇关于 Java 编程各个方面的文章。
讨论加入developerWorks 中文社区。查看开发人员推动的博客、论坛、组和维基,并与其他 developerWorks 用户交流。
作者简介吴校军,IBM CSTL 软件工程师,长期从事 IBM 系统管理相关软件的开发,目前负责 Director6.1 Update Manager 的开发。刘冠群现为 IBM 上海系统科技开发中心(CSTL)的软件工程师,有多年的 Java 和 C++ 编程经验,对于操作系统,网络和编程语言的内部实现有强烈兴趣。关闭[x]关于报告滥用的帮助报告滥用谢谢! 此内容已经标识给管理员注意。关闭[x]关于报告滥用的帮助报告滥用报告滥用提交失败。 请稍后重试。关闭[x]developerWorks:登录IBM ID:需要一个 IBM ID?忘记IBM ID?密码:忘记密码?更改您的密码 保持登录。单击提交则表示您同意developerWorks 的条款和条件。 使用条款 当您初次登录到 developerWorks 时,将会为您创建一份概要信息。您在developerWorks 概要信息中选择公开的信息将公开显示给其他人,但您可以随时修改这些信息的显示状态。您的姓名(除非选择隐藏)和昵称将和您在 developerWorks 发布的内容一同显示。所有提交的信息确保安全。关闭[x]请选择您的昵称:当您初次登录到 developerWorks 时,将会为您创建一份概要信息,您需要指定一个昵称。您的昵称将和您在 developerWorks 发布的内容显示在一起。昵称长度在 3 至 31 个字符之间。 您的昵称在 developerWorks 社区中必须是唯一的,并且出于隐私保护的原因,不能是您的电子邮件地址。昵称:(长度在 3 至 31 个字符之间)单击提交则表示您同意developerWorks 的条款和条件。 使用条款. 所有提交的信息确保安全。
JAVA如何生成一个随机的有向连通图
很简单,全部的边存在就是强连通的(要去掉一部分也可以) 也可以把所有点组成一个有向圈 再随机加点边就是了
把矩阵所有的成员都赋一个大于零的随机数
java如何判断一个二维数组两个位置是否连通
给你说个简单的楼主不妨试试吧。其实你这个就是个走迷宫,比之连连看要省事些。
走迷宫的方法就是,没有岔道,对于本题就是当前位置除了身后之外其余相邻的三个位置只有一个是0,就走到那个位置上去;
如果有岔道,对于本题就是当前位置除了身后之外其余相邻的三个位置不只一个是0,就选择相对前进方向最右边的那个位置,也就是总是向右拐(想向左拐也成,这个随便);
要是死胡同,对于本题就是当前位置除了身后之外其余相邻的三个位置没有一个是0,就向后转,走到来的位置上(这个时候需要注意改变前进方向的标记)。
嗯,这也就是一个二叉树遍历的应用。要是能走通,肯定能够到达目的位置(准确的说是目的位置周围的四个位置之一,这个注意判断一下);要是走不通,必然会返回原点(一步都不能走也是一种特殊情况,注意判断啊)。
就这么大的数组,应该很快……呵呵,希望有帮助吧
PS:对了,还要注意,第一步有多种选择,大可随机随便选择方向。但是由于有可能会选到死胡同,这时候需要折回,必然经过起始点;要么就是走那条路,结果从另外一个方向绕回了起始点。但是这时找路并没有结束哦。因为还有其他的选择可以尝试。别的应该再没有问题啦!
Java实现IP是否能Ping通功能
1.Jdk1.5的InetAddresss方式
自从Java 1.5,java.net包中就实现了ICMP ping的功能。
见:Ping类的ping(String)函数,String为ip不加端口号。
使用时应注意,如果远程服务器设置了防火墙或相关的配制,可能会影响到结果。另外,由于发送ICMP请求需要程序对系统有一定的权限,当这个权限无法满足时, isReachable方法将试着连接远程主机的TCP端口 7(Echo)。
2.最简单的办法,直接调用CMD
见Ping类的ping02(String)函数。
3.Java调用控制台执行ping命令
具体的思路是这样的:
通过程序调用类似“ping 127.0.0.1 -n 10 -w 4”的命令,这命令会执行ping十次,如果通顺则会输出类似“来自127.0.0.1的回复: 字节=32 时间1ms TTL=64”的文本(具体数字根据实际情况会有变化),其中中文是根据环境本地化的,有些机器上的中文部分是英文,但不论是中英文环境, 后面的“1ms TTL=62”字样总是固定的,它表明一次ping的结果是能通的。如果这个字样出现的次数等于10次即测试的次数,则说明127.0.0.1是百分之百能连通的。
技术上:具体调用dos命令用Runtime.getRuntime().exec实现,查看字符串是否符合格式用正则表达式实现。
见Ping类的ping(String,int,int)函数。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ping {
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ; //超时应该在3钞以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
return status;
}
public static void ping02(String ipAddress) throws Exception {
String line = null;
try {
Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
BufferedReader buf = new BufferedReader(new InputStreamReader(
pro.getInputStream()));
while ((line = buf.readLine()) != null)
System.out.println(line);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime(); // 将要执行的ping命令,此命令是windows格式的命令
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
try { // 执行命令并获取输出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream())); // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
} // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
return connectedCount == pingTimes;
} catch (Exception ex) {
ex.printStackTrace(); // 出现异常则返回假
return false;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//若line含有=18ms TTL=16字样,说明已经ping通,返回1,否则返回0.
private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+line);
Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
return 1;
}
return 0;
}
public static void main(String[] args) throws Exception {
String ipAddress = "127.0.0.1";
System.out.println(ping(ipAddress));
ping02(ipAddress);
System.out.println(ping(ipAddress, 5, 5000));
}
}
JAVA用1、2、2、3、4、5排列组合,最多能排列多少组合并打印出来。要求:4不能放在第三位,4和5不能相连
算法程序题:
该公司笔试题就1个,要求在10分钟内作完。
题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
static int[] bits = new int[] { 1, 2, 3, 4, 5 };
/**
* @param args
*/
public static void main(String[] args) {
sort("", bits);
}
private static void sort(String prefix, int[] a) {
if (a.length == 1) {
System.out.println(prefix + a[0]);
}
for (int i = 0; i a.length; i++) {
sort(prefix + a[i], copy(a, i));
}
}
private static int[] copy(int[] a,int index){
int[] b = new int[a.length-1];
System.arraycopy(a, 0, b, 0, index);
System.arraycopy(a, index+1, b, index, a.length-index-1);
return b;
}
**********************************************************************
基本思路:
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。
采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i n; i++) {
for (int j = 0; j n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j n; j++) {
if (a[startIndex][j] == 1 visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
Java开发常用的几个数据库连接池
数据库连接池的好处是不言而喻的,现在大部分的application
server都提供自己的数据库连接池方案,此时,只要按照application server的文档说明,正确配置,即可在应用中享受到数据库连接池的好处。
但是,有些时候,我们的应用是个独立的java
application,并不是普通的WEB/J2EE应用,而且是单独运行的,不要什么application
server的配合,这种情况下,我们就需要建立自己的数据库连接池方案了。
1、 DBCP
DBCP是Apache的一个开源项目:
commons.dbcp
DBCP依赖Apache的另外2个开源项目
commons.collections和commons.pool
dbcp包,目前版本是1.2.1:
pool包,目前版本是1.3:,
common-collections包:
下载这些包并将这些包的路径添加到classpath中就可以使用dbcp做为项目中的数据库连接池使用了。
在建立我们自己的数据库连接池时,可以使用xml文件来传入需要的参数,这里只使用hard
code的方式来简单介绍,所有需要我们自己写的代码很少,只要建立一个文件如下:
import
org.apache.commons.dbcp.BasicDataSource;
import
org.apache.commons.dbcp.BasicDataSourceFactory;
import
java.sql.SQLException;
import java.sql.Connection;
import
java.util.Properties;
public class ConnectionSource {
private static BasicDataSource dataSource =
null;
public ConnectionSource() {
}
public static void init() {
if (dataSource != null) {
try
{
dataSource.close();
} catch (Exception e)
{
}
dataSource = null;
}
try {
Properties p = new
Properties();
p.setProperty("driverClassName",
"oracle.jdbc.driver.OracleDriver");
p.setProperty("url",
"jdbc:oracle:thin:@192.168.0.1:1521:testDB");
p.setProperty("password", "scott");
p.setProperty("username",
"tiger");
p.setProperty("maxActive", "30");
p.setProperty("maxIdle", "10");
p.setProperty("maxWait",
"1000");
p.setProperty("removeAbandoned",
"false");
p.setProperty("removeAbandonedTimeout",
"120");
p.setProperty("testOnBorrow", "true");
p.setProperty("logAbandoned", "true");
dataSource = (BasicDataSource)
BasicDataSourceFactory.createDataSource(p);
} catch (Exception e) {
}
}
public static synchronized Connection
getConnection() throws SQLException {
if (dataSource == null) {
init();
}
Connection conn = null;
if (dataSource != null) {
conn = dataSource.getConnection();
}
return conn;
}
}
接下来,在我们的应用中,只要简单地使用ConnectionSource.getConnection()就可以取得连接池中的数据库连接,享受数据库连接带给我们的好处了。当我们使用完取得的数据库连接后,只要简单地使用connection.close()就可把此连接返回到连接池中,至于为什么不是直接关闭此连接,而是返回给连接池,这是因为dbcp使用委派模型来实现Connection接口了。
在使用Properties来创建BasicDataSource时,有很多参数可以设置,比较重要的还有:
testOnBorrow、testOnReturn、testWhileIdle,他们的意思是当是取得连接、返回连接或连接空闲时是否进行有效性验证(即是否还和数据库连通的),默认都为false。所以当数据库连接因为某种原因断掉后,再从连接池中取得的连接,实际上可能是无效的连接了,所以,为了确保取得的连接是有效的,
可以把把这些属性设为true。当进行校验时,需要另一个参数:validationQuery,对oracle来说,可以是:SELECT COUNT(*) FROM
DUAL,实际上就是个简单的SQL语句,验证时,就是把这个SQL语句在数据库上跑一下而已,如果连接正常的,当然就有结果返回了。
还有2个参数:timeBetweenEvictionRunsMillis 和
minEvictableIdleTimeMillis,
他们两个配合,可以持续更新连接池中的连接对象,当timeBetweenEvictionRunsMillis
大于0时,每过timeBetweenEvictionRunsMillis
时间,就会启动一个线程,校验连接池中闲置时间超过minEvictableIdleTimeMillis的连接对象。
还有其他的一些参数,可以参考源代码。
2、
C3P0:
C3P0是一个开放源代码的JDBC连接池,C3PO
连接池是一个优秀的连接池,推荐使用。C3PO实现了JDBC3.0规范的部分功能,因而性能更加突出,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSources 对象。
下载地址:
package
com.systex.utils.web;
import java.beans.PropertyVetoException;
import
java.sql.Connection;
import java.sql.SQLException;
import
javax.sql.DataSource;
import
com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3PODataSource {
private static
ComboPooledDataSource dataSource = null;
private static final String driver
= "com.mysql.jdbc.Driver";
private static final String url =
"jdbc:mysql://localhost:3306/wyd";
private static final String userName =
"root";
private static final String password = "root";
public static DataSource getDataSource() {
if
(dataSource == null) {
dataSource = new ComboPooledDataSource();
try
{
dataSource.setDriverClass(driver);
} catch (PropertyVetoException
e) {
System.out.println("DataSource Load Driver
Exception!!");
e.printStackTrace();
}
dataSource.setJdbcUrl(url);
dataSource.setUser(userName);
dataSource.setPassword(password);
//
设置连接池最大连接容量
dataSource.setMaxPoolSize(20);
//
设置连接池最小连接容量
dataSource.setMinPoolSize(2);
//
设置连接池最大statements对象容量
dataSource.setMaxStatements(100);
}
return
dataSource;
}
public static Connection getConnection() throws
SQLException {
return
C3PODataSource.getDataSource().getConnection();
}
}
3、 Proxool
这是一个Java SQL
Driver驱动程序,提供了对你选择的其它类型的驱动程序的连接池封装。可以非常简单的移植到现存的代码中。完全可配置。快速,成熟,健壮。可以透明地为你现存的JDBC驱动程序增加连接池功能。
官方网站:
下载地址:
关于数字连通java和数字连接成语的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-04,除非注明,否则均为
原创文章,转载请注明出处。