jsp怎么连接sql2005数据库啊 ,好纠结啊,能有人在线指导一下吗?
按照网上的代码和书上的代码,都连接不成功啊 ,都不知道哪出问题了。JSP数据库sql2005

解决方案 »

  1.   

    不成功抛出什么异常或者错误啊。
    jdbc连接数据库总的不就那么两行代码吗。
    加载驱动,打开连接
      

  2.   

    这是用的代码:
    //------------文件名:connection.jsp----------
    <% @ page language="java" import="java.util.*" %>
    <% @ page import = "java.sql.*" %>
    <html>
     <head>
       <title>JSP连接SQL Server数据库示例</title>
     </head>
    <body>
       <%
         Connection conn= null;
         //加载数据库驱动
         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
         //连接数据库URL
         String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
         //数据库用户名
         String user = "sa";
         //数据库密码
         String password ="13518035521xxss";
         //根据数据库参数取得一个数据库连接
         conn = DriverManager.getConnection(url,user,password);
         out.print("取得一个数据库连接: \n");
         out.print(conn.toString());
    %>
    </body>
    </html>
    错误显示:type Exception reportmessage Unable to compile class for JSP: description The server encountered an internal error that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 2 in the jsp file: /work.jsp
    Syntax error on token "language", VariableDeclaratorId expected after this token
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    An error occurred at line: 2 in the jsp file: /work.jsp
    language cannot be resolved to a type
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    An error occurred at line: 2 in the jsp file: /work.jsp
    The left-hand side of an assignment must be a variable
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    An error occurred at line: 2 in the jsp file: /work.jsp
    Syntax error on token "import", delete this token
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    An error occurred at line: 2 in the jsp file: /work.jsp
    Syntax error, insert ";" to complete LocalVariableDeclarationStatement
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    An error occurred at line: 3 in the jsp file: /work.jsp
    Syntax error, insert "enum Identifier" to complete EnumHeaderName
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    6:    <title>JSP连接SQL Server数据库示例</title>
    An error occurred at line: 3 in the jsp file: /work.jsp
    Syntax error, insert "EnumBody" to complete BlockStatement
    1: //------------文件名:connection.jsp----------
    2: <% @ page language="java" import="java.util.*" %>
    3: <% @ page import = "java.sql.*" %>
    4: <html>
    5:  <head>
    6:    <title>JSP连接SQL Server数据库示例</title>
    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      

  3.   

    你是把connection.jsp包含在work.jsp里面的吧,你这种是编译器包含的方式,两个jsp文件都有page标签,当然报错了。错误提示已经很明确了啊,明显是编译错误,和你连接数据库毛关系都没有。
      

  4.   

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    把最有那个新实便给Delete;
    还有;
    Connection conn =  null;   不能等于null ;  不然这个一直都是空,  如果报错的话;
    你可以;Connection conn = DriverManager.getConnection(url,user,password);
      

  5.   

    SQL Server 2000:com.microsoft.jdbc.sqlserver.SQLServerDriver
    SQL Server 2005:com.microsoft.sqlserver.jdbc.SQLServerDriver
      

  6.   

    我是来观看LZ照片的,顺便说一下<%@ page ……%>其中的 <%和@之间不可以有空格  你写成了 <% @ page
      

  7.   

    lib下面加载这个JAR包 ,然后LZ把代码写在JAVA类中,这样可以用 DEBUG运行,看看代码执行到哪一行报错的,这样可以帮助自己分析问题的原因
      

  8.   

    lib下面加载这个包是什么意思?
      

  9.   


    这说的还不够详细吗,把connection.jsp上面的page标签去掉试试。
      

  10.   


    这说的还不够详细吗,把connection.jsp上面的page标签去掉试试。试过了,不行也
      

  11.   


    这个目录是在哪的呀,我不太清楚这点,是tomcat的?