⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlsource.java

📁 java编写的OCR软件
💻 JAVA
字号:
/* * SqlSource.java * * Created on 26. M鋜z 2004, 00:51 */package de.spieleck.app.jacson.source;import de.spieleck.app.jacson.JacsonConfigException;import de.spieleck.app.jacson.JacsonException;import de.spieleck.config.ConfigNode;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * This class is a JacsonSource reading input from a database. It is a * GenericSource so it can be configured. * <br> * $Id: SQLSource.java 15 2005-09-28 18:45:04Z pcs $ * @author Patrick Carl * @since 0.89 */public class SQLSource extends GenericChunkSourceBase{        public static final String QUERY_NODE = "query";    public static final String JDBC_URL_NODE = "jdbc-url";    public static final String JDBC_DRIVER_NODE = "driver";    public static final String USER_NODE = "user";    public static final String PASSWORD_NODE = "password";    public static final String READ_MODE_NODE = "read-mode";    public static final String READ_MODE_DEFAULT = "row";    public static final String COLUMN_SEPARATOR_NODE = "separator";    public static final String COLUMN_SEPARATOR_DEFAULT = "*";        private String clazz;    private String url;    private String query;    private String user;    private String password;    private boolean readRows;    private String separator;        private Connection con;    private Statement st;    private ResultSet rs;    private int columnCount;    private int columnIndex;        /**     * inits this Source using the given config node.<br>     * The following settings are used:     * <ul>     * <li>jdbc-driver: the driver to use, make sure it is in your classpath</li>     * <li>jdbc-url: the URL to use for database connection/li>     * <li>query: the query that will be executed/li>     * <li>user: name of the database user (optional)/li>     * <li>password: password of the database user (optional)/li>     * <li>read-mode: defines wether chunks are created from whole lines     * (row-mode) or for every column (column-mode), default is row mode</li>     * <li>separator: inserted in chunks in row-mode to separate column valuess/li>     * </ul>     * This source needs the corresponding JDBC driver within the classpath.     */    public void init(ConfigNode config) throws JacsonConfigException {        // looking up configured data        clazz = config.getString(JDBC_DRIVER_NODE, null);        if(clazz == null)            throw new JacsonConfigException("No driver class configured");        url = config.getString(JDBC_URL_NODE, null);        if(url == null)            throw new JacsonConfigException("No database configured");        query = config.getString(QUERY_NODE, null);        if(query == null)            throw new JacsonConfigException("No query configured");                user = config.getString(USER_NODE, null);        password = config.getString(PASSWORD_NODE, null);        readRows = config.getString(                READ_MODE_NODE, READ_MODE_DEFAULT).equals(READ_MODE_DEFAULT);        separator = config.getString(                COLUMN_SEPARATOR_NODE, COLUMN_SEPARATOR_DEFAULT);                try{            Class.forName(clazz);            con = (user == null && password == null) ?                DriverManager.getConnection(url) :                DriverManager.getConnection(url, user, password);                        st = con.createStatement();            rs = st.executeQuery(query);            columnCount = rs.getMetaData().getColumnCount();        } catch (Exception e){            throw new JacsonConfigException("Could not init SQLSource", e);        }        columnIndex = columnCount + 1;    }        public boolean accept(ConfigNode node) {        String name = node.getName();        return name.equals(QUERY_NODE)            || name.equals(JDBC_DRIVER_NODE) || name.equals(JDBC_URL_NODE)            || name.equals(USER_NODE) || name.equals(PASSWORD_NODE)            || name.equals(READ_MODE_NODE) || name.equals(COLUMN_SEPARATOR_NODE);    }        public String message() {        return "SqlSource running " + query + " at " + url;    }        public String nextChunk() throws JacsonException {        try{            // reading complete row of result and return it            if(readRows){                if(rs.next()){                    StringBuffer sb = new StringBuffer();                    for(int i=1; i <= columnCount; i++)                        sb.append(rs.getString(i) + separator);                    return sb.toString();                }            } else{                // have we reached last column?                if(columnIndex > columnCount){                    // is there a next row?                    if(rs.next()){                        columnIndex = 1;                        return rs.getString(columnIndex++);                    }                } else // not reached last column so continuing in the current row                    return rs.getString(columnIndex++);                            }        } catch (SQLException e){            e.printStackTrace();        }                if(rs != null) {            try{                rs.close();            } catch (SQLException ignore){}        }        if(st != null) {            try{                st.close();            } catch (SQLException ignore){}        }        if(con != null) {            try{                con.close();            } catch (SQLException ignore){}        }        return null;    }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -