⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.java

📁 java+javabean开发的一套网上购物系统.
💻 JAVA
字号:
package cdshop.util;
/**
 * <p>数据库连接专用包 </p>
 * <p>Copyright: wxy Copyright (c) 2004</p>
 * <p>Company:CD Shop </p>
 * @by :wxy
 * @version 1.0
 */
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DataBase {
  public Connection conn;
  public Statement stmt;
  public ResultSet rs=null;
  public InitialContext ctx=null;
  DataSource ds = null;
  public String sqlStr="";

  public DataBase() {
    this.connect();
  }
 public boolean connect(){

   /*   try{
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url ="jdbc:mysql://localhost/cdStore?user=wxy&useUnicode=true&characterEncoding=8859_1";
        conn=DriverManager.getConnection(url);
        stmt = conn.createStatement ();
       }catch(Exception ee){
        System.out.println("connect db error:"+ee.getMessage());
        return false;
       }
      */
     /* try{
       ctx = new InitialContext();
        if (ctx == null)
          throw new Exception("没有匹配的上下文环境");

        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SqlServermidp");
        if (ds == null)
          throw new Exception("没有匹配的数据库");
        conn = ds.getConnection();
        stmt=conn.createStatement();


      }catch(Exception ee){
       System.out.println("connect db error:"+ee.getMessage());
       return false;
      }
      */


       try{
       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
       String url="jdbc:microsoft:sqlserver://localhost:1305;DatabaseName=cdstore";
       String user="sa";
       String password="";
       conn= DriverManager.getConnection(url,user,password);
       stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
     }catch(Exception ee){
     System.out.println("connect db error:"+ee.getMessage());
     return false;
    }




      return true;
    }
 public void close()
 {
    try{
      if(rs!=null)rs.close();
      if(stmt!=null)stmt.close();
      if(conn!=null)conn.close();

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

 }

 public static void main(String[] args) {
     try{
            DataBase db = new DataBase();
            db.connect();
          }catch(Exception e){
            e.printStackTrace();
          }
        }
}

⌨️ 快捷键说明

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