|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java的桌面程序开发在java程序员里通常叫swing开发,主要用的swing包里的类开发的,也就是通常说的c/s架构开发
//毗连BeanConnectionBean.java
packagecom.pool;
importjava.io.Serializable;
importjava.sql.*;
publicclassConnectionBeanimplementsjava.io.Serializable
{
privateConnectionvConnection=null;
privatebooleanbUseState=false;
publicConnectionBean(){}
publicConnectionBean(Connectionconnection)
{
if(connection!=null)
{
vConnection=connection;
}
}
publicConnectiongetConnection()
{
returnvConnection;
}
publicvoidsetConnection(Connectionconnection)
{
vConnection=connection;
}
publicvoidsetUseState(booleanbUseState)
{
this.bUseState=bUseState;
}
publicbooleangetUseState()
{
returnbUseState;
}
publicvoidclose()
{
try
{
vConnection.close();
}
catch(SQLExceptionsqlException)
{
System.err.println(sqlException.getMessage());
}
}
}
//毗连池
PoolBean.java
packagecom.pool;
importjava.io.Serializable;
importjava.sql.*;
importjava.util.*;
importcom.pool.ConnectionBean;
publicclassPoolBeanimplementsjava.io.Serializable
{
privateStringstrDriver=null;
privateStringstrURL=null;
privateintiSize=0;
privateStringstrUserName="";
privateStringstrPassword="";
privateConnectionBeanvConnectionBean=null;
privateVectorvPool=null;
publicPoolBean(){}
publicvoidsetDriver(StringstrDriver)
{
if(strDriver!=null)
{
this.strDriver=strDriver;
}
}
publicStringgetDriver()
{
returnstrDriver;
}
publicvoidsetURL(StringstrURL)
{
if(strURL!=null)
{
this.strURL=strURL;
}
}
publicStringgetURL()
{
returnstrURL;
}
publicvoidsetSize(intiSize)
{
if(iSize>1)
{
this.iSize=iSize;
}
}
publicintgetSize()
{
returniSize;
}
publicStringgetUserName()
{
returnstrUserName;
}
publicvoidsetUserName(StringstrUserName)
{
if(strUserName!=null)
{
this.strUserName=strUserName;
}
}
publicvoidsetPassword(StringstrPassword)
{
if(strPassword!=null)
{
this.strPassword=strPassword;
}
}
publicStringgetPassword()
{
returnstrPassword;
}
publicvoidsetConnectionBean(ConnectionBeanvConnectionBean)
{
if(vConnectionBean!=null)
{
this.vConnectionBean=vConnectionBean;
}
}
publicConnectionBeangetConnectionBean()throwsException
{
ConnectionvConnection=getConnection();
ConnectionBeanvConnectionBean=newConnectionBean(vConnection);
vConnectionBean.setUseState(true);
returnvConnectionBean;
}
privateConnectioncreateConnection()throwsException
{
ConnectionvConnection=null;
vConnection=DriverManager.getConnection(strURL,strUserName,strPassword);
returnvConnection;
}
publicsynchronizedvoidinitializePool()throwsException
{
if(strDriver==null)
{
thrownewException("没有供应驱动程序称号!");
}
if(strURL==null)
{
thrownewException("没有供应URL!");
}
if(iSize<1)
{
thrownewException("毗连池巨细小于1!");
}
try
{
Class.forName(strDriver);
for(intiIndex=0;iIndex<iSize;iIndex++)
{
ConnectionvConnection=createConnection();
if(vConnection!=null)
{
ConnectionBeanvConnectionBean=newConnectionBean(vConnection);
addConnection(vConnectionBean);
}
}
}
catch(ExceptioneException)
{
System.err.println(eException.getMessage());
thrownewException(eException.getMessage());
}
}
privatevoidaddConnection(ConnectionBeanvConnectionBean)
{
if(vPool==null)
{
vPool=newVector(iSize);
}
vPool.addElement(vConnectionBean);
}
publicsynchronizedvoidreleaseConnection(ConnectionvConnection)
{
for(intiIndex=0;iIndex<vPool.size();iIndex++)
{
ConnectionBeanvConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getConnection()==vConnection)
{
System.err.println("开释第"+iIndex+"个毗连!");
vConnectionBean.setUseState(false);
break;
}
}
}
publicsynchronizedConnectiongetConnection()throwsException
{
ConnectionBeanvConnectionBean=null;
for(intiIndex=0;iIndex<vPool.size();iIndex++)
{
vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getUseState()==false)
{
vConnectionBean.setUseState(true);
ConnectionvConnection=vConnectionBean.getConnection();
returnvConnection;
}
}
try
{
ConnectionvConnection=createConnection();
vConnectionBean=newConnectionBean(vConnection);
vConnectionBean.setUseState(true);
vPool.addElement(vConnectionBean);
}
catch(ExceptioneException)
{
System.err.println(eException.getMessage());
thrownewException(eException.getMessage());
}
returnvConnectionBean.getConnection();
}
publicsynchronizedvoidemptyPool()
{
for(intiIndex=0;iIndex<vPool.size();iIndex++)
{
System.err.println("封闭第"+iIndex+"JDBC毗连!");
ConnectionBeanvConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getUseState()==false)
{
vConnectionBean.close();
}
else
{
try
{
java.lang.Thread.sleep(20000);
vConnectionBean.close();
}
catch(InterruptedExceptioninterruptedException)
{
System.err.println(interruptedException.getMessage());
}
}
}
}
}
用JSDK141下编译经由过程
java也能做一些底层语言开发做的事情(难度很高,不是java顶尖高手是做不来的), |
|