免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1363 | 回复: 0
打印 上一主题 下一主题

openDB [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-11 14:01 |只看该作者 |倒序浏览
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);
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/84425/showart_2067663.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP