利用连接池与SqlServer连接,我仿照tomcat帮助文档里面对mysql的链接示例做的在conf/server.xml的配置:
<Context path="/liwei" docBase="liwei" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/pubs" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1533/pubs"/>
</Context>
在liwei/web-inf/web.xml的配置:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/pubs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
连接javabean:
package beans;
import javax.sql.DataSource;
import javax.naming.*;
import java.sql.*;
public class DataSour{
private Connection cn;
private Statement st;
private ResultSet rt;
public DataSour(){
try{
Context initcontext=new InitialContext();
Context ex=(Context)initcontext.lookup("java:comp/env");
DataSource source=(DataSource)ex.lookup("jdbc/pubs");
cn=source.getConnection();}
catch(Exception ex){}
}
public ResultSet getData(String str){
try{
st=cn.createStatement();
rt=st.executeQuery(str);}
catch(SQLException er){}
return rt;
}
}
测试页面:<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<html><head><title>baen</title></head>
<body><center>
<h1>property is</h1>
<jsp:useBean id="db" scope="page" class="beans.DataSour"/>
<% String sql="select * from jobs";
ResultSet rt=db.getData(sql);%>
<table border="1" celspacing="0" celpadding="3">
<tr bgcolor="lightgrey">
<th align="center">ID</th>
<th align="center">JOB</th>
<th align="center">MAX</th>
<th align="center">MIN</th></tr>
<%while(rt.next()){%>
<tr>
<td><%=rt.getInt(1)%></td>
<td><%=rt.getString(2)%></td>
<td><%=rt.getInt(3)%></td>
<td><%=rt.getInt(4)%></td>
</tr>
<% }
if(rt!=null)rt.close(); %>
</table>
</center>
</body>
</html>
不知道问题出在哪里....
<Context path="/liwei" docBase="liwei" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/pubs" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1533/pubs"/>
</Context>
在liwei/web-inf/web.xml的配置:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/pubs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
连接javabean:
package beans;
import javax.sql.DataSource;
import javax.naming.*;
import java.sql.*;
public class DataSour{
private Connection cn;
private Statement st;
private ResultSet rt;
public DataSour(){
try{
Context initcontext=new InitialContext();
Context ex=(Context)initcontext.lookup("java:comp/env");
DataSource source=(DataSource)ex.lookup("jdbc/pubs");
cn=source.getConnection();}
catch(Exception ex){}
}
public ResultSet getData(String str){
try{
st=cn.createStatement();
rt=st.executeQuery(str);}
catch(SQLException er){}
return rt;
}
}
测试页面:<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<html><head><title>baen</title></head>
<body><center>
<h1>property is</h1>
<jsp:useBean id="db" scope="page" class="beans.DataSour"/>
<% String sql="select * from jobs";
ResultSet rt=db.getData(sql);%>
<table border="1" celspacing="0" celpadding="3">
<tr bgcolor="lightgrey">
<th align="center">ID</th>
<th align="center">JOB</th>
<th align="center">MAX</th>
<th align="center">MIN</th></tr>
<%while(rt.next()){%>
<tr>
<td><%=rt.getInt(1)%></td>
<td><%=rt.getString(2)%></td>
<td><%=rt.getInt(3)%></td>
<td><%=rt.getInt(4)%></td>
</tr>
<% }
if(rt!=null)rt.close(); %>
</table>
</center>
</body>
</html>
不知道问题出在哪里....
解决方案 »
- 百万火急,求救。。高手高手 来。。。。
- 分页
- java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServDriver
- 寻 找 ecplise 环 境 里 , 下 拉 菜 单 的 代 码 。
- (顶者有分)有经验的老手们进来给些建议,感激不尽!!!
- jsp配置
- velocity 高手请进
- 想找jsp的帮助,哪里能找到?用request.setAttribute传递参数有什么坏处吗?
- 两个String怎样看是否相等?
- JDBC连接SQL SERVER2005 报异常。。谢谢各位了
- 关于jsp和servlet开发的编码问题 中文显示不出来
- 关于JSP环境配置问题!高手请进!
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause java.lang.NullPointerException
beans.DataSour.getData(DataSour.java:19)
org.apache.jsp.datasource_jsp._jspService(datasource_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)不知道是配置的问题,还是连接的代码写的不对
是不是数据源的实现类 没有引用对 导致没有获取数据源
jsp:useBean id="db" scope="page" class="beans.DataSour"/> //==>beans.DataSour.实现类
实现类的引用也没有问题,有俩个连接bean
DB.java:
package beans;
import java.io.PrintStream;
import java.sql.*;
public class Db
{ private String s1;
private String s2;
private Connection con;
private int showcount;
private ResultSet rt;
public Db()
{
s1 = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
s2 = "jdbc:microsoft:sqlserver://127.0.0.1:1533;DatabaseName=pubs";
showcount = 6;
try
{
Class.forName(s1);
con = DriverManager.getConnection(s2, "sa", "");
}
catch(ClassNotFoundException classnotfoundexception)
{
System.out.println(classnotfoundexception);
}
catch(SQLException sqlexception)
{
System.out.println(sqlexception);
}
} public ResultSet doselect(String s)
{
try
{
Statement statement = con.createStatement();
rt = statement.executeQuery(s);
}
catch(SQLException sqlexception)
{
System.out.println(sqlexception);
}
return rt;
} public boolean doupdate(String s)
{
boolean flag = true;
try
{
Statement statement = con.createStatement();
statement.executeQuery(s);
flag = true;
}
catch(SQLException sqlexception)
{
flag = false;
}
return flag;
}
}
如果我用Db,就能查询出数据库的内容。换成DataSour就不能,不知道问题在哪里。。
改为
com.microsoft.sqlserver.jdbc.SQLServerDriver