jsp与sql数据库连接,高分求救!

zql2002 2003-08-23 04:04:12
请问在jsp:usebean中与sqlserver数据库怎样连接的?
<jsp:useBean id = "connpool" scope = "application"
class = "xbook.common.connPool">
<jsp:setProperty name = "connpool" property = "driverName"
value = "com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<jsp:setProperty name = "connpool" property = "jdbcURL"
value = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = bookstore","sa","1010"/>
按以上代码调用不正确。
...全文
69 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zql2002 2003-08-27
  • 打赏
  • 举报
回复
上面jsp代码有误,应为:(/index.jsp)is not available.

<%@ page language="java"
errorPage="bookStoreErr.jsp"
import="java.sql.*,
java.io.*,
java.text.*,
java.util.*,
xbook.common.*,
xbook.bookstore.*;"
contentType = "text/html; charset = GB2312"%>
<jsp:useBean id = "connpool" scope = "application"
class = "xbook.common.connPool">
<jsp:setProperty name = "connpool" property = "driverName"
value = "com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<jsp:setProperty name = "connpool" property = "jdbcURL"
value = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = bookstore","sa","1010"/>
<jsp:setProperty name = "connpool" property = "username" value = "Admin"/>
<jsp:setProperty name ="connpool" property ="password" value = "Admin"/>
<jsp:setProperty name = "connpool" property = "connectionSwitch" value = "on"/>
</jsp:useBean>
<jsp:useBean id = "sqlBridge" scope = "page"
class = "xbook.common.SQLBridge">
<jsp:setProperty name = "sqlBridge" property = "connpool"
value = "<%=connpool%>"/>
<jsp:setProperty name = "sqlBridge" property = "connectionSwitch"
value = "on"/>
</jsp:useBean>
......


zql2002 2003-08-27
  • 打赏
  • 举报
回复
public class connPool {
private static final int defaultMaxConnections = 3;
private Vector freeConnections;
private Hashtable boundConnections;
private String driverName;
private String jdbcURL;
private String username;
private String password;
private int maxConnections;

public connPool(int numConnections) {
maxConnections = numConnections;
boundConnections = null;
driverName = "";
jdbcURL = "";
username = "";
password = "";
}
public connPool(){
this(defaultMaxConnections);
}
public void closeDB() throws SQLException{
if(boundConnections!=null){
for(Enumeration e = boundConnections.elements();
e.hasMoreElements();){
Connection conn = (Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections=null;
}
if(freeConnections!=null){
for(Enumeration e = freeConnections.elements();
e.hasMoreElements() ; ){
Connection conn = (Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections = null;
}
}
public synchronized Connection getConnection() throws SQLException{
if(freeConnections==null) throw new SQLException(
"The conection poll has not been establishd yet");
if(boundConnections.get(Thread.currentThread())!=null)
throw new SQLException(
"Cannot get connections over once for this current running thread");
try{
if(freeConnections.size()==0)
wait();
}
catch(InterruptedException ex){
throw new SQLException(ex.toString());
}
Connection conn = (Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
return conn;
}
public void openDB(String drvName, String url,
String uname, String passwd)
throws SQLException{
try{
boundConnections = new Hashtable(maxConnections);
freeConnections = new Vector(maxConnections);
Class.forName(drvName);
for(int i = 0; i < maxConnections; i++)
freeConnections.addElement(DriverManager.getConnection(url, uname, passwd));

}
catch(Exception ex){
boundConnections = null;
freeConnections = null;
throw new SQLException(ex.toString());
}
}
public synchronized void returnConnection() throws SQLException{
Connection conn =(Connection)boundConnections.remove(Thread.currentThread());
if(conn == null)
throw new SQLException(
"the connection which this current running thread got is not found"
);
freeConnections.addElement(conn);
notify();
}
public void setConnectionSwitch(String on_off) throws ServletException{
try{
if(on_off.equalsIgnoreCase("ON"))
openDB(driverName, jdbcURL, username, password);
else if(on_off.equalsIgnoreCase("OFF"))
closeDB();
}
catch(SQLException ex){
throw new ServletException(ex.toString());
}
}
public void setMaxConnections(int numConnections){
maxConnections = numConnections;
}
public void setDriverName(String drvName){
driverName = drvName;
}
public void setJdbcURL(String url){
jdbcURL = url;
}
public void setUserName(String uname){
username = uname;
}
public void setPassword(String passwd){
password = passwd;
}

}

以上为数据库连接池的代码,下面是jsp的代码,显示出错应是the requested resource(/index.jsp)is not available.

<%@ page language="java"
errorPage="bookStoreErr.jsp"
import="java.sql.*,
java.io.*,
java.text.*,
java.util.*,
xbook.common.*,
xbook.bookstore.*;"
contentType = "text/html; charset = GB2312"%>
<jsp:useBean id = "connpool" scope = "application"
class = "xbook.common.connPool">
<jsp:setProperty name = "connpool" property = "driverName"
value = "com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<jsp:setProperty name = "connpool" property = "jdbcURL"
value = ""/>
<jsp:setProperty name = "connpool" property = "username" value = "Admin"/>
<jsp:setProperty name ="connpool" property ="password" value = "Admin"/>
<jsp:setProperty name = "connpool" property = "connectionSwitch" value = "on"/>
</jsp:useBean>
<jsp:useBean id = "sqlBridge" scope = "page"
class = "xbook.common.SQLBridge">
<jsp:setProperty name = "sqlBridge" property = "connpool"
value = "<%=connpool%>"/>
<jsp:setProperty name = "sqlBridge" property = "connectionSwitch"
value = "on"/>
</jsp:useBean>
......
seaman0916 2003-08-24
  • 打赏
  • 举报
回复
the requested resource(/index.htm)is not available.// index.htm不存在!
说明你访问的是index.htm ,你的这个文件名叫什么 ? 扩展名是.jsp才支持 <jsp:useBean /> 等等,这些东东的!

你把你的javabean代码贴出来!看看
gisgeoboy 2003-08-23
  • 打赏
  • 举报
回复
localhost要用本机的计算机名
dext 2003-08-23
  • 打赏
  • 举报
回复
用ODBC不可以吗?
chanceqw 2003-08-23
  • 打赏
  • 举报
回复
jsp页面用.htm扩展名也可以么?
diewikwang 2003-08-23
  • 打赏
  • 举报
回复
the requested resource(/index.htm)is not available.
~~~~~~~~~~~
我估计跟数据库没关
chanceqw 2003-08-23
  • 打赏
  • 举报
回复
请参看如下文章,也许对你有些帮助:
http://www.zdnet.com.cn/developer/code/story/0,2000081534,39134180,00.htm
zql2002 2003-08-23
  • 打赏
  • 举报
回复
jdk环境变量与Tomcat配置都正确,主要是与数据库连接不上,我怀疑与上面的代码有关,因为
第一次用sqlserver,出错信息如下:the requested resource(/index.htm)is not available.
页面没有错误显示。
chanceqw 2003-08-23
  • 打赏
  • 举报
回复
出错信息是什么啊?可能性太多了啊
也许你CLASSPATH没设对,或者你TOMCAT没配置好。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧