代码如下:
dbconn.java如下:
---------------------------------------
package Border; 
import java.sql.*; //完成环境设置,导入java.sql包 
public class dbconn { 
public dbconn() 


//declare variable 
private Connection conn = null; 
private ResultSet rs = null; 
private String server = "127.0.0.1"; 
private String port = "1433"; 
private String db = "publish"; 
private String user = "jieking";
private String pass = "0085529j225"; //change to your password 
private String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=testDate"; public Connection getConn(){ //get database connection 
try{ 
Class.forName(drivername).newInstance(); //载入驱动器 
conn = DriverManager.getConnection(URL,user,pass); //连接到数据库 

catch(Exception e){ 
e.printStackTrace(); 

return conn ; 
} public ResultSet executeSQL(String str) { 
try{ 
Statement stmt = conn.createStatement(); //语句接口 
rs = stmt.executeQuery(str); //获得结果集 

catch(Exception e){ 
e.printStackTrace(); 

return rs; 

}
---------------------------------------
jsp引用代码如下:
----------------------------------------
<%@ page contentType="text/html;charset=GBK" import="java.sql.*;Border.dbconn"%> <jsp:useBean id="Border" scope="page" class="Border.dbconn" /> 
<% 
ResultSet rs = null; 
Connection conn = null; 
conn = test.getConn() ; 
rs = test.executeSQL("select * from book"); 
%> 
<html> 
<body> 
<br> 
<h2 align="center" > My first Jsp JavaBean Mysql </h2> 
<br> 
<table border="1" align="center"> 
<tr> 
<th> 
id 
</th> 
<th> 
title 
</th> 
<th> 
price 
</th> 
</tr> <% 
while(rs.next()) { 
%> 
<tr> 
<th> 
<%=rs.getString("id")%> 
</th> 
<th> 
<%=rs.getString("title")%> 
</th> 
<th> 
<%=rs.getString("price")%> 
</th> 
</tr> 
<%}%> 
<% 
rs.close(); 
conn.close(); 
%> 
</table> 
</body> 
</html>
------------------------------------------------

解决方案 »

  1.   

    <jsp:useBean id="Border" scope="page" class="Border.dbconn" /> 
    <% 
    ResultSet rs = null; 
    Connection conn = null; 
    conn = test.getConn() ; 
    rs = test.executeSQL("select * from book"); 
    %> 
    改为:
    <jsp:useBean id="Border" scope="page" class="Border.dbconn" /> 
    <% 
    ResultSet rs = null; 
    Connection conn = null; 
    conn = Border.getConn() ; 
    rs = Border.executeSQL("select * from book"); 
    %> 
      

  2.   

    你函数中用到的连接是执行getConn时产生的,所以你页面上的conn是没有用的
      

  3.   

    conn = test.getConn() ; 这句话里test是哪里来的!
    应该改成Border吧
      

  4.   

    <%@page import="Border.dbconn"%>......
    ResultSet rs = null; 
    Connection conn = null; 
    dbconn test = new dbcon();
    conn = test.getConn() ; 
    ....
      

  5.   

    vacuumboy(好好学习,天天向上) 那怎样让页面上的conn有用呢?
    xiachedan(瞎扯蛋)不好意思,那里写错了。
      

  6.   

    你要用页面上的conn,executeSQL函数就得接受一个Connection类型的变量.还有就是getConn()改成
    public Connection getConn(){ //get database connection 
    Connection conn = null;
    try{ 
    Class.forName(drivername).newInstance(); //载入驱动器 
    conn = DriverManager.getConnection(URL,user,pass); //连接到数据库 

    catch(Exception e){ 
    e.printStackTrace(); 

    return conn ; 

    然后把类中定义的conn去掉
      

  7.   

    那Connection conn = null;这句是不是也要加入 
    public ResultSet executeSQL(String str) { 
    try{ 
    Statement stmt = conn.createStatement(); //语句接口 
    rs = stmt.executeQuery(str); //获得结果集 

    catch(Exception e){ 
    e.printStackTrace(); 

    return rs; 

    不那样的话那conn.createStatement(); 里的conn会变为没定义的标示符号。
      

  8.   

    我把Connection conn = null;加入executeSQL中还是编译没问题,但是用jsp引用测试还是说
     The value for the useBean class attribute Border.dbconn is invalid
      

  9.   

    那Connection conn = null;这句是不是也要加入 The value for the useBean class attribute Border.dbconn is invalid
    两个没关系,是你的Bean有问题!楼主试试直接生成定义对象,不用
    <jsp:useBean id="Border" scope="page" class="Border.dbconn" /> 直接 Border.dbconn dbconn = new Border.dbconn()
      

  10.   

    Bean有啥问题?
    不用<jsp:useBean id="Border" scope="page" class="Border.dbconn" />这个行吗
    jsp文件加入Border.dbconn dbconn = new Border.dbconn()这句后仍然错误哪位能帮我整好这两段代码
      

  11.   

    改了好几天终于今天上午修成正果
    dbconn.java
    --------------------
    package Border;import java.sql.*; //完成环境设置,导入java.sql包 public class dbconn { 
    public dbconn() { 
    try{
    Class.forName(drivername); //载入驱动器 
    }catch(java.lang.ClassNotFoundException e){
    System.err.println(e.getMessage());
    }

    //declare variable 
    Connection conn = null; 
    ResultSet rs = null; 
    String user = "jieking";
    String pass = "830924k19"; //change to your password 
    String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=publish";  public Connection getConn(){ //get database connection 
    try{ 
    conn = DriverManager.getConnection(URL,user,pass); //连接到数据库 
    } catch(Exception e){ 
    e.printStackTrace(); 

    return conn ; 
    }  public ResultSet executeQuery(String sql) { 
    try{ 
    Statement stmt = conn.createStatement(); //语句接口 
    rs = stmt.executeQuery(sql); //获得结果集
     
    } catch(Exception e){ 
    e.printStackTrace(); 

    return rs;
    }  public int executeUpdate(String sql){
    int result=0;
    try{
    Statement stmt = conn.createStatement(); //语句接口 
    result = stmt.executeUpdate(sql); //获得结果集
     
    }catch(Exception e){ 
    e.printStackTrace(); 

    return result;

    }
    --------------------------
    usedbconn.jsp:
    ------------------
    <%@ page contentType="text/html;charset=GBK" import="java.sql.*"%> 
    <%@ page import="Border.dbconn"%>
    <jsp:useBean id="Border" scope="page" class="Border.dbconn" /> 
    <% 
    ResultSet rs = null; 
    Connection conn = null; 
    conn = Border.getConn() ; 
    rs = Border.executeQuery("select * from book"); 
    %> 
    <html> 
    <body> 
    <br/> 
    <h2 align="center" > My first Jsp JavaBean Mssql </h2> 
    <br/> 
    <table border="1" align="center"> 
    <tr> 
    <th> 
    id 
    </th> 
    <th> 
    title 
    </th> 
    <th> 
    price 
    </th> 
    </tr> <% 
    while(rs.next()) { 
    %> 
    <tr> 
    <th> 
    <%=rs.getString("id")%> 
    </th> 
    <th> 
    <%=rs.getString("title")%> 
    </th> 
    <th> 
    <%=rs.getString("price")%> 
    </th> 
    </tr> 
    <%}%> 
    <% 
    rs.close(); 
    conn.close(); 
    %> 
    </table> 
    </body> 
    </html>
    ------------------------------------
    虽然各位的话没起太大作用,但还是感谢大家