本人初学jsp数据库的使用,看了一些连接数据库的代码,但是总是连接不成功,页面没有显示!
请有经验者指点一下,最好步骤详细点!

解决方案 »

  1.   

    <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <html> <body> <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    String url="jdbc:microsoft:sqlserver: //localhost:1433;DatabaseName=pubs"; 
    //pubs为你的数据库的 String user="sa"; String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
    String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); 
    %> </body> </html>
    这种连接也不行,是不是还需要什么
      

  2.   

    如果用 Tomcat,将 MS 提供的三个 .jar 文件 copy 到 Tomcat 下的 common\lib 文件夹中。在 Tomcat 下的 webapps 目录下新建一个文件夹 sql,在 sql 文件夹中新建文件夹 WEB-INF,打开文本编辑器,录入:
    <webapp>
    </webapp>
    这两行内容,并将其保存为 WEB-INF\web.xml。将下边的代码作为一个 .jsp 文件保存到 sql 目录中:<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1" import="java.sql.*"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    String dirver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String url = "jdbc:microsoft:sqlserver://localhost;DatabaseName=pubs";Class.forName(dirver);
    Connection conn = DriverManager.getConnection(url, "sysman", "password");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM jobs");
    while (rs.next()) {
    out.println(rs.getString(2)+"<br>");
    }
    %>
    </body>
    </html>启动 Tomcat,打开 IE,在地址栏中录入 http://localhost:8080/sql,点击相应的 .jsp 文件即可。
      

  3.   

    去网上下载MsSqlServer的JDBC驱动,并设好CLASSPATH.
    然后并按楼上的做.
      

  4.   

    可以看看这里:
    http://www.blogjava.net/rickhunter/category/3664.html
      

  5.   

    <%@ page contentType="text/html;charset=GB2312" %>
    <%@ page import="java.sql.*" %>
    <HTML>
    <BODY>
     <% Connection con;
        Statement sql; 
        ResultSet rs;
        try
        {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(ClassNotFoundException e)
        {
          out.print("类找不到!");
        }    try 
        {  
             con=DriverManager.getConnection("jdbc:odbc:sun");
     sql=con.createStatement();
             rs=sql.executeQuery("SELECT * FROM information");
        }
       catch(SQLException e1) 
       {
          out.print("SQL异常!");
       }
    %>
    </BODY>
    </HTML>
    运行结果是:SQL异常!
    所以应该是数据库连接错误吧,可能是什么原因呢,谁知道?
      

  6.   

    驱动可以用jdbc或是jtds
    os和驱动要装上最新的补丁包
      

  7.   

    楼上的楼上
    你的代码是没有问题的
    你仔细看看你的“sun"数据源是不是建立好了测试下、有没连接成功
      

  8.   

    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.sql.Connection"%> 
    <%@ page import="java.sql.Statement"%> 
    <%@ page import="java.sql.ResultSet"%> 
    <%@ page import="java.sql.DriverManager"%> <html> <body> <%
    try{
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
        }catch(ClassNotFountException e){
           System.out.println("没有找到数据库驱动异常");
        }
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String url = "jdbc:microsoft:sqlserver: //localhost:1433;DatabaseName=pubs"; 
    String user = "sa"; 
    String password = ""; 
    String sql="select * from test"; 
    try{
        conn= DriverManager.getConnection(url,user,password); 
        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
        rs=stmt.executeQuery(sql); 
    }catch(SQLException e){
        System.out.println("数据库查询异常");
    }catch(Exception e){
        System.out.println("非数据库异常");
    }
    while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%
    out.print("数据库操作成功,恭喜你"); 
    finally{
    if(rs != null || stmt != null || conn != null){
       rs.close();
       stmt.close();
       conn.close();
      }
    }%> </body> </html>
      

  9.   

    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.sql.Connection"%> 
    <%@ page import="java.sql.Statement"%> 
    <%@ page import="java.sql.ResultSet"%> 
    <%@ page import="java.sql.DriverManager"%> <html> <body> <%
    try{
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
        }catch(ClassNotFountException e){
           System.out.println("没有找到数据库驱动异常");
        }
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String url = "jdbc:microsoft:sqlserver: //localhost:1433;DatabaseName=pubs"; 
    String user = "sa"; 
    String password = ""; 
    String sql="select * from test"; 
    try{
        conn= DriverManager.getConnection(url,user,password); 
        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
        rs=stmt.executeQuery(sql); 
    while(rs.next()) {%> 
    您的第一个字段内容为:<%=rs.getString(1)%> 
    您的第二个字段内容为:<%=rs.getString(2)%> 
    <%
    }catch(SQLException e){
        System.out.println("数据库查询异常");
    }catch(Exception e){
        System.out.println("非数据库异常");
    }finally{
    if(rs != null || stmt != null || conn != null){
       rs.close();
       stmt.close();
       conn.close();
      }
    }
    out.print("数据库操作成功,恭喜你"); 
    %> 
    </body> 
    </html>
      

  10.   

    http://blog.sina.com.cn/u/44b8fb7701000002
      

  11.   

    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*"%>
    <html>
    <body>
    <table border=1>
    <tr><td>id</td><td>书名</td><td>出版社</td><td>价格</td></tr>
    <%! String trans(String chi)
    {
                   String result = null;
                   byte temp [];
                   try
                   {
                           temp=chi.getBytes("iso-8859-1");
                          result = new String(temp);
                    }
                    catch(UnsupportedEncodingException e)
                    {
                            System.out.println (e.toString());
                    }
    return result;
    }
    %>
    <%   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
       Connection con=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books","sa","");
       Statement stmt=con.createStatement();
        ResultSet rst=stmt.executeQuery("select * from book");
        while(rst.next())
        {
        out.println("<tr>");
        out.println("<td>"+rst.getString("bookId")+"</td>");
        out.println("<td>"+trans(rst.getString("bookName"))+"</td>");
        out.println("<td>"+trans(rst.getString("publisher"))+"</td>");
        out.println("<td>"+rst.getFloat("price")+"</td>");
        out.println("</tr>");
        }
        //关闭连接、释放资源
        rst.close();
        stmt.close();
        con.close();
        %>    
    </table>
    </body>
    </html>
      

  12.   

    1、在控制面板里的数据源里添加一个系统DSN,例如名称为TMP2、在JSP文件里的部分代码为
       Class.forNmae("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con = DrvierManager("jdbc:odbc:TMP","sa","password");
       Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("sql语句");