Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1798759
  • 博文数量: 600
  • 博客积分: 10581
  • 博客等级: 上将
  • 技术积分: 6205
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-06 10:13
文章分类
文章存档

2016年(2)

2015年(9)

2014年(8)

2013年(5)

2012年(8)

2011年(36)

2010年(34)

2009年(451)

2008年(47)

分类: Java

2009-10-11 14:01:18

package ep1;
import java.sql.*;

public class openDB {
    private String DatabaseDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
    private String databaseConnStr= "jdbc:odbc:tt";
    private String username = null;
    private String password = null;
    public Statement stmt = null;
    public Connection conn = null;
    public ResultSet rs = null;
 
    public void setDatabaseConnStr(String databaseConnStr) {
        this.databaseConnStr = databaseConnStr;
    }
    public String getDatabaseConnStr() {
        return databaseConnStr;
    }

    public String getDatabaseDriver() {
            return DatabaseDriver;
    }
    public void setDatabaseDriver(String databaseDriver) {
            DatabaseDriver = databaseDriver;
    }
    // ----------------------------------------------------------
    // 构造函数
    // -----------------------------------------------------------
    openDB() {
            try {
                    Class.forName(this.DatabaseDriver);
                    this.username = "";
                    this.password = "";
            } catch (ClassNotFoundException e) {
                    System.err.println("加载驱动器有错误:" + e.getMessage());
                    System.out.println("执行插入有错误:" + e.getMessage());
            }
    }
    // ---------------------------------------------------------
    // 执行数据插入
    // ---------------------------------------------------------
    public int executeInsert(String sql) {
            int num = 0;
            try {
                    conn = DriverManager.getConnection(this.databaseConnStr,username,
                                    password);
                    stmt = conn.createStatement();
                    num = stmt.executeUpdate(sql);
            } catch (SQLException e) {
                    System.err.println("执行插入有错误:" + e.getMessage());
                    System.out.println("执行插入有错误:" + e.getMessage());
            }
            this.Close();
            return num;
    }
    // ----------------------------------------------------------
    // 执行删除数据
    // ---------------------------------------------------------
    public int ExecuteDelete(String sql) {
            int num = 0;
            try {
                conn = DriverManager.getConnection(this.databaseConnStr,username,
                                    password);
                    stmt = conn.createStatement();
                    num = stmt.executeUpdate(sql);
            } catch (SQLException e) {
                    System.err.println("执行删除有错误:" + e.getMessage());
                    System.out.println("执行删除有错误:" + e.getMessage());
            }
            this.Close();
            return num;
    }
    // ----------------------------------------------------------
    // 执行修改数据
    // ----------------------------------------------------------
    public int ExecuteUpdate(String sql) {
            int num = 0;
            try {
                    conn = DriverManager.getConnection(this.databaseConnStr,username,
                                    password);
                    stmt = conn.createStatement();
                    num = stmt.executeUpdate(sql);
            } catch (SQLException e) {
                    System.err.println("执行修改有错误:" + e.getMessage());
                    System.out.println("执行修改有错误:" + e.getMessage());
            }
            this.Close();
            return num;
    }
    // ---------------------------------------------------------
    // 执行查询数据
    // ---------------------------------------------------------
    public ResultSet ExecuteQuery(String sql) {
            try {
                   conn = DriverManager.getConnection(this.databaseConnStr,username,
                                    password);
                    stmt = conn.createStatement();
                    rs = stmt.executeQuery(sql);
            } catch (SQLException e) {
                    System.err.println("执行查询有错误:" + e.getMessage());
                    System.out.println("执行查询有错误:" + e.getMessage());
            }
            // this.Close();
            return rs;
    }
    // ----------------------------------------------------------
    // 关闭数据库
    // ----------------------------------------------------------
    public void Close() {
            try {
                    if (conn != null) {
                            conn.close();
                    }
                    if (stmt != null) {
                            stmt.close();
                    }
            } catch (Exception end) {
                    System.err.println("执行关闭Connection对象有错误:" + end.getMessage());
                    System.out.println("执行执行关闭Connection对象有错误:有错误:" + end.getMessage()); // 输出到客户端
            }
    }
    // ----------------------------------------------------------
    // 主函数,供测试使用
    // ----------------------------------------------------------
    public static void main(String[] args) {
            openDB login = new openDB();
            String sql = "insert into login values('w','w','1')";
            int n;
            n = login.executeInsert(sql);
            System.out.println(n);
    }

}
阅读(1121) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~