首先是JavaBean的原程序
//——————————————————————
package
mywebconn
import
java.sql.*
public
class
JDBCrs
{
//
static
final
String
sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver"
static
final
String
sConnStr="jdbc:odbc:Driver={MicroSoft
Access
Driver
(*.mdb)}DBQ=D:/myWeb01/WebModule1/test.mdb"
Connection
conn=null
ResultSet
rs=null
public
JDBCrs()
{
try
{
Class.forName(sDBDriver)
}
catch(java.lang.ClassNotFoundException
e)
{
System.err.println(e.getMessage())
}
}
//
the
public
public
ResultSet
executeQuery(String
sql)
{
rs=null
try
{
conn=DriverManager.getConnection(sConnStr)
Statement
stmt=conn.createStatement()
rs=stmt.executeQuery(sql)
}
catch(SQLException
ex){
System.err.println("aq.executeQuery
"+ex.getMessage())
}
return
rs
}
public
void
executeUpdate(String
sql)
{
try
{
conn
=
DriverManager.getConnection(sConnStr)
Statement
stmt
=
conn.createStatement()
stmt.executeUpdate(sql)
conn.close()
}
catch(SQLException
ex)
{
System.err.println("aq.executeUpdate
"+ex.getMessage())
}
}
public
void
close()
{
try
{
if
(!conn.isClosed())
{
conn.close()
}
}
catch
(SQLException
ex)
{
System.err.println("conn.close
"+ex.getMessage())
}
}
}
//——————————————————————
这个JavaBean叫JDBCrs(自己命名)
这个JavaBean是属于mywebconn这个包的(自己定义)
其中有一个叫executeQuery(String
sql)的返回ResultSet对象的方法!
当调用这个方法后,最好要调用close()这个方法关闭连接!
还有一个叫executeUpdate(String
sql)的方法,该方法不返回对象,只执行SQL语句,当执行完后会自动
关闭数据库连接!
String
sDBDriver和String
sConnStr字符串变量要根据实际情况做更改!!
在页面被调用时:
//保证可以使用ResultSet对象
//id
属性是被实例化后的名称
你下个编译环境吧 Myeclipse 现在都是8.5了package DB
import java.sql.*
import java.util.*
public class DbPoolManage {
private static int maxConnectNum=5
private java.sql.Connection conn[]=new Connection[maxConnectNum]
private static ArrayList connectPool=new ArrayList()
private static int flag=0
private String user="sa"//数据库用户名
private String password="123456"//数据库登录密码
private String sSQL=""
private ResultSet rs=null
private Connection conn1=null
private Statement stmt=null
//这里建立所有的连接
private void init()
{
for(int i=0i<this.maxConnectNumi++)
{
conn[i]=getConnectionFromDatabase()
this.connectPool.add(i,conn[i])
}
flag=1
}
public Connection getConnection()
{
Connection conn = null
if (connectPool.size() == 0) {
try {
java.lang.Thread.sleep(1000)
getConnection()
} catch (InterruptedException e) {
System.out.println("连接全部用光,这里sleep出错了.")
}
} else {
conn = (Connection) connectPool.remove(0)
}
return conn
}
private Connection getConnectionFromDatabase()
{
Connection trueConn=null
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance()//这里修改下你百度 就是SQL2008 trueConn= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433databaseName=lectureclient",user,password)
} catch (Exception e) {
System.out.println("数据库连接出错了:"+e.toString())
}
return trueConn
}
private void CreatConAndStmt()
{
try{
if(flag==0){init()}
conn1=this.getConnection()
stmt=conn1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)
}catch(Exception e)
{
e.printStackTrace()
System.out.println("can't execute sql,reson:"+e.getMessage())
}
}
public ResultSet getResult(String sql) throws SQLException
{
rs=null
this.CreatConAndStmt()
rs=stmt.executeQuery(sql)
return rs
}
public boolean executeSql(String sql)
{
try{
this.CreatConAndStmt()
stmt.executeUpdate(sql)
return true
}
catch(Exception e)
{
System.err.println("exectueSql:"+e.toString())
return false
}finally
{
this.Release()
}
}
public void Release(){
try{
if(stmt!=null) stmt.close()
if(conn1!=null) release(conn1)
}catch(Exception e)
{
System.out.println("can't execute sql,reson:"+e.getMessage())
}
System.out.println(this.connectPool.size())
}
public boolean release(Connection conn)
{
return this.connectPool.add(conn)
}
public static void main(String args[]) throws SQLException
{
DbcPoolManage test=new DbcPoolManage()
String sql= "SELECT * FROM lectureLib WHERE (lectureTitle = '的')"+"and (author1='的')"
String num=""
ResultSet rs=null
rs=test.getResult(sql)
while(rs.next())
{
System.out.println(rs.getString("lectureTitle"))
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)