我做个用户登陆系统 里面包含的对象有 用户名 和 密码 可怎么在登陆的时候从数据库查询用户名是否存在;SQL2005 怎么创建用户 并设置密码呢? 我发现怎么跟Oracle 不一样呢? 还有谁帮我解释下下面创建数据库连接池的代码 详细点最好的public class DBConnpool {
private int inUse = 0;
private Vector connections = new Vector();
private String poolname;
private String dbid;
private String drivername;
private String username;
private String passwd;
private int maxconn;
public void DBConnPool(String poolname,String drivername,String dbid,String username,String passwd,
int maxconn){
this.poolname = poolname;
this.dbid = dbid;
this.drivername = drivername;
this.username = username;
this.passwd = passwd;
this.maxconn = maxconn;
}
public synchronized void releaseConnection(Connection con)
{
//connection.addElement(con);
connections.addElement(con);
inUse--;
}
public synchronized Connection getConnection()
{
Connection con = null;
if(connections.size()>0) {
con = (Connection)connections.elementAt(0);
//connections.removeElementAT(0);
connections.removeElementAt(0);
try{
if(con.isClosed())
con = getConnection();
}
catch(Exception e){
e.printStackTrace();
}
}
else if(maxconn == 0||inUse < maxconn){
con = this.newConnection();
}
if(con != null){
inUse++;
}
return con;
}
private Connection newConnection(){
Connection con = null;
try{
Class.forName(drivername);
con = DriverManager.getConnection(dbid,username,passwd);
}
catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeConn(){
Enumeration allConnections = connections.elements();
//connections.elements();
while (allConnections.hasMoreElements()){
Connection con = (Connection)allConnections.nextElement();
try{
con.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
connections.removeAllElements();
}
}
private int inUse = 0;
private Vector connections = new Vector();
private String poolname;
private String dbid;
private String drivername;
private String username;
private String passwd;
private int maxconn;
public void DBConnPool(String poolname,String drivername,String dbid,String username,String passwd,
int maxconn){
this.poolname = poolname;
this.dbid = dbid;
this.drivername = drivername;
this.username = username;
this.passwd = passwd;
this.maxconn = maxconn;
}
public synchronized void releaseConnection(Connection con)
{
//connection.addElement(con);
connections.addElement(con);
inUse--;
}
public synchronized Connection getConnection()
{
Connection con = null;
if(connections.size()>0) {
con = (Connection)connections.elementAt(0);
//connections.removeElementAT(0);
connections.removeElementAt(0);
try{
if(con.isClosed())
con = getConnection();
}
catch(Exception e){
e.printStackTrace();
}
}
else if(maxconn == 0||inUse < maxconn){
con = this.newConnection();
}
if(con != null){
inUse++;
}
return con;
}
private Connection newConnection(){
Connection con = null;
try{
Class.forName(drivername);
con = DriverManager.getConnection(dbid,username,passwd);
}
catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeConn(){
Enumeration allConnections = connections.elements();
//connections.elements();
while (allConnections.hasMoreElements()){
Connection con = (Connection)allConnections.nextElement();
try{
con.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
connections.removeAllElements();
}
}
解决方案 »
- 给大家出个题,问一个多态的问题,大神们谁能解答?
- 为什么我得TOMCAT显示不出来
- 请问JAVA截图时怎么把鼠标光标也截取下来
- jar manifest.mf 打包问题
- 请问:ojdbc14.jar的驱动可不可以用于JDK1.5下的Oracle访问?有没有更好的驱动
- RE:读入文件处理问题?
- java里字面值单独占一个空间吗?
- Vector包含的对象什么时候是可clone的?
- 有没有人有用JAVA的CHARTAPPLET画图的SOURCE?
- 我想问一下,我装了Jbuilder4之后,为什么没有关于Java document的帮助!
- 原来JAVA里也有拷贝构造方法。如代码如示:
- Array 奇数和和偶数和
{
//connection.addElement(con);
connections.addElement(con); //将打开的连接存放到connections中
inUse--; //正在使用的连接数贱1
}
public synchronized Connection getConnection()
{
Connection con = null;
//如果connections中有连接(以前建立的),则取出最前面的一个,并将这个连接从connection中移出
if(connections.size()>0) {
con = (Connection)connections.elementAt(0);
//connections.removeElementAT(0);
connections.removeElementAt(0);
//如果得到的连接已经关闭了,从新去获取
try{
if(con.isClosed())
con = getConnection();
}
catch(Exception e){
e.printStackTrace();
}
}
//如果没有现有的,就新建
else if(maxconn == 0||inUse < maxconn){
con = this.newConnection();
}
if(con != null){
inUse++;
}
return con;
} private Connection newConnection(){
Connection con = null;
try{
Class.forName(drivername);
con = DriverManager.getConnection(dbid,username,passwd);
}
catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
//巴拉巴拉
public synchronized void closeConn(){
Enumeration allConnections = connections.elements();
//connections.elements();
while (allConnections.hasMoreElements()){
Connection con = (Connection)allConnections.nextElement();
try{
con.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
connections.removeAllElements();
}