我做个用户登陆系统 里面包含的对象有 用户名 和 密码 可怎么在登陆的时候从数据库查询用户名是否存在;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();
}
}
{
//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();
}