首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jsp连接sql2000 顽疾 !

2012-02-19 
jsp连接sql2000 顽疾在线等!!!!!%Class.forName( com.microsoft.jdbc.sqlserver.SQLServerDriver )Str

jsp连接sql2000 顽疾 在线等!!!!!
<%Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");  
String   url= "jdbc:sqlserver://localhost:1433;DatabaseName=Student ";  
//student为你的数据库的  
String   user= "henson ";        
String   password= "123 ";  
  Connection   conn=   DriverManager.getConnection(url,user,password);  
Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
String   sql= "select   *   from   student ";  
ResultSet   rs=stmt.executeQuery(sql);  
while(rs.next())   {%>  
  您的第一个字段内容为: <%=rs.getString(1)%>  
  您的第二个字段内容为: <%=rs.getString(2)%>  
   <%}%>  
   <%out.print( "数据库操作成功,恭喜你 ");%>  
   <%rs.close();  
          stmt.close();  
          conn.close();
%>  
用这个可以连接数据库输出数据,打了补丁sp3,放好了驱动文件在tomcat/common/lib中
package   student;
import   java.sql.*;
public   class   conn   {
String   sDBDriver= "sun.jdbc.odbc.JdbcOdbcDriver ";
String   sConnStr= "jdbc:odbc:student ";
Connection   conn=null;    
ResultSet   rs=null;

public   conn(){
try{
Class.forName(sDBDriver);  
}catch(java.lang.ClassNotFoundException   e){
System.err.println(e.getMessage());
}
}
public   ResultSet   executeQuery(String   sql){
try{
conn=DriverManager.getConnection(sConnStr);
Statement   stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException   ex){
System.err.println(ex.getMessage());
}
return   rs;
}
}
这个可以连接到数据源,没有出现提示:[Microsoft][ODBC   驱动程序管理器]   未发现数据源名称并且未指定默认驱动程序
究竟是什么问题呢?   在线等

最后一个Bebug:  
Thread   [http-8080-1]   (Suspended   (exception   NullPointerException))
ErrorDispatcher.createJavacError(String,   Node$Nodes,   StringBuffer,   int,   JspCompilationContext)   line:   526
JspServletWrapper.handleJspException(Exception)   line:   453
JspServletWrapper.service(HttpServletRequest,   HttpServletResponse,   boolean)   line:   371
JspServlet.serviceJspFile(HttpServletRequest,   HttpServletResponse,   String,   Throwable,   boolean)   line:   315
JspServlet.service(HttpServletRequest,   HttpServletResponse)   line:   265
JspServlet(HttpServlet).service(ServletRequest,   ServletResponse)   line:   803
ApplicationFilterChain.internalDoFilter(ServletRequest,   ServletResponse)   line:   269
ApplicationFilterChain.doFilter(ServletRequest,   ServletResponse)   line:   188
StandardWrapperValve.invoke(Request,   Response)   line:   210
StandardContextValve.invoke(Request,   Response)   line:   174
StandardHostValve.invoke(Request,   Response)   line:   127
ErrorReportValve.invoke(Request,   Response)   line:   117
StandardEngineValve.invoke(Request,   Response)   line:   108
CoyoteAdapter.service(Request,   Response)   line:   151
Http11AprProcessor.process(long)   line:   834
Http11AprProtocol$Http11ConnectionHandler.process(long)   line:   640
AprEndpoint$Worker.run()   line:   1286


ThreadWithAttributes(Thread).run()   line:   595


[解决办法]
上面的一个,根本就没有用到数据源,而是 连接数据库的三个驱动包的作用,连上了数据库。所以你没有配置数据源,这个程序也能够访问数据库。当然,这也证明了你的计算机上的设置没错。好事!

如果你用下面的一种方法,就需要配置数据源了。
这两种方式是不一样的。

建议你后面的那个类,也用数据库驱动包写,不然你的工程移植到别的计算机上,会多一个配置数据源的问题,你还要再设置一次。



[解决办法]
Connection conn=null;
你这个conn什么时候让他指向一个对象了。nullpointer 啊
[解决办法]
to::henson2007()
> > 我要写类,就要用到这个String sDBDriver= "sun.jdbc.odbc.JdbcOdbcDriver ";

你的理解错误的,对于不同的数据库,需要使用该数据库厂商提供的JDBC驱动包,通过该驱动包,你才能链接到相应的数据库。

jeffysky1(天天向上)
给你回答的已经很完善了,所以参考他的答案,你就可以获取你需要的链接了。
[解决办法]
他是要用odbc连接,是否你没有设置odbc源。在系统中设置好odbc源试试

热点排行