Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MS SQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 JDBC Object has been closed
huajiazhou





发贴: 14
积分: 0
于 2005-05-21 23:55 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
JAVABEAN连接数据库的代码!
import java.sql.*;
import java.lang.*;
public class sqlBean{
  public Connection conn=null;
  public ResultSet rs=null;
  private String DatabaseDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
  private String DbConnStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mis";
  private String User="sa";
  private String Pswd="5696285";
  
  
  public void setDatabaseDriver(String dbDriver){
    this.DatabaseDriver=dbDriver;
  }
  public void setDatabaseConnStr(String ConnStr){
    this.DbConnStr=ConnStr;
  }
  public void setUser(String user){
    this.User=user;
  }
  public void setPassword(String pwd){
    this.Pswd=pwd;
  }
  public String getDatabaseDriver(){
    return (this.DatabaseDriver);
  }
  public String getDatabaseConnStr(){
    return (this.DbConnStr);
  }
  // constructor
  public sqlBean(){
    try{
      Class.forName(DatabaseDriver);//.newInstance();
    }catch(ClassNotFoundException e){
      System.out.println("Load DB Driver Error:"+e.getMessage());
    }
    try{
      conn = DriverManager.getConnection(DbConnStr,User,Pswd);
    }catch(Exception ex){
      System.out.println("Query Error:"+ex.getMessage());
    }
  }  
  public int executeInsert(String sql){
    int num=0;
    try{
      Statement stmt=conn.createStatement();
      num=stmt.executeUpdate(sql);
    }catch(SQLException ex){
      System.out.println("Insert Data Error:"+ex.getMessage());
    }
    CloseDataBase();
    return num;
  }   
  public ResultSet executeQuery(String sql){
    rs=null;
    try{
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sql);
    }catch(SQLException ex){
      System.out.println("Query Error:"+ex.getMessage());
    }
    CloseDataBase();
    return rs;
  }
  
  public int executeDelete(String sql){
    int num=0;
    try{
      //conn = DriverManager.getConnection(DbConnStr,User,Pswd);
      Statement stmt=conn.createStatement();
      num=stmt.executeUpdate(sql);
    }catch(SQLException ex){
      System.out.println("Delete Data Error:"+ex.getMessage());
    }
    CloseDataBase();
    return num;
  }
  // Close the DB
  public void CloseDataBase(){
    try{
     conn.close();
    }catch(Exception end){
      System.out.println("Can not close the DB:"+end.getMessage());
    }
  }
}
编译没错,但是实际运用在JSP里面后提示java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed. 的错误!
请大家指教啊!这个问题搞的我想自杀,一直解决不了!




Java应用者与<IDE/环境>

话题树型展开
人气 标题 作者 字数 发贴时间
16078 JDBC Object has been closed huajiazhou 2375 2005-05-21 23:55
13207 Re:JDBC Object has been closed huajiazhou 29 2005-05-21 23:55
12990 Re:JDBC Object has been closed why 117 2005-05-22 03:06
14147 Re:JDBC Object has been closed huajiazhou 109 2005-05-22 13:57
13887 Re:JDBC Object has been closed yhjvcnet 56 2005-06-02 14:31

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923