是象manage consle那样操作么??
还是只是来执行几句sql语句??

解决方案 »

  1.   

    先建立ODBC/*
    * @dbbean.java
    * Author:nicolas
    * Date:2001-3-21
    * version:1.0v
    * Function:connect database,query table,update table by parameter
    */
    package dbbean;
    import java.sql.*;
    import java.io.*;public class dbbean{
    //String sConnStr="jdbc:oracle:thin:@172.16.1.2:1521:labora","scott","tiger";
    String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
    Connection conn=null;
    ResultSet rs=null;
        
    /*
            * constructor:try to create a dababase driver
      */
    public dbbean(){
    try{
    Class.forName(sDBDriver);
    }catch(java.lang.ClassNotFoundException e){
    System.err.println("dbbean():  "+e.getMessage());
    }
    } public ResultSet executeQuery(String sqlStr){
    rs=null;
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sqlStr);
    }catch(SQLException e){
    System.err.println("executeQuery():  "+e.getMessage());
    }
    return rs;
    } public int executeInsert(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeInsert():  "+e.getMessage());
    return 0;//失败
    }
    }    public int executeUpdate(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeInsert():  "+e.getMessage());
    return 0;//失败
    }
    } public int executeDelete(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeDelete():  "+e.getMessage());
    return 0;//失败
    }
    }
    }
      

  2.   

    给nicolas(nicolas) 题个建议,一家之言,见谅.
    在建立一个Connection的时候,是很花费时间的,所以你的程序就有点效率不高之嫌疑.
    应该只建立一个Connection.
      

  3.   

    偶的一些看法:
    1.摒弃 ODBC,使用专用的 JDBC Driver,连接 SQL Server 本人喜欢用 i-net 的产品
      可以到 http://www.inetsoftware.de/ 下载,使用方法可参考其说明文件。
    2.如果程序比较复杂,可以使用连接池来提高效率(前提:你的应用服务器要支持此项功能)
      

  4.   

    请问FSbaby(虫虫特工) 
    那里有当
    “JDBC Driver,连接 SQL Server ”请MAIL我。[email protected]