CONST DBCONN="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=aaa.mdb"
Set conn=Server.CreateObject("ADODB.Connection")
conn.open DBCONN
str ="select * from t1"
Set rs=Server.CreateObject("ADODB.Recordset")
rs.open str,conn

解决方案 »

  1.   

    package red;
    /**
     * Title:        select insert update and delete
     * Description:  select insert update and delete
     * Copyright:    Copyright (c) 2002
     * @author luminji
     * @version 1.0
     */import java.sql.*;
    public class aaaOpen
    {
        private String query_statement;  /*定义sql语句*/
        private String param[];          /*查询条件,或者是新的记录*/
        private String strParam[];       //事务中的一系列SQL语句
        private String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
        private String sConnStr="jdbc:odbc:dep_its";    private ResultSet result=null;
        private Connection conn = null;
        private Connection conn1;    public aaaOpen(){
          try{
          Class.forName(sDBDriver);
          this.conn1=DriverManager.getConnection(sConnStr,"sa","");
          this.conn1.setAutoCommit(false);
          this.conn = this.conn1;
          }catch(java.lang.Exception e){
          System.err.println("OpenDb():"+e.getMessage());}
        }    public Connection getConn(){
          return this.conn;
        }    public void setParam(String[] param)
        {
            this.param=param;
        }
        public void setStrParam(String[] temp)
        {
            this.strParam = temp;
        }
        public void setQuerystatement(String query_statement)
        {
            this.query_statement=query_statement;
        }    /*获取查询结果*/
        public ResultSet getResult()
        {
            if(query_statement == null || query_statement == "")
              System.out.println("SQL语句为空!");
            try
            {
                PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
                if (param!=null)
                        for(int i=0;i<param.length;i++)
                            select_stm.setString(i+1,param[i]);
                result=select_stm.executeQuery();
            }catch(Exception e){System.out.println(e);}
            return result;
        }
        //执行事务
        public boolean commitToDb() throws SQLException
        {
          conn.setAutoCommit(false);
          if(strParam == null){
              System.out.println("事务语句为空!");
              return false;
          }
          Statement tt = conn.createStatement();
          try{
            conn.setAutoCommit(false);
            for(int i = 0; i < strParam.length; i++){
              //System.out.println("i=" + i);
              tt.execute(strParam[i]);
            }
            conn.commit();
            }catch(SQLException e){
            conn.rollback();
            tt.close();
            e.printStackTrace();
            return false;
          }
          tt.close();
          conn.setAutoCommit(true);
          return true;
        } /*对数据库进行增加记录操作*/
        public boolean insertRecord() throws SQLException,java.io.UnsupportedEncodingException
        {
            boolean returnTemp = true;
            if(query_statement == null || query_statement == "")
              System.out.println("SQL语句为空!");
            try
            {
                PreparedStatement insert_stm=conn.prepareStatement(query_statement);
                if (param!=null)
                    for(int i=0;i<param.length;i++)
                        insert_stm.setString(i+1,param[i]);
                insert_stm.executeUpdate();
                insert_stm.close();
                conn.commit();
            }
            catch(Exception e)
            {
                System.out.println(e);
                returnTemp = false;
                conn.rollback();
            }
            return returnTemp;
        } /*对数据记录进行更改操作*/
            public boolean updateRecord() throws SQLException,java.io.UnsupportedEncodingException
            {
                boolean returnTemp = true;
                if(query_statement == null || query_statement == "")
                  System.out.println("SQL语句为空!");
                try
                {
                    PreparedStatement update_stm=conn.prepareStatement(query_statement);
                    if (param!=null)
                        for (int i=0;i<param.length;i++)
                            update_stm.setString(i+1,param[i]);
                    update_stm.executeUpdate();
                    returnTemp = true;
                    update_stm.close();
                    conn.commit();
                }
                catch(Exception e)
                {
                    System.out.println(e);
                    returnTemp = false;
                    conn.rollback();
                }
                return returnTemp;
            }
       /*删除数据记录*/
            public boolean deleteRecord() throws SQLException,java.io.UnsupportedEncodingException
            {
                if(query_statement == null || query_statement == "")
                  System.out.println("SQL语句为空!");
                try
                {
                    PreparedStatement delete_stm=conn.prepareStatement(query_statement);
                    if (param!=null)
                        for (int i=0; i < param.length; i++)
                            delete_stm.setString(i+1,param[i]);
                    delete_stm.executeUpdate();
                    delete_stm.close();
                    conn.commit();
                    return true;
                }
                catch(Exception e)
                {
                    System.out.println(e);
                    conn.rollback();
                    return false;
                }
            }        /*//得到统计值
            public float getFloat()
            {
                float returnTemp = 0;
                if(query_statement == null || query_statement == "")
                  System.out.println("SQL语句为空!");
                try
                {
                    Statement rs;
                    returnTemp = rs.getMaxRows(query_statement);
                    rs.close();
                    conn.commit();
                }
                catch(Exception e)
                {
                    System.out.println(e);
                    conn.rollback();
                }
                return returnTemp;
            }
            */
            /*在数据库中执行任何SQL语句*/
            public boolean executeSql() throws SQLException,java.io.UnsupportedEncodingException
            {
                boolean returnTemp = true;
                if(query_statement == null || query_statement == "")
                  System.out.println("SQL语句为空!");
                try
                {
                    PreparedStatement execute_stm=conn.prepareStatement(query_statement);
                    if (param!=null)
                        for (int i=0;i<param.length;i++)
                            execute_stm.setString(i+1,param[i]);
                    execute_stm.execute();
                    returnTemp = true;
                    execute_stm.close();
                    conn.commit();
                }
                catch(Exception e)
                {
                    System.out.println(e);
                    returnTemp = false;
                    conn.rollback();
                }
                return returnTemp;
            }
    }