<%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

解决方案 »

  1.   

    private String dbDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";public Connection getConnectionDB(){//设置Connection 变量con 的值
            try {
                Class.forName(dbDriver);
                con=DriverManager.getConnection(url,user,password);
            }catch(Exception e){
                System.out.println("找不到驱动程序");
            }
            return con;
        }
    这样
      

  2.   

    private String url="jdbc:sqlserver://localhost:1433;DatabaseName=Student"; 
    private String user="sa";
    private String password="123";
    private Connection con=null;应该OK了
      

  3.   

    我要写类,就要用到这个String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
      

  4.   

    Connection conn=null;
    你这个conn什么时候让他指向一个对象了。nullpointer 啊
      

  5.   

    to::henson2007() 
    >>我要写类,就要用到这个String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";你的理解错误的,对于不同的数据库,需要使用该数据库厂商提供的JDBC驱动包,通过该驱动包,你才能链接到相应的数据库。jeffysky1(天天向上) 
    给你回答的已经很完善了,所以参考他的答案,你就可以获取你需要的链接了。
      

  6.   

    他是要用odbc连接,是否你没有设置odbc源。在系统中设置好odbc源试试