厦门外贸人's Archiver

Jansfer 发表于 2005-6-27 13:32

[Sqlite]使用sqlite的一个Java示例

[url]http://www.mending.cn/ibook/doc.aspx?uid=17&cid=0&did=3151[/url]

数据库引擎使用JDBC

/*
* Init connection
* # Edwon Jiang
* # [email]edwon2002@hotmail.com[/email]
* # [url]http://www.mending.cn/[/url]
*
*/


初始化数据库链接
[code]
...

import java.sql.*;

...

public Connection GetConnObj() {
        try {
            Class clz = Class.forName("SQLite.JDBCDriver");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:/c:/test.db");
            return conn;
        } catch (ClassNotFoundException ex) {
            this.connErrInfo += ";dbConn ex:" + ex.toString();
        } catch (SQLException es) {
            this.connErrInfo += ";dbConn es:" + es.getMessage();
        } catch (Exception e) {
            this.connErrInfo += ";dbConn e:" + e.getMessage();
        }
        return null;
}

public String commonUpdate(String rSqlString) {
        Connection conn = this.GetConnObj();
        if (conn != null) {
            try {
                Statement stmt = conn.createStatement();
                stmt.execute(rSqlString);
                conn.close();
            } catch (SQLException e) {
                return e.getMessage();
            }
        }
        return "";
}

public ResultSet commonSelect(String rSqlString) {
        Connection conn = this.GetConnObj();
        if (conn != null) {
            try {
                Statement stmt = conn.createStatement();
                stmt.execute(rSqlString);
                ResultSet result = stmt.executeQuery(rSqlString);
                conn.close();
                return result;
            } catch (SQLException es) {
                this.connErrInfo = "dbProcess es:" + es.getMessage();
            } catch (Exception e) {
                this.connErrInfo = "dbProcess e:" + e.getMessage();
            }
        }
        return null;
}
[/code]

数据库操作

[code]
...

public String deleteColumn**yId(String[] rArrIds) {
        return this.commonUpdate(iListSqlString.deleteColumn**yId(rArrIds));
    }

    public ResultSet selectPageTlInUsing(String[] rArrIds) {
        return this.commonSelect(iListSqlString.selectPageTlInUsing(rArrIds));
}

...
[/code]


显示数据

[code]
public String columnsListHtml() {
        iListDbProcess _dbProcess = new iListDbProcess(this.dbFilePath);
        ResultSet _result = _dbProcess.listAllColumns();
        if (null == _result) {
            return _dbProcess.getConnErrInfo();
        } else {
            StringBuffer _outHtml = new StringBuffer("");
            int _pages = 0;
            try {
                int _row = 0;
                while (_result.next()) {
                    String temp = this.listTemplate;
                    String columnId    = _result.getString("columnid");
                    String columnTitle = _result.getString("title");
                    String templateId  = _result.getString("templateid");
                    String datetime    = _result.getString("datetime");

//to do with result ....
                }
            } catch (SQLException es) {
                return "resultSetList es:" + es.getMessage();
            } catch (Exception e) {
                return "resultSetList e:" + e.getMessage();
            }
            return _outHtml.toString();
        }
}
[/code]

页: [1]
Google

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.