日期:2014-05-18  浏览次数:20688 次

JavaBean连接sql2000的问题
小弟初学jsp
连接数据库出现一点问题

//DatabaseCon.java
package test.db;
import java.io.*;
import java.sql.*;

public class DatabaseCon{
public Connection con;
public String URL="jdbc:odbc:TSG";
public String Message;

public boolean connection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
Message=ex.getMessage();
System.exit(-1);
}
try{
con=DriverManager.getConnection(URL,"sa","sa");
return true;
}
catch(SQLException ex){
Message=ex.getMessage();
return false;
}
}

public boolean close(){
try{
con.close();
return true;
}
catch(Exception ex){
Message=ex.getMessage();
return false;
}
}

public ResultSet exeSQL(String sqlstr){
try{
connection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr);
return rs;
}
catch(Exception ex){
Message=ex.getMessage();
return null;
}
//return null;
}

public String getString(ResultSet rs,String str){
try{
return rs.getString(str);
}
catch(Exception ex){
Message=ex.getMessage();
return null;
}
//return null;
}
}

//connTest.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>测试数据库连接</title>
</head>
<jsp:useBean id="dbBean" scope="session" class="test.db.DatabaseCon"/>
<body>
<%
if(!dbBean.close())
out.print("连接成功!");
ResultSet rs=dbBean.exeSQL("select * from Book");
String str;
try{
while(rs.next()){
str=dbBean.getString(rs,"b_name");
%>
<%=str%><br />
<%
}
}
catch(Exception ex){
out.print(ex.getMessage());
  out.print("abc");
}

%>

</body>
</html>


运行的结果是:
连接成功!nullabc 
看样子是发生异常了,到底错在哪里?

------解决方案--------------------
驱动问题 ???