database.java

来自「很实用的JSP代码」· Java 代码 · 共 65 行

JAVA
65
字号
package bookshop.util;
/**
 * <p>数据库连接专用包 </p>
 * <p>Copyright: wxy Copyright (c) 2004</p>
 * <p>Company:juanjuan book shop online </p>
 * @by :wxy
 * @version 1.0
 */
import java.sql.*;
public class DataBase {
  public Connection conn;
  public Statement stmt;
  public ResultSet rs=null;
  public String sqlStr="";
  public String m_strErrMsg = "";

  public DataBase() {
    this.connect();
  }
  public void finalize()
  {
     try
     {
       if (rs != null) {
         rs.close(); ;
       }
       if( stmt != null )
       {
         stmt.close();
       }
       if( conn != null)
       {
          conn.close();
       }

     }
     catch(Exception ex)
     {
       System.out.println("connect db error:"+ex.getMessage());
     }
  }

 public boolean connect(){
      try{
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url ="jdbc:mysql://localhost/BookStore?user=root&password=19850103&characterEncoding=GB2312";
        conn=DriverManager.getConnection(url);
        stmt = conn.createStatement ();
       }catch(Exception ee){
         m_strErrMsg = "使用JDBC驱动连接数据库错误:" + ee.getMessage();
         System.out.println("connect db error:"+ee.getMessage());
        return false;
       }
      return true;
    }
 public static void main(String[] args) {
     try{
            DataBase db = new DataBase();
            db.connect();
          }catch(Exception e){
            e.printStackTrace();
          }
        }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?