jsp连接数据库大全

作者在 2006-12-10 03:04:00 发布以下内容

ConnectDB.java

package aptech.bbs.dataaccess;
/*此Javabean主要实现连接数据库*/
import java.sql.*;
public class ConnectDB
{
    protected Connection con = null; //定义Connection对象
    protected ResultSet rs = null; //结果集对象rs
   //protected Statement st = null; //定义Statement对象
    protected PreparedStatement pst = null; //定义PreparedStatement对象
    public ConnectDB(){}
    /*1.连接数据库*/
    public void openConnection()throws SQLException
    {
        try
        {
            //加载MYSQL驱动
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            //连接数据库
            this.con = java.sql.DriverManager.getConnection(
                    "jdbc:mysql://localhost/bbsDB?characterEncoding=UTF-8","root","123");
            this.con.setAutoCommit(false); // 关闭自动提交功能
        }
        catch (Exception e)
        {
            if(this.con!=null)
               this.con.close();
            System.out.println("ConnectDB.java is error:"+e.getMessage());
        }
    }
    /*关闭数据库连接的方法*/
     protected void closeAll()throws SQLException
     {
         try
         {
             if(pst!=null)
                 pst.close();
         }
         catch(Exception e)
         {
             System.out.println("close pst is error:"+e.getMessage());
         }
         try
          {
              if(rs!=null)
                  rs.close();
          }
          catch(Exception e)
          {
              System.out.println("close rs is error:"+e.getMessage());
          }
          try
          {
              if(con!=null)
                  con.close();
          }
          catch(Exception e)
          {

默认分类 | 阅读 1404 次
文章评论,共0条
游客请输入验证码
文章分类
最新评论